Skip to content

Commit

Permalink
Issue #15: added test to make sure combined reducers handle incoming …
Browse files Browse the repository at this point in the history
…actions only once

Change-Id: I9d4dc8d803372249df46b21b7feb57a89d783acf
  • Loading branch information
Patrick Hund committed Jan 24, 2019
1 parent 321fdd9 commit f032aad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
17 changes: 13 additions & 4 deletions packages/bricks/src/utils/combineNestedReducers.test.js
@@ -1,10 +1,10 @@
import { combineNestedReducers, createFakeActionAndReducer } from '.';
import { createStore } from 'redux';

const [foo, fooAction] = createFakeActionAndReducer('FOO');
const [bar, barAction] = createFakeActionAndReducer('BAR');
const [baz, bazAction] = createFakeActionAndReducer('BAZ');
const [bazong, bazongAction] = createFakeActionAndReducer('BAZONG');
const [foo, fooAction, fooSpy] = createFakeActionAndReducer('FOO');
const [bar, barAction, barSpy] = createFakeActionAndReducer('BAR');
const [baz, bazAction, bazSpy] = createFakeActionAndReducer('BAZ');
const [bazong, bazongAction, bazongSpy] = createFakeActionAndReducer('BAZONG');

describe('When I combine some nested reducers', () => {
let reducer;
Expand All @@ -26,6 +26,15 @@ describe('When I combine some nested reducers', () => {
store.dispatch(bazongAction);
});
describe('the resulting state', () => it('is correct', () => expect(store.getState()).toMatchSnapshot()));
describe('the “foo” reducer', () =>
it('handles the “foo” action only once', () => expect(fooSpy).toHaveBeenCalledTimes(1)));
describe('the “bar” reducer', () =>
it('handles the “bar” action only once', () => expect(barSpy).toHaveBeenCalledTimes(1)));
describe('the “baz” reducer', () =>
it('handles the “baz” action only once', () => expect(bazSpy).toHaveBeenCalledTimes(1)));
describe('the “bazong” reducer', () =>
it('handles the “bazong” action only once', () => expect(bazongSpy).toHaveBeenCalledTimes(1)));
});
afterEach(() => jest.clearAllMocks());
});
});
17 changes: 13 additions & 4 deletions packages/bricks/src/utils/createFakeActionAndReducer.js
@@ -1,5 +1,14 @@
/* this is used only in unit tests */
export default type => [
(state = {}, action = {}) => (action.type === type ? { ...state, value: action.value } : state),
{ type, value: type.toLowerCase() }
];
/* global jest */
export default type => {
const spy = jest.fn();
const reducer = (state = {}, action = {}) => {
if (action.type === type) {
spy(action.value);
return { ...state, value: action.value };
}
return state;
};
const action = { type, value: type.toLowerCase() };
return [reducer, action, spy];
};

0 comments on commit f032aad

Please sign in to comment.