-
Notifications
You must be signed in to change notification settings - Fork 50
/
consistency.js
324 lines (264 loc) · 8.39 KB
/
consistency.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
import React, { Component } from 'react';
import createShallowRenderer from './helpers/createShallowRenderer';
import expect from 'expect';
import createProxy from '../src';
function createModernFixtures() {
class Bar extends Component {
componentWillUnmount() {
this.didUnmount = true;
}
doNothing() {
}
render() {
return <div>Bar</div>;
}
}
class Baz extends Component {
componentWillUnmount() {
this.didUnmount = true;
}
render() {
return <div>Baz</div>;
}
}
class Foo extends Component {
static displayName = 'Foo (Custom)';
componentWillUnmount() {
this.didUnmount = true;
}
render() {
return <div>Foo</div>;
}
}
class Anon extends React.Component {
constructor(props) {
super(props);
throw new Error('Oops.');
}
render() {
return <div>Anon</div>;
}
}
delete Anon.name;
return { Bar, Baz, Foo, Anon };
}
function createClassicFixtures() {
const Bar = React.createClass({
componentWillUnmount() {
this.didUnmount = true;
},
doNothing() {
},
render() {
return <div>Bar</div>;
}
});
const Baz = React.createClass({
componentWillUnmount() {
this.didUnmount = true;
},
render() {
return <div>Baz</div>;
}
});
const Foo = React.createClass({
displayName: 'Foo (Custom)',
componentWillUnmount() {
this.didUnmount = true;
},
render() {
return <div>Foo</div>;
}
});
const Anon = React.createClass({
getInitialState() {
throw new Error('Oops.');
},
render() {
return <div>Anon</div>;
}
});
delete Anon.displayName;
return { Bar, Baz, Foo, Anon };
}
describe('consistency', () => {
let renderer;
let warnSpy;
beforeEach(() => {
renderer = createShallowRenderer();
warnSpy = expect.spyOn(console, 'error').andCallThrough();
});
afterEach(() => {
warnSpy.destroy();
expect(warnSpy.calls.length).toBe(0);
});
function runCommonTests(createFixtures) {
let Bar, Baz, Foo, Anon;
beforeEach(() => {
({ Foo, Bar, Baz, Anon } = createFixtures());
});
it('does not overwrite the original class', () => {
const proxy = createProxy(Bar);
const Proxy = proxy.get();
const barInstance = renderer.render(<Proxy />);
expect(renderer.getRenderOutput().props.children).toEqual('Bar');
proxy.update(Baz);
const realBarInstance = renderer.render(<Bar />);
expect(renderer.getRenderOutput().props.children).toEqual('Bar');
expect(barInstance).toNotEqual(realBarInstance);
expect(barInstance.didUnmount).toEqual(true);
});
it('returns an existing proxy when wrapped twice', () => {
const proxy = createProxy(Bar);
const Proxy = proxy.get();
const proxyTwice = createProxy(Proxy);
expect(proxyTwice).toBe(proxy);
});
/*
* https://github.com/reactjs/react-redux/issues/163#issuecomment-192556637
*/
it('avoid false positives when statics are hoisted', () => {
const fooProxy = createProxy(Foo);
const FooProxy = fooProxy.get();
class Stuff extends Component {
render() {}
}
const KNOWN_STATICS = {
name: true,
length: true,
prototype: true,
caller: true,
arguments: true,
arity: true,
type: true
};
Object.getOwnPropertyNames(FooProxy).forEach(key => {
if (!KNOWN_STATICS[key]) {
Stuff[key] = FooProxy[key];
}
});
const stuffProxy = createProxy(Stuff);
expect(stuffProxy).toNotBe(fooProxy);
});
it('prevents recursive proxy cycle', () => {
const proxy = createProxy(Bar);
const Proxy = proxy.get();
proxy.update(Proxy);
expect(proxy.get()).toEqual(Proxy);
});
it('prevents mutually recursive proxy cycle', () => {
const barProxy = createProxy(Bar);
const BarProxy = barProxy.get();
const fooProxy = createProxy(Foo);
const FooProxy = fooProxy.get();
barProxy.update(FooProxy);
fooProxy.update(BarProxy);
});
it('sets up constructor to match the most recent type', () => {
let proxy = createProxy(Bar);
const BarProxy = proxy.get();
const barInstance = renderer.render(<BarProxy />);
expect(barInstance.constructor).toEqual(Bar);
expect(barInstance instanceof BarProxy).toEqual(true);
expect(barInstance instanceof Bar).toEqual(true);
proxy.update(Baz);
const BazProxy = proxy.get();
expect(BarProxy).toEqual(BazProxy);
expect(barInstance.constructor).toEqual(Baz);
expect(barInstance instanceof BazProxy).toEqual(true);
expect(barInstance instanceof Baz).toEqual(true);
});
it('sets up name and displayName from displayName or name', () => {
let proxy = createProxy(Bar);
const Proxy = proxy.get();
expect(Proxy.name).toEqual('Bar');
expect(Proxy.displayName).toEqual('Bar');
proxy.update(Baz);
expect(Proxy.name).toEqual('Baz');
expect(Proxy.displayName).toEqual('Baz');
proxy.update(Foo);
expect(Proxy.name).toEqual('Foo (Custom)');
expect(Proxy.displayName).toEqual('Foo (Custom)');
proxy.update(Anon);
expect(Proxy.name).toEqual('Unknown');
expect(Proxy.displayName).toEqual('Unknown');
});
it('keeps own methods on the prototype', () => {
let proxy = createProxy(Bar);
const Proxy = proxy.get();
const propertyNames = Object.getOwnPropertyNames(Proxy.prototype);
expect(propertyNames).toInclude('doNothing');
});
it('preserves method names', () => {
let proxy = createProxy(Bar);
const Proxy = proxy.get();
expect(Proxy.prototype.doNothing.name).toBe('doNothing');
});
it('preserves enumerability and writability of methods', () => {
let proxy = createProxy(Bar);
const Proxy = proxy.get();
['doNothing', 'render', 'componentDidMount', 'componentWillUnmount'].forEach(name => {
const originalDescriptor = Object.getOwnPropertyDescriptor(Bar.prototype, name);
const proxyDescriptor = Object.getOwnPropertyDescriptor(Proxy.prototype, name);
if (originalDescriptor) {
expect(proxyDescriptor.enumerable).toEqual(originalDescriptor.enumerable, name);
expect(proxyDescriptor.writable).toEqual(originalDescriptor.writable, name);
} else {
expect(proxyDescriptor.enumerable).toEqual(false, name);
expect(proxyDescriptor.writable).toEqual(true, name);
}
});
});
it('preserves toString() of methods', () => {
let proxy = createProxy(Bar);
const Proxy = proxy.get();
['doNothing', 'render', 'componentWillUnmount', 'constructor'].forEach(name => {
const originalMethod = Bar.prototype[name];
const proxyMethod = Proxy.prototype[name];
expect(originalMethod.toString()).toEqual(proxyMethod.toString());
});
const doNothingBeforeItWasDeleted = Proxy.prototype.doNothing;
proxy.update(Baz);
['render', 'componentWillUnmount', 'constructor'].forEach(name => {
const originalMethod = Baz.prototype[name];
const proxyMethod = Proxy.prototype[name];
expect(originalMethod.toString()).toEqual(proxyMethod.toString());
});
expect(doNothingBeforeItWasDeleted.toString()).toEqual('<method was deleted>');
});
it('does not swallow constructor errors', () => {
let proxy = createProxy(Anon);
const Proxy = proxy.get();
expect(() => renderer.render(<Proxy />)).toThrow('Oops');
});
}
describe('classic', () => {
runCommonTests(createClassicFixtures);
});
describe('modern', () => {
runCommonTests(createModernFixtures);
let Bar, Baz, Foo;
beforeEach(() => {
({ Bar, Baz, Foo } = createModernFixtures());
})
it('should not crash if new Function() throws', () => {
let oldFunction = global.Function;
global.Function = class extends oldFunction {
constructor () {
super();
throw new Error();
}
};
try {
expect(() => {
const proxy = createProxy(Bar);
const Proxy = proxy.get();
const barInstance = renderer.render(<Proxy />);
expect(barInstance.constructor).toEqual(Bar);
}).toNotThrow();
} finally {
global.Function = oldFunction;
}
});
});
});