-
Notifications
You must be signed in to change notification settings - Fork 45.7k
/
ReactJSXElement-test.js
203 lines (174 loc) · 5.9 KB
/
ReactJSXElement-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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let React;
let ReactDOM;
let ReactTestUtils;
describe('ReactJSXElement', () => {
let Component;
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
Component = class extends React.Component {
render() {
return <div />;
}
};
});
it('returns a complete element according to spec', () => {
const element = <Component />;
expect(element.type).toBe(Component);
expect(element.key).toBe(null);
expect(element.ref).toBe(null);
const expectation = {};
Object.freeze(expectation);
expect(element.props).toEqual(expectation);
});
it('allows a lower-case to be passed as the string type', () => {
const element = <div />;
expect(element.type).toBe('div');
expect(element.key).toBe(null);
expect(element.ref).toBe(null);
const expectation = {};
Object.freeze(expectation);
expect(element.props).toEqual(expectation);
});
it('allows a string to be passed as the type', () => {
const TagName = 'div';
const element = <TagName />;
expect(element.type).toBe('div');
expect(element.key).toBe(null);
expect(element.ref).toBe(null);
const expectation = {};
Object.freeze(expectation);
expect(element.props).toEqual(expectation);
});
it('returns an immutable element', () => {
const element = <Component />;
if (__DEV__) {
expect(() => (element.type = 'div')).toThrow();
} else {
expect(() => (element.type = 'div')).not.toThrow();
}
});
it('does not reuse the object that is spread into props', () => {
const config = {foo: 1};
const element = <Component {...config} />;
expect(element.props.foo).toBe(1);
config.foo = 2;
expect(element.props.foo).toBe(1);
});
it('extracts key and ref from the rest of the props', () => {
const element = <Component key="12" ref="34" foo="56" />;
expect(element.type).toBe(Component);
expect(element.key).toBe('12');
expect(element.ref).toBe('34');
const expectation = {foo: '56'};
Object.freeze(expectation);
expect(element.props).toEqual(expectation);
});
it('coerces the key to a string', () => {
const element = <Component key={12} foo="56" />;
expect(element.type).toBe(Component);
expect(element.key).toBe('12');
expect(element.ref).toBe(null);
const expectation = {foo: '56'};
Object.freeze(expectation);
expect(element.props).toEqual(expectation);
});
it('merges JSX children onto the children prop', () => {
const a = 1;
const element = <Component children="text">{a}</Component>;
expect(element.props.children).toBe(a);
});
it('does not override children if no JSX children are provided', () => {
const element = <Component children="text" />;
expect(element.props.children).toBe('text');
});
it('overrides children if null is provided as a JSX child', () => {
const element = <Component children="text">{null}</Component>;
expect(element.props.children).toBe(null);
});
it('overrides children if undefined is provided as an argument', () => {
const element = <Component children="text">{undefined}</Component>;
expect(element.props.children).toBe(undefined);
const element2 = React.cloneElement(
<Component children="text" />,
{},
undefined,
);
expect(element2.props.children).toBe(undefined);
});
it('merges JSX children onto the children prop in an array', () => {
const a = 1;
const b = 2;
const c = 3;
const element = (
<Component>
{a}
{b}
{c}
</Component>
);
expect(element.props.children).toEqual([1, 2, 3]);
});
it('allows static methods to be called using the type property', () => {
class StaticMethodComponent {
static someStaticMethod() {
return 'someReturnValue';
}
render() {
return <div />;
}
}
const element = <StaticMethodComponent />;
expect(element.type.someStaticMethod()).toBe('someReturnValue');
});
it('identifies valid elements', () => {
expect(React.isValidElement(<div />)).toEqual(true);
expect(React.isValidElement(<Component />)).toEqual(true);
expect(React.isValidElement(null)).toEqual(false);
expect(React.isValidElement(true)).toEqual(false);
expect(React.isValidElement({})).toEqual(false);
expect(React.isValidElement('string')).toEqual(false);
expect(React.isValidElement(Component)).toEqual(false);
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
});
it('is indistinguishable from a plain object', () => {
const element = <div className="foo" />;
const object = {};
expect(element.constructor).toBe(object.constructor);
});
it('should use default prop value when removing a prop', () => {
Component.defaultProps = {fruit: 'persimmon'};
const container = document.createElement('div');
const instance = ReactDOM.render(<Component fruit="mango" />, container);
expect(instance.props.fruit).toBe('mango');
ReactDOM.render(<Component />, container);
expect(instance.props.fruit).toBe('persimmon');
});
it('should normalize props with default values', () => {
class NormalizingComponent extends React.Component {
render() {
return <span>{this.props.prop}</span>;
}
}
NormalizingComponent.defaultProps = {prop: 'testKey'};
const instance = ReactTestUtils.renderIntoDocument(
<NormalizingComponent />,
);
expect(instance.props.prop).toBe('testKey');
const inst2 = ReactTestUtils.renderIntoDocument(
<NormalizingComponent prop={null} />,
);
expect(inst2.props.prop).toBe(null);
});
});