Skip to content

Test/store wrapper tests #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/components/storeWrapper.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import expect from 'expect';
import { createStore } from 'redux';
import wrapStore from '../../src/components/storeWrapper';

const mockStore = function(callback) {
return {
dispatch: (action) => {
callback(action);
},
};
};


describe('storeWrapper', () => {
it('should pass new state from provided store to ngReduxStore', () => {
let dispatches = 0;
const providedStore = createStore((state, action) => action.payload);
const ngReduxStore = mockStore((action) => {
dispatches++;
expect(action.type).toEqual('@@NGREDUX_PASSTHROUGH');

if (action.payload) {
expect(action.payload).toEqual('TEST DISPATCH');
}
});

const wrappedStore = wrapStore(providedStore, ngReduxStore);

providedStore.dispatch({
type: 'TEST',
payload: 'TEST DISPATCH'
});

expect(dispatches).toEqual(2);
});
});
89 changes: 89 additions & 0 deletions test/integration/ngReduxProvidedStore.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import expect from 'expect';
import ngReduxProvider from '../../src/components/ngRedux';
import { createStore, combineReducers } from 'redux';


describe('ngRedux provide store functionality', () => {
let ngRedux = {};
let $injector = {};
let $rootScope = {
$evalAsync: () => {}
}

beforeEach(() => {
ngRedux = new ngReduxProvider();
$injector = {
get: () => {
return $rootScope;
},
};
});

it('should dispatch both ways', () => {
const providedStore = createStore((state, action) => action.payload || state, 'initial state');

ngRedux.provideStore(providedStore);

const ngredux = ngRedux['$get']($injector);

expect(ngredux.getState()).toEqual('initial state');

providedStore.dispatch({ type: 'TEST', payload: 'new state through provider' });

expect(providedStore.getState()).toEqual('new state through provider');
expect(ngredux.getState()).toEqual('new state through provider');

ngredux.dispatch({
type: 'TEST',
payload: 'new state through ngredux'
});

expect(providedStore.getState()).toEqual('new state through ngredux');
expect(ngredux.getState()).toEqual('new state through ngredux');
});

it('should work with combineReducers', () => {
const initialFooState = 'foo state';
const initialBarState = 'bar state';
const combinedReducers = combineReducers({
fooState: (state = initialFooState, action) => action.payload || state,
barState: (state = initialBarState, action) => action.payload || state,
})
const providedStore = createStore(combinedReducers);

ngRedux.provideStore(providedStore);

const ngredux = ngRedux['$get']($injector);

expect(ngredux.getState()).toEqual({
fooState: 'foo state',
barState: 'bar state'
});

providedStore.dispatch({ type: 'TEST', payload: 'new state through provider' });

expect(providedStore.getState()).toEqual({
fooState: 'new state through provider',
barState: 'new state through provider'
});
expect(ngredux.getState()).toEqual({
fooState: 'new state through provider',
barState: 'new state through provider'
});

ngredux.dispatch({
type: 'TEST',
payload: 'new state through ngredux'
});

expect(providedStore.getState()).toEqual({
fooState: 'new state through ngredux',
barState: 'new state through ngredux'
});
expect(ngredux.getState()).toEqual({
fooState: 'new state through ngredux',
barState: 'new state through ngredux'
});
});

});