Added commit tree builder test
Brian Vaughn committed
May 6, 2019 at 13:41 UTC
c2bf71f406e530813b023eaa267c5e5e49869d32
2 files changed
+253
src/__tests__/__snapshots__/profilingCommitTreeBuilder-test.js.snap
new
+162
@@ -0,0 +1,162 @@
1
+// Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+exports[`commit tree should be able to rebuild the store tree for each commit: 0: CommitTree 1`] = `
4
+Object {
5
+ "nodes": Map {
6
+ 1 => Object {
7
+ "children": Array [
8
+ 2,
9
+ ],
10
+ "displayName": null,
11
+ "id": 1,
12
+ "key": null,
13
+ "parentID": 0,
14
+ "treeBaseDuration": 12,
15
+ },
16
+ 2 => Object {
17
+ "children": Array [
18
+ 3,
19
+ ],
20
+ "displayName": "Parent",
21
+ "id": 2,
22
+ "key": null,
23
+ "parentID": 1,
24
+ "treeBaseDuration": 12,
25
+ },
26
+ 3 => Object {
27
+ "children": Array [],
28
+ "displayName": "Memo(Child)",
29
+ "id": 3,
30
+ "key": "0",
31
+ "parentID": 2,
32
+ "treeBaseDuration": 2,
33
+ },
34
+ },
35
+ "rootID": 1,
36
+}
37
+`;
38
+
39
+exports[`commit tree should be able to rebuild the store tree for each commit: 1: CommitTree 1`] = `
40
+Object {
41
+ "nodes": Map {
42
+ 1 => Object {
43
+ "children": Array [
44
+ 2,
45
+ ],
46
+ "displayName": null,
47
+ "id": 1,
48
+ "key": null,
49
+ "parentID": 0,
50
+ "treeBaseDuration": 16,
51
+ },
52
+ 2 => Object {
53
+ "children": Array [
54
+ 3,
55
+ 4,
56
+ 5,
57
+ ],
58
+ "displayName": "Parent",
59
+ "id": 2,
60
+ "key": null,
61
+ "parentID": 1,
62
+ "treeBaseDuration": 16,
63
+ },
64
+ 3 => Object {
65
+ "children": Array [],
66
+ "displayName": "Memo(Child)",
67
+ "id": 3,
68
+ "key": "0",
69
+ "parentID": 2,
70
+ "treeBaseDuration": 2,
71
+ },
72
+ 4 => Object {
73
+ "children": Array [],
74
+ "displayName": "Memo(Child)",
75
+ "id": 4,
76
+ "key": "1",
77
+ "parentID": 2,
78
+ "treeBaseDuration": 2,
79
+ },
80
+ 5 => Object {
81
+ "children": Array [],
82
+ "displayName": "Memo(Child)",
83
+ "id": 5,
84
+ "key": "2",
85
+ "parentID": 2,
86
+ "treeBaseDuration": 2,
87
+ },
88
+ },
89
+ "rootID": 1,
90
+}
91
+`;
92
+
93
+exports[`commit tree should be able to rebuild the store tree for each commit: 2: CommitTree 1`] = `
94
+Object {
95
+ "nodes": Map {
96
+ 1 => Object {
97
+ "children": Array [
98
+ 2,
99
+ ],
100
+ "displayName": null,
101
+ "id": 1,
102
+ "key": null,
103
+ "parentID": 0,
104
+ "treeBaseDuration": 14,
105
+ },
106
+ 2 => Object {
107
+ "children": Array [
108
+ 3,
109
+ 4,
110
+ ],
111
+ "displayName": "Parent",
112
+ "id": 2,
113
+ "key": null,
114
+ "parentID": 1,
115
+ "treeBaseDuration": 14,
116
+ },
117
+ 3 => Object {
118
+ "children": Array [],
119
+ "displayName": "Memo(Child)",
120
+ "id": 3,
121
+ "key": "0",
122
+ "parentID": 2,
123
+ "treeBaseDuration": 2,
124
+ },
125
+ 4 => Object {
126
+ "children": Array [],
127
+ "displayName": "Memo(Child)",
128
+ "id": 4,
129
+ "key": "1",
130
+ "parentID": 2,
131
+ "treeBaseDuration": 2,
132
+ },
133
+ },
134
+ "rootID": 1,
135
+}
136
+`;
137
+
138
+exports[`commit tree should be able to rebuild the store tree for each commit: 3: CommitTree 1`] = `
139
+Object {
140
+ "nodes": Map {
141
+ 1 => Object {
142
+ "children": Array [
143
+ 2,
144
+ ],
145
+ "displayName": null,
146
+ "id": 1,
147
+ "key": null,
148
+ "parentID": 0,
149
+ "treeBaseDuration": 10,
150
+ },
151
+ 2 => Object {
152
+ "children": Array [],
153
+ "displayName": "Parent",
154
+ "id": 2,
155
+ "key": null,
156
+ "parentID": 1,
157
+ "treeBaseDuration": 10,
158
+ },
159
+ },
160
+ "rootID": 1,
161
+}
162
+`;
src/__tests__/profilingCommitTreeBuilder-test.js
new
+91
@@ -0,0 +1,91 @@
1
+// @flow
2
+
3
+import typeof TestRendererType from 'react-test-renderer';
4
+import type Store from 'src/devtools/store';
5
+
6
+describe('commit tree', () => {
7
+ let React;
8
+ let ReactDOM;
9
+ let Scheduler;
10
+ let SchedulerTracing;
11
+ let TestRenderer: TestRendererType;
12
+ let store: Store;
13
+ let utils;
14
+
15
+ beforeEach(() => {
16
+ utils = require('./utils');
17
+ utils.beforeEachProfiling();
18
+
19
+ store = global.store;
20
+ store.collapseNodesByDefault = false;
21
+
22
+ React = require('react');
23
+ ReactDOM = require('react-dom');
24
+ Scheduler = require('scheduler');
25
+ SchedulerTracing = require('scheduler/tracing');
26
+ TestRenderer = utils.requireTestRenderer();
27
+ });
28
+
29
+ it('should be able to rebuild the store tree for each commit', async done => {
30
+ const Parent = ({ count }) => {
31
+ Scheduler.advanceTime(10);
32
+ return new Array(count)
33
+ .fill(true)
34
+ .map((_, index) => <Child key={index} />);
35
+ };
36
+ const Child = React.memo(function Child() {
37
+ Scheduler.advanceTime(2);
38
+ return null;
39
+ });
40
+
41
+ const container = document.createElement('div');
42
+
43
+ utils.act(() => store.startProfiling());
44
+ utils.act(() => ReactDOM.render(<Parent count={1} />, container));
45
+ utils.act(() => ReactDOM.render(<Parent count={3} />, container));
46
+ utils.act(() => ReactDOM.render(<Parent count={2} />, container));
47
+ utils.act(() => ReactDOM.render(<Parent count={0} />, container));
48
+ utils.act(() => store.stopProfiling());
49
+
50
+ let suspenseResolved = false;
51
+
52
+ function Suspender({ commitIndex, rendererID, rootID }) {
53
+ const profilingSummary = store.profilingCache.ProfilingSummary.read({
54
+ rendererID,
55
+ rootID,
56
+ });
57
+ suspenseResolved = true;
58
+ const commitTree = store.profilingCache.getCommitTree({
59
+ commitIndex,
60
+ profilingSummary,
61
+ });
62
+ expect(commitTree).toMatchSnapshot(`${commitIndex}: CommitTree`);
63
+ return null;
64
+ }
65
+
66
+ const rendererID = utils.getRendererID();
67
+ const rootID = store.roots[0];
68
+
69
+ for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
70
+ suspenseResolved = false;
71
+
72
+ await utils.actSuspense(
73
+ () =>
74
+ TestRenderer.create(
75
+ <React.Suspense fallback={null}>
76
+ <Suspender
77
+ commitIndex={commitIndex}
78
+ rendererID={rendererID}
79
+ rootID={rootID}
80
+ />
81
+ </React.Suspense>
82
+ ),
83
+ 3
84
+ );
85
+
86
+ expect(suspenseResolved).toBe(true);
87
+ }
88
+
89
+ done();
90
+ });
91
+});