/
react-dom-tests.tsx
220 lines (184 loc) · 8.43 KB
/
react-dom-tests.tsx
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
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as ReactDOMServer from 'react-dom/server';
import * as ReactDOMNodeStream from 'react-dom/node-stream';
import * as ReactTestUtils from 'react-dom/test-utils';
declare function describe(desc: string, f: () => void): void;
declare function it(desc: string, f: () => void): void;
class TestComponent extends React.Component<{x: string}> { }
describe('ReactDOM', () => {
it('render', () => {
const rootElement = document.createElement('div');
ReactDOM.render(React.createElement('div'), rootElement);
});
it('hydrate', () => {
const rootElement = document.createElement('div');
ReactDOM.hydrate(React.createElement('div'), rootElement);
});
it('unmounts', () => {
const rootElement = document.createElement('div');
ReactDOM.render(React.createElement('div'), rootElement);
ReactDOM.unmountComponentAtNode(rootElement);
});
it('works with document fragments', () => {
const fragment = document.createDocumentFragment();
ReactDOM.render(React.createElement('div'), fragment);
ReactDOM.unmountComponentAtNode(fragment);
});
it('find dom node', () => {
const rootElement = document.createElement('div');
ReactDOM.render(React.createElement('div'), rootElement);
ReactDOM.findDOMNode(rootElement);
ReactDOM.findDOMNode(null);
ReactDOM.findDOMNode(undefined);
});
it('createPortal', () => {
const rootElement = document.createElement('div');
const portalTarget = document.createElement('div');
class ClassComponent extends React.Component {
render() {
return ReactDOM.createPortal(<div />, portalTarget);
}
}
ReactDOM.createPortal(<div />, document.createElement('div'));
ReactDOM.createPortal(<div />, document.createElement('div'), null);
ReactDOM.createPortal(<div />, document.createElement('div'), 'key');
ReactDOM.createPortal(React.createElement('div'), document.createElement('div'));
ReactDOM.createPortal(React.createElement('div'), document.createElement('div'), null);
ReactDOM.createPortal(React.createElement('div'), document.createElement('div'), 'key');
ReactDOM.render(<ClassComponent />, rootElement);
});
});
describe('ReactDOMServer', () => {
it('renderToString', () => {
const content: string = ReactDOMServer.renderToString(React.createElement('div'));
});
it('renderToStaticMarkup', () => {
const content: string = ReactDOMServer.renderToStaticMarkup(React.createElement('div'));
});
});
describe('ReactDOMNodeStream', () => {
it('renderToStream', () => {
const content: any = ReactDOMNodeStream.renderToStream(React.createElement('div'));
});
it('renderToStaticStream', () => {
const content: any = ReactDOMNodeStream.renderToStaticStream(React.createElement('div'));
});
});
describe('React dom test utils', () => {
it('Simulate', () => {
const element = document.createElement('div');
const dom = ReactDOM.render(
React.createElement('input', { type: 'text' }),
element
) as Element;
const node = ReactDOM.findDOMNode(dom) as HTMLInputElement;
node.value = 'giraffe';
ReactTestUtils.Simulate.change(node);
ReactTestUtils.Simulate.keyDown(node, { key: "Enter", keyCode: 13, which: 13 });
});
it('renderIntoDocument', () => {
const element = React.createElement('input', { type: 'text' });
ReactTestUtils.renderIntoDocument(element);
});
it('mockComponent', () => {
ReactTestUtils.mockComponent(TestComponent, 'div');
});
it('isElement', () => {
const element = React.createElement(TestComponent);
const isReactElement: boolean = ReactTestUtils.isElement(element);
});
it('isElementOfType', () => {
const element = React.createElement(TestComponent);
const isReactElement: boolean = ReactTestUtils.isElementOfType(element, TestComponent);
});
it('isDOMComponent', () => {
const element = React.createElement('div');
const instance = ReactTestUtils.renderIntoDocument(element) as HTMLDivElement;
const isDOMElement: boolean = ReactTestUtils.isDOMComponent(instance);
});
it('isCompositeComponent', () => {
const element = React.createElement(TestComponent);
const instance: TestComponent = ReactTestUtils.renderIntoDocument(element);
const isCompositeComponent: boolean = ReactTestUtils.isCompositeComponent(instance);
});
it('isCompositeComponentWithType', () => {
const element = React.createElement(TestComponent);
const instance: TestComponent = ReactTestUtils.renderIntoDocument(element);
const isCompositeComponent: boolean = ReactTestUtils.isCompositeComponentWithType(instance, TestComponent);
});
it('findAllInRenderedTree', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.findAllInRenderedTree(component, (i: React.ReactInstance) => true);
});
it('scryRenderedDOMComponentsWithClass', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.scryRenderedDOMComponentsWithClass(component, 'class');
});
it('findRenderedDOMComponentWithClass', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.findRenderedDOMComponentWithClass(component, 'class');
});
it('scryRenderedDOMComponentsWithTag', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.scryRenderedDOMComponentsWithTag(component, 'div');
});
it('findRenderedDOMComponentWithTag', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.findRenderedDOMComponentWithTag(component, 'tag');
});
it('scryRenderedComponentsWithType', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.scryRenderedComponentsWithType(component, TestComponent);
});
it('findRenderedComponentWithType', () => {
const component = ReactTestUtils.renderIntoDocument(React.createElement(TestComponent));
ReactTestUtils.findRenderedComponentWithType(component, TestComponent);
});
describe('Shallow Rendering', () => {
it('createRenderer', () => {
const component = React.createElement(TestComponent);
const shallowRenderer = ReactTestUtils.createRenderer();
});
it('shallowRenderer.render', () => {
const component = React.createElement(TestComponent);
const shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(component);
});
it('shallowRenderer.getRenderOutput', () => {
const component = React.createElement(TestComponent);
const shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.getRenderOutput();
});
});
describe('act', () => {
describe('with sync callback', () => {
it('accepts a callback that is void', () => {
ReactTestUtils.act(() => {});
});
it('rejects a callback that returns null', () => {
// $ExpectError
ReactTestUtils.act(() => null);
});
it('returns a type that is not Promise-like', () => {
// tslint:disable-next-line no-void-expression
const result = ReactTestUtils.act(() => {});
// $ExpectError
result.then((x) => {});
});
});
describe('with async callback', () => {
it('accepts a callback that is void', async () => {
await ReactTestUtils.act(async () => {});
});
it('rejects a callback that returns a value', async () => {
// $ExpectError
await ReactTestUtils.act(async () => null);
});
it('returns a Promise-like', () => {
const result = ReactTestUtils.act(async () => {});
result.then((x) => {});
});
});
});
});