-
Notifications
You must be signed in to change notification settings - Fork 1
/
configureStore.test.js
205 lines (167 loc) 路 5.18 KB
/
configureStore.test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// @flow
import React from "react";
import {mount} from "enzyme";
import {compose, bindActionCreators} from "redux";
import {connect} from "react-redux";
import {put, takeLatest} from "redux-saga/effects";
import {configureStore, injectReducer, injectSaga} from ".";
const {describe, beforeAll, it, expect} = global;
const getWrappedSagaComponent = (prefix, isSetting = true) => {
// constants
const NAME_SEND = `${prefix}/NAME_SEND`;
const NAME_SET = `${prefix}/NAME_SET`;
// actions
const nameSendAction = name => ({
type: NAME_SEND,
payload: name,
});
const nameSetAction = name => ({
type: NAME_SET,
payload: name,
});
// reducers
const reducer = (state = "no-name", {type, payload}) => {
switch (type) {
case NAME_SET:
return payload;
default:
return state;
}
};
// sagas
function* nameSetSaga(action) {
const name = action.payload;
yield put(nameSetAction(name));
}
function* saga() {
// $FlowFixMe
yield takeLatest(NAME_SEND, nameSetSaga);
}
const withReducer = injectReducer({key: `${prefix}/testReducer`, reducer});
const withSaga = injectSaga({key: `${prefix}/testSaga`, saga});
const withConnect = connect(
state => ({name: state.get(`${prefix}/testReducer`)}), // mapStateToProps
dispatch => bindActionCreators({nameSend: nameSendAction}, dispatch), // mapDispatchToProps
);
type Props = {
name: string,
nameSend: string => void,
};
class Component extends React.Component<Props, any> {
componentDidMount() {
if (isSetting) {
this.props.nameSend(`Name ${prefix}`);
}
}
render() {
return <span>{this.props.name}</span>;
}
}
// prettier-ignore
return compose(
withReducer,
withSaga,
withConnect,
)(Component);
};
const getWrappedThunkComponent = prefix => {
// constants
const NAME_SET = `${prefix}/NAME_SET`;
const nameSetAction = name => ({
type: NAME_SET,
payload: name,
});
// reducers
const reducer = (state = "no-name", {type, payload}) => {
switch (type) {
case NAME_SET:
return payload;
default:
return state;
}
};
// thunks
const nameSendThunk = name => dispatch => dispatch(nameSetAction(name));
const withReducer = injectReducer({key: `${prefix}/testReducer`, reducer});
const withConnect = connect(
state => ({name: state.get(`${prefix}/testReducer`)}), // mapStateToProps
dispatch => bindActionCreators({nameSend: nameSendThunk}, dispatch), // mapDispatchToProps
);
type Props = {
name: string,
nameSend: string => void,
};
class Component extends React.Component<Props, any> {
componentDidMount() {
this.props.nameSend(`Name ${prefix}`);
}
render() {
return <span>{this.props.name}</span>;
}
}
// prettier-ignore
return compose(
withReducer,
withConnect,
)(Component);
};
describe("configureStore", () => {
it("default store, without parameters", () => {
const store = configureStore();
expect(store.getState()).toMatchSnapshot();
});
it("store with reducers parameter", () => {
function staticReducer(state = null) {
return state;
}
const reducers = {config: staticReducer};
const store = configureStore(reducers);
expect(store.getState()).toMatchSnapshot();
});
it("store with reducers and initialState parameter", () => {
function staticReducer(state = null) {
return state;
}
const reducers = {config: staticReducer};
const initialState = {config: true};
const store = configureStore(reducers, initialState);
expect(store.getState()).toMatchSnapshot();
});
});
describe("configureStore and inject reducers, sagas and thunks", () => {
let store;
beforeAll(() => {
store = configureStore();
});
it("default store", () => {
expect(store.getState()).toMatchSnapshot();
});
it("component A, with reducer and saga", () => {
const WrappedComponent = getWrappedSagaComponent("A");
expect(store.getState()).toMatchSnapshot();
const component = mount(<WrappedComponent />, {context: {store}});
expect(component).toMatchSnapshot();
expect(store.getState()).toMatchSnapshot();
});
it("component B, with reducer and saga", () => {
const WrappedComponent = getWrappedSagaComponent("B");
expect(store.getState()).toMatchSnapshot();
const component = mount(<WrappedComponent />, {context: {store}});
expect(component).toMatchSnapshot();
expect(store.getState()).toMatchSnapshot();
});
it("component C, with reducer and using thunk", () => {
const WrappedComponent = getWrappedThunkComponent("C");
expect(store.getState()).toMatchSnapshot();
const component = mount(<WrappedComponent />, {context: {store}});
expect(component).toMatchSnapshot();
expect(store.getState()).toMatchSnapshot();
});
it("component A2, with reducer and saga should not override current state", () => {
const WrappedComponent = getWrappedSagaComponent("A", false);
expect(store.getState()).toMatchSnapshot();
const component = mount(<WrappedComponent />, {context: {store}});
expect(component).toMatchSnapshot();
expect(store.getState()).toMatchSnapshot();
});
});