Skip to content

Instantly share code, notes, and snippets.

@TapaniAla
Last active June 8, 2017 05:20
Show Gist options
  • Save TapaniAla/78f459ccf076953e3d9f30eb5f108761 to your computer and use it in GitHub Desktop.
Save TapaniAla/78f459ccf076953e3d9f30eb5f108761 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/cequnu
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
expect(
counter(0, {type: 'INCREMENT'})
).toEqual(1);
expect(
counter(1, {type:'INCREMENT'})
).toEqual(2);
expect(
counter(2, {type: 'DECREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'DECREMENT'})
).toEqual(0);
expect(
counter(1, {type: 'SOMETHING_ELSE'})
).toEqual(1);
expect(
counter(undefined, {})
).toEqual(0);
console.log('Test passed!');
'use strict';
function counter(state, action) {
return state;
}
expect(counter(0, { type: 'INCREMENT' })).toEqual(1);
expect(counter(1, { type: 'INCREMENT' })).toEqual(2);
expect(counter(2, { type: 'DECREMENT' })).toEqual(1);
expect(counter(1, { type: 'DECREMENT' })).toEqual(0);
console.log('Test passed!');
function counter(state, action) {
if (action.type === 'INCREMENT') {
return state + 1;
} else if (action.type === 'DECREMENT') {
return state - 1;
}
}
expect(
counter(0, {type: 'INCREMENT'})
).toEqual(1);
expect(
counter(1, {type:'INCREMENT'})
).toEqual(2);
expect(
counter(2, {type: 'DECREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'DECREMENT'})
).toEqual(0);
console.log('Test passed!');
function counter(state, action) {
if (action.type === 'INCREMENT') {
return state + 1;
} else if (action.type === 'DECREMENT') {
return state - 1;
} else {
return state;
}
}
expect(
counter(0, {type: 'INCREMENT'})
).toEqual(1);
expect(
counter(1, {type:'INCREMENT'})
).toEqual(2);
expect(
counter(2, {type: 'DECREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'DECREMENT'})
).toEqual(0);
expect(
counter(1, {type: 'SOMETHING_ELSE'})
).toEqual(1);
console.log('Test passed!');
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
expect(
counter(0, {type: 'INCREMENT'})
).toEqual(1);
expect(
counter(1, {type:'INCREMENT'})
).toEqual(2);
expect(
counter(2, {type: 'DECREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'DECREMENT'})
).toEqual(0);
expect(
counter(1, {type: 'SOMETHING_ELSE'})
).toEqual(1);
expect(
counter(undefined, {})
).toEqual(0);
console.log('Test passed!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment