forked from zalmoxisus/crossbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for actions and reducers from redux counter example
- Loading branch information
1 parent
9815ca7
commit 65c52ca
Showing
3 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import expect from 'expect'; | ||
import { applyMiddleware } from 'redux'; | ||
import thunk from 'redux-thunk'; | ||
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../../../src/app/constants/ActionTypes'; | ||
import * as actions from '../../../src/app/actions/counter'; | ||
|
||
const middlewares = [thunk]; | ||
|
||
/* | ||
* Creates a mock of Redux store with middleware. | ||
*/ | ||
function mockStore(getState, expectedActions, onLastAction) { | ||
if (!Array.isArray(expectedActions)) { | ||
throw new Error('expectedActions should be an array of expected actions.'); | ||
} | ||
if (typeof onLastAction !== 'undefined' && typeof onLastAction !== 'function') { | ||
throw new Error('onLastAction should either be undefined or function.'); | ||
} | ||
|
||
function mockStoreWithoutMiddleware() { | ||
return { | ||
getState() { | ||
return typeof getState === 'function' ? | ||
getState() : | ||
getState; | ||
}, | ||
|
||
dispatch(action) { | ||
const expectedAction = expectedActions.shift(); | ||
expect(action).toEqual(expectedAction); | ||
if (onLastAction && !expectedActions.length) { | ||
onLastAction(); | ||
} | ||
return action; | ||
} | ||
}; | ||
} | ||
|
||
const mockStoreWithMiddleware = applyMiddleware( | ||
...middlewares | ||
)(mockStoreWithoutMiddleware); | ||
|
||
return mockStoreWithMiddleware(); | ||
} | ||
|
||
describe('actions', () => { | ||
it('increment should create increment action', () => { | ||
expect(actions.increment()).toEqual({ type: actions.INCREMENT_COUNTER }); | ||
}); | ||
|
||
it('decrement should create decrement action', () => { | ||
expect(actions.decrement()).toEqual({ type: actions.DECREMENT_COUNTER }); | ||
}); | ||
|
||
it('incrementIfOdd should create increment action', (done) => { | ||
const expectedActions = [ | ||
{ type: actions.INCREMENT_COUNTER } | ||
]; | ||
const store = mockStore({ counter: 1 }, expectedActions, done); | ||
store.dispatch(actions.incrementIfOdd()); | ||
}); | ||
|
||
it('incrementIfOdd shouldnt create increment action if counter is even', (done) => { | ||
const expectedActions = []; | ||
const store = mockStore({ counter: 2 }, expectedActions); | ||
store.dispatch(actions.incrementIfOdd()); | ||
done(); | ||
}); | ||
|
||
it('incrementAsync should create increment action', (done) => { | ||
const expectedActions = [ | ||
{ type: actions.INCREMENT_COUNTER } | ||
]; | ||
const store = mockStore({ counter: 0 }, expectedActions, done); | ||
store.dispatch(actions.incrementAsync(100)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import expect from 'expect'; | ||
import counter from '../../../src/app/reducers/counter'; | ||
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../../../src/app/actions/counter'; | ||
|
||
describe('reducers', () => { | ||
describe('counter', () => { | ||
it('should handle initial state', () => { | ||
expect(counter(undefined, {})).toBe(0); | ||
}); | ||
|
||
it('should handle INCREMENT_COUNTER', () => { | ||
expect(counter(1, { type: INCREMENT_COUNTER })).toBe(2); | ||
}); | ||
|
||
it('should handle DECREMENT_COUNTER', () => { | ||
expect(counter(1, { type: DECREMENT_COUNTER })).toBe(0); | ||
}); | ||
|
||
it('should handle unknown action type', () => { | ||
expect(counter(1, { type: 'unknown' })).toBe(1); | ||
}); | ||
}); | ||
}); |