forked from gaearon/react-side-effect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
370 lines (289 loc) · 10.9 KB
/
index.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
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
const createClass = require('create-react-class');
const { expect } = require('chai');
const React = require('react');
const ExecutionEnvironment = require('exenv');
const jsdom = require('jsdom');
const { shallow, mount } = require('enzyme')
const { renderToStaticMarkup } = require('react-dom/server')
const { render } = require('react-dom')
const Enzyme = require('enzyme');
const EnzymeAdapter = require('enzyme-adapter-react-16');
Enzyme.configure({ adapter: new EnzymeAdapter() });
const withSideEffect = require('../src');
function noop() { }
const identity = x => x
describe('react-side-effect', () => {
describe('argument validation', () => {
it('should throw if no reducePropsState function is provided', () => {
expect(withSideEffect).to.throw('Expected reducePropsToState to be a function.');
});
it('should throw if no handleStateChangeOnClient function is provided', () => {
expect(withSideEffect.bind(null, noop)).to.throw('Expected handleStateChangeOnClient to be a function.');
});
it('should throw if mapStateOnServer is defined but not a function', () => {
expect(withSideEffect.bind(null, noop, noop, 'foo')).to.throw('Expected mapStateOnServer to either be undefined or a function.');
});
it('should throw if no WrappedComponent is provided', () => {
expect(withSideEffect(noop, noop)).to.throw('Expected WrappedComponent to be a React component');
});
});
describe('displayName', () => {
const withNoopSideEffect = withSideEffect(noop, noop);
it('should wrap the displayName of wrapped createClass component', () => {
const DummyComponent = createClass({ displayName: 'Dummy', render: noop });
const SideEffect = withNoopSideEffect(DummyComponent);
expect(SideEffect.displayName).to.equal('SideEffect(Dummy)');
});
it('should wrap the displayName of wrapped ES2015 class component', () => {
class DummyComponent extends React.Component {
static displayName = 'Dummy'
render() { }
}
const SideEffect = withNoopSideEffect(DummyComponent);
expect(SideEffect.displayName).to.equal('SideEffect(Dummy)');
});
it('should use the constructor name of the wrapped functional component', () => {
function DummyComponent() { }
const SideEffect = withNoopSideEffect(DummyComponent);
expect(SideEffect.displayName).to.equal('SideEffect(DummyComponent)');
});
it('should fallback to "Component"', () => {
const DummyComponent = createClass({ displayName: null, render: noop });
const SideEffect = withNoopSideEffect(DummyComponent);
expect(SideEffect.displayName).to.equal('SideEffect(Component)');
});
});
describe('SideEffect component', () => {
class DummyComponent extends React.Component {
render() {
return <div>hello {this.props.foo}</div>
}
};
const withIdentitySideEffect = withSideEffect(identity, noop);
let SideEffect;
beforeEach(() => {
SideEffect = withIdentitySideEffect(DummyComponent);
});
it('should expose the canUseDOM flag', () => {
expect(SideEffect).to.have.property('canUseDOM', ExecutionEnvironment.canUseDOM);
});
describe('rewind', () => {
it('should throw if used in the browser', () => {
SideEffect.canUseDOM = true;
expect(SideEffect.rewind).to.throw('You may only call rewind() on the server. Call peek() to read the current state.');
});
it('should return the current state', () => {
shallow(<SideEffect foo="bar" />);
const state = SideEffect.rewind();
expect(state).to.deep.equal([{ foo: 'bar' }]);
});
it('should reset the state', () => {
shallow(<SideEffect foo="bar" />);
SideEffect.rewind();
const state = SideEffect.rewind();
expect(state).to.equal(undefined);
});
});
describe('peek', () => {
it('should return the current state', () => {
shallow(<SideEffect foo="bar" />);
expect(SideEffect.peek()).to.deep.equal([{ foo: 'bar' }]);
});
it('should NOT reset the state', () => {
shallow(<SideEffect foo="bar" />);
SideEffect.peek();
const state = SideEffect.peek();
expect(state).to.deep.equal([{ foo: 'bar' }]);
});
});
describe('handleStateChangeOnClient', () => {
it('should execute handleStateChangeOnClient', () => {
let sideEffectCollectedData;
const handleStateChangeOnClient = state => (sideEffectCollectedData = state)
SideEffect = withSideEffect(identity, handleStateChangeOnClient)(DummyComponent);
SideEffect.canUseDOM = true;
shallow(<SideEffect foo="bar" />);
expect(sideEffectCollectedData).to.deep.equal([{ foo: 'bar' }]);
});
});
describe('mapStateOnServer', () => {
it('should apply a custom mapStateOnServer function', () => {
const mapStateOnServer = ([prop]) => prop
SideEffect = withSideEffect(identity, noop, mapStateOnServer)(DummyComponent);
SideEffect.canUseDOM = false;
shallow(<SideEffect foo="bar" />);
let state = SideEffect.rewind();
expect(state).not.to.be.an('Array');
expect(state).to.deep.equal({ foo: 'bar' });
SideEffect.canUseDOM = true;
shallow(<SideEffect foo="bar" />);
state = SideEffect.peek();
expect(state).to.an('Array');
expect(state).to.deep.equal([{ foo: 'bar' }]);
});
});
it('should collect props from all instances', () => {
shallow(<SideEffect foo="bar" />);
shallow(<SideEffect something="different" />);
const state = SideEffect.peek();
expect(state).to.deep.equal([{ foo: 'bar' }, { something: 'different' }]);
});
it('should render the wrapped component', () => {
const markup = renderToStaticMarkup(<SideEffect foo="bar" />);
expect(markup).to.equal('<div>hello bar</div>');
});
describe('with DOM', () => {
const originalWindow = global.window;
const originalDocument = global.document;
before(done => {
jsdom.env('<!doctype html><html><head></head><body></body></html>', (error, window) => {
if (!error) {
global.window = window;
global.document = window.document;
}
done(error);
});
});
after(() => {
global.window = originalWindow;
global.document = originalDocument;
});
it('should be findable by react TestUtils', () => {
class AnyComponent extends React.Component {
render() {
return <SideEffect foo="bar" />
}
}
const wrapper = shallow(<AnyComponent />);
const sideEffect = wrapper.find(SideEffect)
expect(sideEffect.props()).to.deep.equal({ foo: 'bar' });
});
it('should only recompute when component updates', () => {
let collectCount = 0;
function handleStateChangeOnClient(state) {
collectCount += 1;
}
SideEffect = withSideEffect(identity, handleStateChangeOnClient)(DummyComponent);
SideEffect.canUseDOM = true;
const node = document.createElement('div');
document.body.appendChild(node);
render(<SideEffect text="bar" />, node);
expect(collectCount).to.equal(1);
render(<SideEffect text="bar" />, node);
expect(collectCount).to.equal(1);
render(<SideEffect text="baz" />, node);
expect(collectCount).to.equal(2);
render(<SideEffect text="baz" />, node);
expect(collectCount).to.equal(2);
});
describe('nesting', () => {
let documentTitle = null;
let TestComponent = null;
let TestErrorComponent = null;
let mountNode = null;
before(() => {
function reducePropsToState(propsList) {
var innermostProps = propsList[propsList.length - 1];
if (innermostProps) {
return innermostProps.title;
}
}
function handleStateChangeOnClient(title) {
documentTitle = title || '';
}
TestComponent = withSideEffect(
reducePropsToState,
handleStateChangeOnClient
)(props => {
if (props.error) {
throw Error('TestErrorComponent');
} else {
return props.children || null;
}
});
TestComponent.canUseDOM = true;
mountNode = document.createElement('div');
document.body.appendChild(mountNode);
});
after(() => {
documentTitle = null;
document.body.removeChild(mountNode);
});
it('should support nested components', () => {
render(
<TestComponent title="outer">
<TestComponent title="inner" />
</TestComponent>,
mountNode,
);
expect(documentTitle).to.equal('inner');
});
it('should cleanup mounting instances after errors', () => {
class ErrorBoundary extends React.Component {
state = {};
componentDidCatch(error) {
error.suppressReactErrorLogging = true;
this.setState({ error })
}
render() {
if (this.state.error) {
return null;
} else {
return this.props.children || null;
}
}
}
render(
<TestComponent title="outer">
<TestComponent title="middle">
<ErrorBoundary>
<TestComponent title="inner" error="true" />
</ErrorBoundary>
</TestComponent>
</TestComponent>,
mountNode,
);
expect(documentTitle).to.equal('middle');
});
it('should mount in correct order', () => {
class DummyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
loaded: false
};
}
componentWillMount() {
this._mounted = true;
Promise.resolve().then(() => {
if (!this._mounted) {
return;
}
this.setState({
loaded: true
});
});
}
componentWillUnmount() {
this._mounted = false;
}
render() {
if (this.state.loaded) {
return <TestComponent title="inner" />;
} else {
return null;
}
}
}
render(
<TestComponent title="outer">
<DummyComponent />
</TestComponent>,
mountNode,
);
expect(documentTitle).to.equal('inner');
});
});
});
});
});