forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-test-renderer-tests.ts
88 lines (74 loc) · 2.25 KB
/
react-test-renderer-tests.ts
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
import React = require("react");
import { act, create, ReactTestInstance } from "react-test-renderer";
import { createRenderer } from 'react-test-renderer/shallow';
class TestComponent extends React.Component { }
const renderer = create(React.createElement("div"), {
createNodeMock: (el: React.ReactElement) => {
return {};
}
});
const json = renderer.toJSON();
if (json) {
json.type = "t";
json.props = {
prop1: "p",
};
json.children = [json];
}
const tree = renderer.toTree();
if (tree) {
tree.type = "t";
tree.props = {
prop1: "p",
};
tree.children = [tree];
tree.rendered = tree;
tree.nodeType = "component";
tree.nodeType = "host";
}
renderer.update(React.createElement(TestComponent));
renderer.unmount();
renderer.unmount(React.createElement(TestComponent));
function testInstance(inst: ReactTestInstance) {
inst.children = [inst, "a"];
inst.parent = instance;
inst.parent = null;
inst.props = {
prop1: "p",
};
inst.type = "a";
testInstance(inst.find(n => n.type === "a"));
testInstance(inst.findByProps({ prop1: "p" }));
testInstance(inst.findByType("a"));
testInstance(inst.findByType(TestComponent));
inst.findAll(n => n.type === "div", { deep: true }).map(testInstance);
inst.findAllByProps({ prop1: "p" }, { deep: true }).map(testInstance);
inst.findAllByType("a", { deep: true }).map(testInstance);
inst.findAllByType(TestComponent, { deep: true }).map(testInstance);
}
const instance = renderer.getInstance();
if (instance) {
testInstance(instance);
}
testInstance(renderer.root);
const component = React.createElement(TestComponent);
const shallowRenderer = createRenderer();
shallowRenderer.render(component);
shallowRenderer.getRenderOutput();
shallowRenderer.getMountedInstance();
// Only synchronous, void callbacks are acceptable for act()
act(() => {});
// $ExpectError
act(() => null);
// $ExpectError
Promise.resolve(act(() => {}));
// async act is now acceptable in React 16.9,
// but the result must be void or undefined
Promise.resolve(act(async () => {}));
void (async () => {
act(() => {});
await act(async () => {});
await act(async () => undefined);
// $ExpectError
await act(async () => null);
})();