Skip to content

Commit c1fa0de

Browse files
authored
Merge pull request #219 from angular-redux/test/store-wrapper-tests
Test/store wrapper tests
2 parents 18b123f + effcfbe commit c1fa0de

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

test/components/storeWrapper.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import expect from 'expect';
2+
import { createStore } from 'redux';
3+
import wrapStore from '../../src/components/storeWrapper';
4+
5+
const mockStore = function(callback) {
6+
return {
7+
dispatch: (action) => {
8+
callback(action);
9+
},
10+
};
11+
};
12+
13+
14+
describe('storeWrapper', () => {
15+
it('should pass new state from provided store to ngReduxStore', () => {
16+
let dispatches = 0;
17+
const providedStore = createStore((state, action) => action.payload);
18+
const ngReduxStore = mockStore((action) => {
19+
dispatches++;
20+
expect(action.type).toEqual('@@NGREDUX_PASSTHROUGH');
21+
22+
if (action.payload) {
23+
expect(action.payload).toEqual('TEST DISPATCH');
24+
}
25+
});
26+
27+
const wrappedStore = wrapStore(providedStore, ngReduxStore);
28+
29+
providedStore.dispatch({
30+
type: 'TEST',
31+
payload: 'TEST DISPATCH'
32+
});
33+
34+
expect(dispatches).toEqual(2);
35+
});
36+
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import expect from 'expect';
2+
import ngReduxProvider from '../../src/components/ngRedux';
3+
import { createStore, combineReducers } from 'redux';
4+
5+
6+
describe('ngRedux provide store functionality', () => {
7+
let ngRedux = {};
8+
let $injector = {};
9+
let $rootScope = {
10+
$evalAsync: () => {}
11+
}
12+
13+
beforeEach(() => {
14+
ngRedux = new ngReduxProvider();
15+
$injector = {
16+
get: () => {
17+
return $rootScope;
18+
},
19+
};
20+
});
21+
22+
it('should dispatch both ways', () => {
23+
const providedStore = createStore((state, action) => action.payload || state, 'initial state');
24+
25+
ngRedux.provideStore(providedStore);
26+
27+
const ngredux = ngRedux['$get']($injector);
28+
29+
expect(ngredux.getState()).toEqual('initial state');
30+
31+
providedStore.dispatch({ type: 'TEST', payload: 'new state through provider' });
32+
33+
expect(providedStore.getState()).toEqual('new state through provider');
34+
expect(ngredux.getState()).toEqual('new state through provider');
35+
36+
ngredux.dispatch({
37+
type: 'TEST',
38+
payload: 'new state through ngredux'
39+
});
40+
41+
expect(providedStore.getState()).toEqual('new state through ngredux');
42+
expect(ngredux.getState()).toEqual('new state through ngredux');
43+
});
44+
45+
it('should work with combineReducers', () => {
46+
const initialFooState = 'foo state';
47+
const initialBarState = 'bar state';
48+
const combinedReducers = combineReducers({
49+
fooState: (state = initialFooState, action) => action.payload || state,
50+
barState: (state = initialBarState, action) => action.payload || state,
51+
})
52+
const providedStore = createStore(combinedReducers);
53+
54+
ngRedux.provideStore(providedStore);
55+
56+
const ngredux = ngRedux['$get']($injector);
57+
58+
expect(ngredux.getState()).toEqual({
59+
fooState: 'foo state',
60+
barState: 'bar state'
61+
});
62+
63+
providedStore.dispatch({ type: 'TEST', payload: 'new state through provider' });
64+
65+
expect(providedStore.getState()).toEqual({
66+
fooState: 'new state through provider',
67+
barState: 'new state through provider'
68+
});
69+
expect(ngredux.getState()).toEqual({
70+
fooState: 'new state through provider',
71+
barState: 'new state through provider'
72+
});
73+
74+
ngredux.dispatch({
75+
type: 'TEST',
76+
payload: 'new state through ngredux'
77+
});
78+
79+
expect(providedStore.getState()).toEqual({
80+
fooState: 'new state through ngredux',
81+
barState: 'new state through ngredux'
82+
});
83+
expect(ngredux.getState()).toEqual({
84+
fooState: 'new state through ngredux',
85+
barState: 'new state through ngredux'
86+
});
87+
});
88+
89+
});

0 commit comments

Comments
 (0)