Skip to content

Commit d384394

Browse files
committed
Added test for global and local middlewares of component state redices
1 parent 4436d16 commit d384394

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

test/componentStateStore.spec.js

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import test from 'tape';
33
import { createStore, combineReducers, compose } from 'redux';
44
import { reduxComponentStateStore } from '../src';
55
import { addTodo } from './helpers/actionCreators';
6+
import { testMiddleware } from './helpers/middleware';
67
import * as reducers from './helpers/reducers';
78

89
const space = ' ';
9-
const setup = () => {
10+
const setup = ({ globalMiddlewares = [], localMiddlewares = [] } = {}) => {
1011
// create redux store
11-
const createFinalStore = compose(reduxComponentStateStore)(createStore);
12+
const createFinalStore = compose(reduxComponentStateStore(...globalMiddlewares))(createStore);
1213
const store = createFinalStore(combineReducers(reducers));
1314

1415
// create redux reducer for component state
@@ -19,6 +20,7 @@ const setup = () => {
1920
const minimalConfig = {
2021
key: '',
2122
reducer,
23+
middlewares: localMiddlewares,
2224
};
2325
const completeConfig = Object.assign({}, minimalConfig, {
2426
initialState: {
@@ -27,6 +29,7 @@ const setup = () => {
2729
text: 're-hydratated todo',
2830
}],
2931
},
32+
middlewares: localMiddlewares,
3033
});
3134
return {store, reducer, subscribe, minimalConfig, completeConfig};
3235
};
@@ -35,11 +38,19 @@ const setup = () => {
3538
test('reduxComponentStateStore', ({end}) => end() );
3639

3740
test(`.${space}should be a function`, ({ok, equal, end}) => {
38-
ok(reduxComponentStateStore, 'should exists');
41+
ok(reduxComponentStateStore);
3942
equal(typeof reduxComponentStateStore, 'function');
4043
end();
4144
});
4245

46+
test(`.${space}should accept 0..n middlewares functions as arguments`, ({throws, doesNotThrow, end}) => {
47+
throws( () => reduxComponentStateStore({}) );
48+
throws( () => reduxComponentStateStore([testMiddleware]) );
49+
doesNotThrow( () => reduxComponentStateStore(testMiddleware('fix')) );
50+
doesNotThrow( () => reduxComponentStateStore(testMiddleware('fix'), testMiddleware('nix') ) );
51+
end();
52+
});
53+
4354
test(`.${space}should expose the public API`, ({equal, end}) => {
4455
const { store } = setup();
4556
const methods = Object.keys(store);
@@ -56,6 +67,7 @@ test(`.${space}should expose the public API`, ({equal, end}) => {
5667

5768
test(`.${space}should require a valid configuration object`, ({throws, doesNotThrow, end}) => {
5869
const { subscribe, minimalConfig, completeConfig } = setup();
70+
5971
throws( subscribe );
6072
throws( () => subscribe({}) );
6173
throws( () => subscribe({a: 33, b: 'temp'}) );
@@ -67,6 +79,14 @@ test(`.${space}should require a valid configuration object`, ({throws, doesNotTh
6779
completeConfig.key = 'keyB';
6880
doesNotThrow( () => subscribe(completeConfig) );
6981

82+
minimalConfig.key = 'keyC';
83+
minimalConfig.middlewares = [testMiddleware('suffix')];
84+
doesNotThrow( () => subscribe(minimalConfig) );
85+
86+
minimalConfig.key = 'keyD';
87+
minimalConfig.middlewares = {};
88+
throws( () => subscribe(minimalConfig) );
89+
7090
Reflect.deleteProperty(completeConfig.initialState, 'todos');
7191
throws( () => subscribe(completeConfig) );
7292

@@ -135,6 +155,62 @@ test(`.${space}${space}should create a new store populated with given initial st
135155
end();
136156
});
137157

158+
test(`.${space}${space}should support middlewares shared between all component states reducers`, ({equal, end}) => {
159+
const suffix = ' tested';
160+
const { subscribe, minimalConfig } = setup({
161+
globalMiddlewares: [testMiddleware(suffix)],
162+
});
163+
const text = 'test todo';
164+
165+
minimalConfig.key = 'keyA';
166+
const subscription = subscribe(minimalConfig);
167+
subscription.dispatch(addTodo(text));
168+
169+
const todos = subscription.getState().todos;
170+
equal( todos.length, 1 );
171+
equal( todos[0].text, text + suffix );
172+
173+
end();
174+
});
175+
176+
test(`.${space}${space}should support middlewares specific for a component state reducers`, ({equal, end}) => {
177+
const suffix = ' tested';
178+
const { subscribe, minimalConfig } = setup({
179+
localMiddlewares: [testMiddleware(suffix)],
180+
});
181+
const text = 'test todo';
182+
183+
minimalConfig.key = 'keyA';
184+
const subscription = subscribe(minimalConfig);
185+
subscription.dispatch(addTodo(text));
186+
187+
const todos = subscription.getState().todos;
188+
equal( todos.length, 1 );
189+
equal( todos[0].text, text + suffix );
190+
191+
end();
192+
});
193+
194+
test(`.${space}${space}should support middlewares both shared and specific for a component state reducers`, ({equal, end}) => {
195+
const suffix = ' tested';
196+
const globalSuffix = ' global';
197+
const { subscribe, minimalConfig } = setup({
198+
globalMiddlewares: [testMiddleware(globalSuffix)],
199+
localMiddlewares: [testMiddleware(suffix)],
200+
});
201+
const text = 'test todo';
202+
203+
minimalConfig.key = 'keyA';
204+
const subscription = subscribe(minimalConfig);
205+
subscription.dispatch(addTodo(text));
206+
207+
const todos = subscription.getState().todos;
208+
equal( todos.length, 1 );
209+
equal( todos[0].text, text + globalSuffix + suffix );
210+
211+
end();
212+
});
213+
138214
test(`.${space}componentState.unsubscribe()`, ({end}) => end() );
139215

140216
test(`.${space}${space}should remove the component store from redux`, ({notOk, end}) => {

test/helpers/middleware.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ export function thunk({ dispatch, getState }) {
44
action(dispatch, getState) :
55
next(action);
66
}
7+
8+
export function testMiddleware(suffix) {
9+
return () => next => action => {
10+
action.text = action.text + suffix;
11+
return next(action);
12+
};
13+
}
14+
15+

0 commit comments

Comments
 (0)