-
Notifications
You must be signed in to change notification settings - Fork 9
/
redux.spec.js
121 lines (100 loc) · 3.85 KB
/
redux.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {describe, it} from 'mocha';
import chai, {expect} from 'chai';
import React from 'react';
import {Provider, connect} from 'react-redux';
import {
createStore, applyMiddleware, combineReducers, compose, bindActionCreators,
} from 'redux';
import createSagaMiddleware from 'redux-saga';
import {createAction} from 'redux-actions';
import {takeLatest, all} from 'redux-saga/effects';
import {connectRoutes} from 'redux-first-router';
import createHistory from 'history/createMemoryHistory';
import queryString from 'query-string';
import {mount} from 'enzyme';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import {setOrderSaga} from '../../../../sagas';
import orderReducer from '../../../../reducers/order';
import ReduxSort from './redux';
chai.use(sinonChai);
describe('ReduxSort', () => {
const options = [
{label: 'Label A', value: {by: 'by A', direction: 'direction A'}},
{label: 'Label B', value: {by: 'by B', direction: 'direction B'}},
];
const reduxSetup = () => {
// setup routes
const routesMap = {
DUMMY: '/dummyModel',
};
const connectedRoutes = connectRoutes(routesMap, {
createHistory,
querySerializer: queryString,
initialEntries: ['/dummyModel'],
});
// setup actions
const actionTypes = {
order: {
SET: 'DUMMY_MODEL_ORDER',
},
};
const actions = {
order: {set: createAction(actionTypes.order.SET)},
};
const orderReducerSpy = sinon.spy(orderReducer(actionTypes));
// setup sagas
const setOrderSagaSpy = sinon.spy(setOrderSaga);
const sagas = function* sagas() {
yield all([
takeLatest(actionTypes.order.SET, setOrderSagaSpy),
]);
};
const sagaMiddleWare = createSagaMiddleware();
// setup store
const defaultState = {
dummyModel: {
order: {},
},
};
const rootReducer = combineReducers({
dummyModel: orderReducerSpy,
location: connectedRoutes.reducer,
});
const middlewares = applyMiddleware(
connectedRoutes.middleware,
sagaMiddleWare,
);
const store = createStore(rootReducer, defaultState, compose(connectedRoutes.enhancer, middlewares));
sagaMiddleWare.run(sagas);
// mount component
const mapDispatchToProps = dispatch => bindActionCreators({
setOrder: actions.order.set,
}, dispatch);
const Wrapper = connect(null, mapDispatchToProps)(({setOrder}) => <ReduxSort options={options} model="dummyModel" setOrder={setOrder} />);
const wrapper = mount(<Provider store={store}>
<Wrapper />
</Provider>);
return {
wrapper, orderReducerSpy, setOrderSagaSpy, store, actionTypes,
};
};
it('updates location with new selected value', () => {
const {
wrapper, orderReducerSpy, setOrderSagaSpy, store, actionTypes,
} = reduxSetup();
// simulate select new value
const select = wrapper.find('select');
const optionWrappers = wrapper.find('option');
select.simulate('change', {target: {value: optionWrappers.get(1).props.value}});
// make sure the event has been dispatched
expect(orderReducerSpy).to.have.been.calledWith({order: {}}, {
type: actionTypes.order.SET,
payload: options[1].value,
});
// make sure the saga has been triggered
expect(setOrderSagaSpy).to.have.been.calledWith({type: actionTypes.order.SET, payload: options[1].value});
// make sure the location has been updated
expect(store.getState().location.query).to.deep.equal({...options[1].value});
});
});