11
12
"use strict";
13
14
+// NOTE: We're explicitly not using JSX in this file. This is intended to test
15
+// classic JS without JSX.
16
+
17
var mocks;
18
19
var React;
17
-var ReactElement;
20
var ReactTestUtils;
21
22
describe('ReactElement', function() {
28
mocks = require('mocks');
29
30
React = require('React');
29
- ReactElement = require('ReactElement');
31
ReactTestUtils = require('ReactTestUtils');
32
ComponentClass = React.createClass({
32
- render: function() { return <div />; }
33
+ render: function() { return React.createElement('div'); }
34
});
35
});
36
103
}
104
});
105
105
- ReactTestUtils.renderIntoDocument(<Wrapper />);
106
+ ReactTestUtils.renderIntoDocument(
107
+ React.createElement(Wrapper)
108
+ );
109
110
expect(element._context).toEqual({ foo: 'bar' });
111
});
121
}
122
});
123
121
- var instance = ReactTestUtils.renderIntoDocument(<Wrapper />);
124
+ var instance = ReactTestUtils.renderIntoDocument(
125
+ React.createElement(Wrapper)
126
+ );
127
128
expect(element._owner.getPublicInstance()).toBe(instance);
129
});
164
expect(console.warn.argsForCall.length).toBe(0);
165
});
166
162
- it('warns for keys for arrays of elements in rest args', function() {
163
- spyOn(console, 'warn');
164
- var Component = React.createFactory(ComponentClass);
165
-
166
- Component(null, [ Component(), Component() ]);
167
-
168
- expect(console.warn.argsForCall.length).toBe(1);
169
- expect(console.warn.argsForCall[0][0]).toContain(
170
- 'Each child in an array or iterator should have a unique "key" prop.'
171
- );
172
- });
173
-
174
- it('warns for keys for arrays of elements in rest args', function() {
175
- spyOn(console, 'warn');
176
- var Component = React.createFactory(ComponentClass);
177
-
178
- var InnerClass = React.createClass({
179
- displayName: 'InnerClass',
180
- render: function() {
181
- return Component(null, this.props.childSet);
182
- }
183
- });
184
-
185
- var InnerComponent = React.createFactory(InnerClass);
186
-
187
- var ComponentWrapper = React.createClass({
188
- displayName: 'ComponentWrapper',
189
- render: function() {
190
- return InnerComponent({ childSet: [ Component(), Component() ] });
191
- }
192
- });
193
-
194
- ReactTestUtils.renderIntoDocument(<ComponentWrapper />);
195
-
196
- expect(console.warn.argsForCall.length).toBe(1);
197
- expect(console.warn.argsForCall[0][0]).toContain(
198
- 'Each child in an array or iterator should have a unique "key" prop. ' +
199
- 'Check the render method of InnerClass. ' +
200
- 'It was passed a child from ComponentWrapper. '
201
- );
202
- });
203
-
204
- it('warns for keys for iterables of elements in rest args', function() {
205
- spyOn(console, 'warn');
206
- var Component = React.createFactory(ComponentClass);
207
-
208
- var iterable = {
209
- '@@iterator': function() {
210
- var i = 0;
211
- return {
212
- next: function() {
213
- var done = ++i > 2;
214
- return { value: done ? undefined : Component(), done: done };
215
- }
216
- };
217
- }
218
- };
219
-
220
- Component(null, iterable);
221
-
222
- expect(console.warn.argsForCall.length).toBe(1);
223
- expect(console.warn.argsForCall[0][0]).toContain(
224
- 'Each child in an array or iterator should have a unique "key" prop.'
225
- );
226
- });
227
-
228
- it('does not warns for arrays of elements with keys', function() {
229
- spyOn(console, 'warn');
230
- var Component = React.createFactory(ComponentClass);
231
-
232
- Component(null, [ Component({key: '#1'}), Component({key: '#2'}) ]);
233
-
234
- expect(console.warn.argsForCall.length).toBe(0);
235
- });
236
-
237
- it('does not warns for iterable elements with keys', function() {
238
- spyOn(console, 'warn');
239
- var Component = React.createFactory(ComponentClass);
240
-
241
- var iterable = {
242
- '@@iterator': function() {
243
- var i = 0;
244
- return {
245
- next: function() {
246
- var done = ++i > 2;
247
- return {
248
- value: done ? undefined : Component({key: '#' + i}),
249
- done: done
250
- };
251
- }
252
- };
253
- }
254
- };
255
-
256
- Component(null, iterable);
257
-
258
- expect(console.warn.argsForCall.length).toBe(0);
259
- });
260
-
261
- it('warns for numeric keys on objects in rest args', function() {
262
- spyOn(console, 'warn');
263
- var Component = React.createFactory(ComponentClass);
264
-
265
- Component(null, { 1: Component(), 2: Component() });
266
-
267
- expect(console.warn.argsForCall.length).toBe(1);
268
- expect(console.warn.argsForCall[0][0]).toContain(
269
- 'Child objects should have non-numeric keys so ordering is preserved.'
270
- );
271
- });
272
-
273
- it('does not warn for numeric keys in entry iterables in rest args', function() {
274
- spyOn(console, 'warn');
275
- var Component = React.createFactory(ComponentClass);
276
-
277
- var iterable = {
278
- '@@iterator': function() {
279
- var i = 0;
280
- return {
281
- next: function() {
282
- var done = ++i > 2;
283
- return { value: done ? undefined : [i, Component()], done: done };
284
- }
285
- };
286
- }
287
- };
288
- iterable.entries = iterable['@@iterator'];
289
-
290
- Component(null, iterable);
291
-
292
- expect(console.warn.argsForCall.length).toBe(0);
293
- });
294
-
295
- it('does not warn when the element is directly in rest args', function() {
296
- spyOn(console, 'warn');
297
- var Component = React.createFactory(ComponentClass);
298
-
299
- Component(null, Component(), Component());
300
-
301
- expect(console.warn.argsForCall.length).toBe(0);
302
- });
303
-
304
- it('does not warn when the array contains a non-element', function() {
305
- spyOn(console, 'warn');
306
- var Component = React.createFactory(ComponentClass);
307
-
308
- Component(null, [ {}, {} ]);
309
-
310
- expect(console.warn.argsForCall.length).toBe(0);
311
- });
312
-
167
it('allows static methods to be called using the type property', function() {
168
spyOn(console, 'warn');
169
177
return {valueToReturn: 'hi'};
178
},
179
render: function() {
326
- return <div></div>;
180
+ return React.createElement('div');
181
}
182
});
183
330
- var element = <ComponentClass />;
184
+ var element = React.createElement(ComponentClass);
185
expect(element.type.someStaticMethod()).toBe('someReturnValue');
186
expect(console.warn.argsForCall.length).toBe(0);
187
});
189
it('identifies valid elements', function() {
190
var Component = React.createClass({
191
render: function() {
338
- return <div />;
192
+ return React.createElement('div');
193
}
194
});
195
342
- expect(ReactElement.isValidElement(<div />)).toEqual(true);
343
- expect(ReactElement.isValidElement(<Component />)).toEqual(true);
344
-
345
- expect(ReactElement.isValidElement(null)).toEqual(false);
346
- expect(ReactElement.isValidElement(true)).toEqual(false);
347
- expect(ReactElement.isValidElement({})).toEqual(false);
348
- expect(ReactElement.isValidElement("string")).toEqual(false);
349
- expect(ReactElement.isValidElement(React.DOM.div)).toEqual(false);
350
- expect(ReactElement.isValidElement(Component)).toEqual(false);
196
+ expect(React.isValidElement(React.createElement('div')))
197
+ .toEqual(true);
198
+ expect(React.isValidElement(React.createElement(Component)))
199
+ .toEqual(true);
200
+
201
+ expect(React.isValidElement(null)).toEqual(false);
202
+ expect(React.isValidElement(true)).toEqual(false);
203
+ expect(React.isValidElement({})).toEqual(false);
204
+ expect(React.isValidElement("string")).toEqual(false);
205
+ expect(React.isValidElement(React.DOM.div)).toEqual(false);
206
+ expect(React.isValidElement(Component)).toEqual(false);
207
});
208
209
it('allows the use of PropTypes validators in statics', function() {
210
+ // TODO: This test was added to cover a special case where we proxied
211
+ // methods. However, we don't do that any more so this test can probably
212
+ // be removed. Leaving it in classic as a safety precausion.
213
var Component = React.createClass({
214
render: () => null,
215
statics: {
221
expect(typeof Component.specialType.isRequired).toBe("function");
222
});
223
365
- it('allows a DOM element to be used with a string', function() {
366
- var element = React.createElement('div', { className: 'foo' });
367
- var instance = ReactTestUtils.renderIntoDocument(element);
368
- expect(instance.getDOMNode().tagName).toBe('DIV');
369
- });
370
-
224
it('is indistinguishable from a plain object', function() {
225
var element = React.createElement('div', { className: 'foo' });
226
var object = {};
233
return {fruit: 'persimmon'};
234
},
235
render: function() {
383
- return <span />;
236
+ return React.createElement('span');
237
}
238
});
239
240
var container = document.createElement('div');
241
var instance = React.render(
389
- <Component fruit="mango" />,
242
+ React.createElement(Component, { fruit: 'mango' }),
243
container
244
);
245
expect(instance.props.fruit).toBe('mango');
246
394
- React.render(<Component />, container);
247
+ React.render(React.createElement(Component), container);
248
expect(instance.props.fruit).toBe('persimmon');
249
});
250
251
it('should normalize props with default values', function() {
399
- var warn = console.warn;
400
- console.warn = mocks.getMockFunction();
401
-
252
var Component = React.createClass({
403
- propTypes: {prop: React.PropTypes.string.isRequired},
253
getDefaultProps: function() {
254
return {prop: 'testKey'};
255
},
407
- getInitialState: function() {
408
- return {prop: this.props.prop + 'State'};
409
- },
256
render: function() {
411
- return <span>{this.props.prop}</span>;
257
+ return React.createElement('span', null, this.props.prop);
258
}
259
});
260
415
- var instance = ReactTestUtils.renderIntoDocument(<Component />);
261
+ var instance = ReactTestUtils.renderIntoDocument(
262
+ React.createElement(Component)
263
+ );
264
expect(instance.props.prop).toBe('testKey');
417
- expect(instance.state.prop).toBe('testKeyState');
265
419
- ReactTestUtils.renderIntoDocument(<Component prop={null} />);
420
-
421
- expect(console.warn.mock.calls.length).toBe(1);
422
- expect(console.warn.mock.calls[0][0]).toBe(
423
- 'Warning: Required prop `prop` was not specified in `Component`.'
266
+ var inst2 = ReactTestUtils.renderIntoDocument(
267
+ React.createElement(Component, { prop: null })
268
);
425
-
426
- console.warn = warn;
269
+ expect(inst2.props.prop).toBe(null);
270
});
271
272
});