-
Notifications
You must be signed in to change notification settings - Fork 48.4k
/
Copy pathReactPersistentUpdatesMinimalism-test.js
157 lines (137 loc) · 3.66 KB
/
ReactPersistentUpdatesMinimalism-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
/**
* 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
* @jest-environment node
*/
'use strict';
let React;
let ReactNoopPersistent;
describe('ReactPersistentUpdatesMinimalism', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoopPersistent = require('react-noop-renderer/persistent');
});
it('should render a simple component', () => {
function Child() {
return <div>Hello World</div>;
}
function Parent() {
return <Child />;
}
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 1,
hostCloneCounter: 1,
});
});
it('should not diff referentially equal host elements', () => {
function Leaf(props) {
return (
<span>
hello
<b />
{props.name}
</span>
);
}
const constEl = (
<div>
<Leaf name="world" />
</div>
);
function Child() {
return constEl;
}
function Parent() {
return <Child />;
}
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
});
it('should not diff parents of setState targets', () => {
let childInst;
function Leaf(props) {
return (
<span>
hello
<b />
{props.name}
</span>
);
}
class Child extends React.Component {
state = {name: 'Batman'};
render() {
childInst = this;
return (
<div>
<Leaf name={this.state.name} />
</div>
);
}
}
function Parent() {
return (
<section>
<div>
<Leaf name="world" />
<Child />
<hr />
<Leaf name="world" />
</div>
</section>
);
}
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
childInst.setState({name: 'Robin'});
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
// section > div > Child > div
// section > div > Child > Leaf > span
// section > div > Child > Leaf > span > b
hostDiffCounter: 3,
// section
// section > div
// section > div > Child > div
// section > div > Child > Leaf > span
// section > div > Child > Leaf > span > b
hostCloneCounter: 5,
});
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
// Parent > section
// Parent > section > div
// Parent > section > div > Leaf > span
// Parent > section > div > Leaf > span > b
// Parent > section > div > Child > div
// Parent > section > div > Child > div > Leaf > span
// Parent > section > div > Child > div > Leaf > span > b
// Parent > section > div > hr
// Parent > section > div > Leaf > span
// Parent > section > div > Leaf > span > b
hostDiffCounter: 10,
hostCloneCounter: 10,
});
});
});