-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcompose.js
382 lines (312 loc) · 11.6 KB
/
compose.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* eslint react/prefer-stateless-function: 0, react/prop-types: 0 */
import React from 'react';
import { shallow, mount } from 'enzyme';
import { expect } from 'chai';
import compose from '../compose';
const { describe, it } = global;
class Comp extends React.Component {
render() {
return (<p>{this.props.name}</p>);
}
}
describe('compose', () => {
describe('basic features', () => {
it('should pass props to the Child', () => {
const Container = compose((props, onData) => {
onData(null, {});
})(Comp);
const el = shallow(<Container name="arunoda" />);
expect(el.html()).to.match(/arunoda/);
});
it('should pass data to the Child', () => {
const Container = compose((props, onData) => {
onData(null, { name: 'arunoda' });
})(Comp);
const el = shallow(<Container />);
expect(el.html()).to.match(/arunoda/);
});
it('should pass both data and props to the Child', () => {
const Container = compose((props, onData) => {
onData(null, { name: 'arunoda' });
})(({ name, age }) => (<p>{name}={age}</p>));
const el = shallow(<Container age={20} />);
expect(el.html()).to.match(/arunoda=20/);
});
it('should run with the env', (done) => {
const env = { name: 'arunoda' };
const options = { env };
const Container = compose((props, onData, context) => {
expect(context.name).to.be.equal('arunoda');
done();
}, options)(Comp);
shallow(<Container />);
});
it('should show the given loading handler when there is no data', () => {
const options = {
loadingHandler: () => (<p>loading</p>),
};
const Container = compose((props, onData) => {
onData();
}, options)(Comp);
const el = shallow(<Container />);
expect(el.html()).to.match(/loading/);
});
it('should show the given error handler when there is an error', () => {
const options = {
errorHandler: e => (<p>{e.message}</p>),
};
const Container = compose((props, onData) => {
onData(new Error('Aiyo'));
}, options)(Comp);
const el = shallow(<Container />);
expect(el.html()).to.match(/Aiyo/);
});
it('should set the child ref', () => {
const Container = compose((props, onData) => {
onData(null, { name: 'arunoda' });
})(Comp);
const el = mount(<Container name="arunoda" />);
expect(el.instance().child.props.name).to.be.equal('arunoda');
});
});
describe('dataLoader features', () => {
it('should allow to pass data multiple times', () => {
let onData;
const Container = compose((props, _onData) => {
onData = _onData;
})(Comp);
const el = mount(<Container />);
// First run
onData(null, { name: 'arunoda' });
expect(el.instance().state.data.name).to.be.equal('arunoda');
// Second run
onData(null, { name: 'kamal' });
expect(el.instance().state.data.name).to.be.equal('kamal');
});
it('should unsubscribe when unmounted', (done) => {
const Container = compose((props, onData) => {
onData(null, {});
return done;
})(Comp);
const el = mount(<Container name="arunoda" />);
el.instance().componentWillUnmount();
});
it('should unsubscribe when subscribing again', (done) => {
let onData;
const Container = compose((props, _onData) => {
onData = _onData;
onData(null, {});
return done;
})(Comp);
const el = mount(<Container name="arunoda" />);
el.instance()._subscribe({ aa: 10 });
});
it('should throw an error when sending data when unmounted', () => {
let onData;
const Container = compose((props, _onData) => {
onData = _onData;
onData(null, {});
})(Comp);
const el = mount(<Container name="arunoda" />);
el.instance().componentWillUnmount();
const run = () => onData(null, { aa: 10 });
expect(run).to.throw(/Tyring set data after/);
});
});
describe('performance', () => {
describe('with propsToWatch === []', () => {
describe('dataLoader', () => {
it('should run for the first time - kkrgr', () => {
const options = {
propsToWatch: [],
};
const Container = compose((props, onData) => {
onData(null, { name: 'arunoda' });
}, options)(Comp);
const el = shallow(<Container />);
expect(el.html()).to.match(/arunoda/);
});
it('should not run again', () => {
const options = {
propsToWatch: [],
};
let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);
const el = mount(<Container />);
el.instance()._subscribe({ aa: 10 });
expect(callCount).to.be.equal(1);
});
});
});
describe('with propsToWatch == [some props]', () => {
describe('dataLoader', () => {
it('should not run if the watching props are the same', () => {
const options = {
propsToWatch: ['name'],
};
let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);
const el = mount(<Container name="arunoda" />);
el.instance()._subscribe({ name: 'arunoda', age: 20 });
expect(callCount).to.be.equal(1);
});
it('should not run if the watching props changed', () => {
const options = {
propsToWatch: ['name'],
};
let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);
const el = mount(<Container name="arunoda" />);
el.instance()._subscribe({ name: 'kamal', age: 20 });
expect(callCount).to.be.equal(2);
});
it('should do a shallow comparison', () => {
const options = {
propsToWatch: ['data'],
};
const data = {};
let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);
const el = mount(<Container data={data} />);
// let's change the stuff inside the data
data.foo = 100;
el.instance()._subscribe({ data });
expect(callCount).to.be.equal(1);
});
it('should watch multiple props', () => {
const options = {
propsToWatch: ['name', 'age'],
};
let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);
const el = mount(<Container name="arunoda" age={20} />);
// first run with same props
el.instance()._subscribe({ name: 'arunoda', age: 20, kkr: 20 });
expect(callCount).to.be.equal(1);
// second run with changed props
el.instance()._subscribe({ name: 'arunoda', age: 30 });
expect(callCount).to.be.equal(2);
});
});
});
describe('with shouldSubscribe', () => {
describe('dataLoader', () => {
it('should run for the first time even shouldSubscribe give false', () => {
const options = {
shouldSubscribe: () => false,
};
const Container = compose((props, onData) => {
onData(null, { name: 'arunoda' });
}, options)(Comp);
const el = shallow(<Container />);
expect(el.html()).to.match(/arunoda/);
});
it('should ignore propsToWatch', () => {
const options = {
shouldSubscribe: () => true,
propsToWatch: [],
};
let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);
const el = mount(<Container />);
el.instance()._subscribe({ aa: 10 });
expect(callCount).to.be.equal(2);
});
});
});
describe('pure', () => {
describe('default', () => {
it('should run not be pure', () => {
const Container = compose(() => null)(Comp);
const i = shallow(<Container />).instance();
expect(i.shouldComponentUpdate()).to.be.equal(true);
});
});
describe('with shouldUpdate', () => {
it('should run should update', () => {
const nextProps = { aa: 20 };
const options = {
shouldUpdate(cp, np) {
expect(cp).to.deep.equal({ aa: 10 });
expect(np).to.deep.equal(nextProps);
return false;
},
};
const Container = compose(() => null, options)(Comp);
const i = shallow(<Container aa={10} />).instance();
expect(i.shouldComponentUpdate(nextProps)).to.be.equal(false);
});
});
describe('with props', () => {
it('should be true if props are different', () => {
const Container = compose(() => null, { pure: true })(Comp);
const i = shallow(<Container p1="10" />).instance();
expect(i.shouldComponentUpdate({ p1: '11' }, {})).to.be.equal(true);
i.props = { p1: '11' };
expect(i.shouldComponentUpdate({ p1: '11', p2: 10 }, {}))
.to.be.equal(true);
});
it('should be false if props are the same', () => {
const Container = compose(() => null, { pure: true })(Comp);
const i = shallow(<Container p1="10" />).instance();
expect(i.shouldComponentUpdate({ p1: '10' }, {}, {})).to.be.equal(false);
});
});
describe('with error', () => {
it('should be true if errors are different', () => {
const Container = compose(() => null, { pure: true })(Comp);
const i = shallow(<Container />).instance();
const error = new Error('hello');
expect(i.shouldComponentUpdate(i.props, { error })).to.be.equal(true);
i.state = { error };
const newError = new Error('hello');
expect(i.shouldComponentUpdate(i.props, { error: newError }))
.to.be.equal(true);
});
it('should be false if errors are the same', () => {
const Container = compose(() => null, { pure: true })(Comp);
const i = shallow(<Container />).instance();
const error = new Error('hello');
expect(i.shouldComponentUpdate(i.props, { error })).to.be.equal(true);
i.state = { error };
expect(i.shouldComponentUpdate(i.props, { error }, {}))
.to.be.equal(false);
});
});
describe('with data', () => {
it('should be true if data are different', () => {
const Container = compose(() => null, { pure: true })(Comp);
const i = shallow(<Container />).instance();
const data = { aa: 10 };
expect(i.shouldComponentUpdate(i.props, { data })).to.be.equal(true);
i.state = { data };
const sameData = { aa: 10 };
expect(i.shouldComponentUpdate(i.props, { data: sameData }, {}))
.to.be.equal(false);
});
it('should be false if data are the same', () => {
const Container = compose(() => null, { pure: true })(Comp);
const i = shallow(<Container />).instance();
const data = { aa: 10 };
expect(i.shouldComponentUpdate(i.props, { data })).to.be.equal(true);
i.state = { data };
const sameData = { aa: 10 };
expect(i.shouldComponentUpdate(i.props, { data: sameData }, {}))
.to.be.equal(false);
});
});
});
});
});