|
| 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