-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
250 lines (190 loc) · 4.84 KB
/
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {h, render, renderToString, mount, Component} from 'ink';
import {createStore, combineReducers} from 'redux';
import stripAnsi from 'strip-ansi';
import {spy} from 'sinon';
import test from 'ava';
import {Provider, connect} from '.';
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT': return state + 1;
default: return state;
}
};
const reducer = combineReducers({
counter: counterReducer
});
const createTestStore = () => createStore(reducer);
test('provider', t => {
t.plan(1);
const store = {};
const Child = (props, context) => {
t.is(context.store, store);
return 'Test';
};
render((
<Provider store={store}>
<Child/>
</Provider>
));
});
test('pass props to child component', t => {
const store = createTestStore();
const Child = props => `Hello, ${props.name}`;
const ConnectedChild = connect()(Child);
const tree = render((
<Provider store={store}>
<ConnectedChild name="Chandler"/>
</Provider>
));
t.is(renderToString(tree), 'Hello, Chandler');
});
test.cb('subscribe to store and rerender on changes', t => {
const store = createTestStore();
const Child = props => `Iteration #${props.iteration}`;
const mapStateToProps = state => ({
iteration: state.counter
});
const ConnectedChild = connect(mapStateToProps)(Child);
const stream = {write: spy()};
const unmount = mount((
<Provider store={store}>
<ConnectedChild/>
</Provider>
), stream);
t.is(stripAnsi(stream.write.firstCall.args[0]), 'Iteration #0\n');
store.dispatch({type: 'INCREMENT'});
process.nextTick(() => {
unmount();
t.is(stripAnsi(stream.write.secondCall.args[0]), 'Iteration #1\n');
t.end();
});
});
test('pass store.dispatch() to child component', t => {
const store = createTestStore();
class Child extends Component {
render() {
return 'Test';
}
componentDidMount() {
this.props.dispatch({type: 'INCREMENT'});
}
}
const ConnectedChild = connect()(Child);
render((
<Provider store={store}>
<ConnectedChild/>
</Provider>
));
t.deepEqual(store.getState(), {counter: 1});
});
test('bind dispatch props', t => {
const store = createTestStore();
class Child extends Component {
render() {
return 'Test';
}
componentDidMount() {
this.props.onIncrement();
}
}
const mapDispatchToProps = {
onIncrement: () => ({type: 'INCREMENT'})
};
const ConnectedChild = connect(null, mapDispatchToProps)(Child);
render((
<Provider store={store}>
<ConnectedChild/>
</Provider>
));
t.deepEqual(store.getState(), {counter: 1});
});
test('pass custom dispatch props', t => {
const store = createTestStore();
spy(store, 'dispatch');
class Child extends Component {
render() {
return 'Test';
}
componentDidMount() {
this.props.onIncrement();
}
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
onIncrement: () => {
dispatch({
type: 'INCREMENT',
message: ownProps.message
});
}
};
};
const ConnectedChild = connect(null, mapDispatchToProps)(Child);
render((
<Provider store={store}>
<ConnectedChild message="Hello"/>
</Provider>
));
t.deepEqual(store.getState(), {counter: 1});
t.deepEqual(store.dispatch.firstCall.args[0], {
type: 'INCREMENT',
message: 'Hello'
});
});
test('overwrite own props', t => {
const store = createTestStore();
class Child extends Component {
render(props) {
return `Iteration #${props.counter}`;
}
componentDidMount() {
this.props.onIncrement();
}
}
const mapStateToProps = state => ({
counter: state.counter
});
const mapDispatchToProps = {
onIncrement: () => ({type: 'INCREMENT'})
};
const ConnectedChild = connect(mapStateToProps, mapDispatchToProps)(Child);
const fakeOnIncrement = spy();
const tree = render((
<Provider store={store}>
<ConnectedChild counter={10} onIncrement={fakeOnIncrement}/>
</Provider>
));
t.is(renderToString(tree), 'Iteration #0');
t.false(fakeOnIncrement.called);
t.deepEqual(store.getState(), {counter: 1});
});
test('custom merge function', t => {
const store = createTestStore();
class Child extends Component {
render(props) {
return `Iteration #${props.counter}`;
}
componentDidMount() {
this.props.onIncrement();
}
}
const mapStateToProps = state => ({
counter: state.counter
});
const mapDispatchToProps = {
onIncrement: () => ({type: 'INCREMENT'})
};
const mergeProps = (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, stateProps, dispatchProps, ownProps);
};
const ConnectedChild = connect(mapStateToProps, mapDispatchToProps, mergeProps)(Child);
const fakeOnIncrement = spy();
const tree = render((
<Provider store={store}>
<ConnectedChild counter={10} onIncrement={fakeOnIncrement}/>
</Provider>
));
t.is(renderToString(tree), 'Iteration #10');
t.true(fakeOnIncrement.calledOnce);
t.deepEqual(store.getState(), {counter: 0});
});