Redux : writing a counter reducer with tests
by amrendra kumar
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
JavaScript
'use strict';
console.clear();
function counter(state=0, action) {
switch(action.type) {
case 'INCREMENT':
return ++state;
case 'DECREMENT':
return --state;
default:
return state;
}
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: 'smth'})
).toEqual(1);
expect(
counter(undefined, {})
).toEqual(0);
console.log('All Tests Passed!');