-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdevtools-bridge.test.ts
More file actions
191 lines (171 loc) · 4.8 KB
/
devtools-bridge.test.ts
File metadata and controls
191 lines (171 loc) · 4.8 KB
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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { DevToolsBridge } from '../devtools-bridge.js';
import { ComponentTree } from '../component-tree.js';
import { Profiler } from '../profiler.js';
function buildStringTable(strings: string[]): [number[], Map<string, number>] {
const idMap = new Map<string, number>();
const data: number[] = [];
for (const s of strings) {
const id = idMap.size + 1;
if (!idMap.has(s)) {
idMap.set(s, id);
data.push(s.length, ...Array.from(s).map((c) => c.charCodeAt(0)));
}
}
return [data, idMap];
}
function buildOps(
rendererID: number,
rootID: number,
strings: string[],
opsFn: (strId: (s: string) => number) => number[],
): number[] {
const [tableData, idMap] = buildStringTable(strings);
const strId = (s: string) => idMap.get(s) || 0;
const ops = opsFn(strId);
return [rendererID, rootID, tableData.length, ...tableData, ...ops];
}
function addOp(
id: number,
elementType: number,
parentId: number,
displayNameStrId: number,
): number[] {
return [1, id, elementType, parentId, 0, displayNameStrId, 0];
}
describe('DevToolsBridge', () => {
let tree: ComponentTree;
let bridge: DevToolsBridge;
beforeEach(() => {
tree = new ComponentTree();
bridge = new DevToolsBridge(8097, tree, new Profiler());
const ops = buildOps(1, 100, ['App'], (s) => [
...addOp(1, 5, 0, s('App')),
]);
tree.applyOperations(ops);
});
afterEach(() => {
vi.restoreAllMocks();
});
it('preserves source metadata from inspected element payloads', async () => {
const pending = new Promise((resolve) => {
(bridge as any).pendingInspections.set(123, {
id: 1,
resolve,
timer: setTimeout(() => {}, 1000),
});
});
(bridge as any).handleInspectedElement({
type: 'full-data',
id: 1,
responseID: 123,
value: {
id: 1,
displayName: 'App',
type: 5,
key: null,
props: {},
state: null,
hooks: null,
source: {
fileName: '/src/App.tsx',
lineNumber: 10,
columnNumber: 4,
},
},
});
await expect(pending).resolves.toMatchObject({
source: {
fileName: '/src/App.tsx',
lineNumber: 10,
columnNumber: 4,
},
});
});
it('keeps inspection working when source metadata is missing', async () => {
const pending = new Promise((resolve) => {
(bridge as any).pendingInspections.set(123, {
id: 1,
resolve,
timer: setTimeout(() => {}, 1000),
});
});
(bridge as any).handleInspectedElement({
type: 'full-data',
id: 1,
responseID: 123,
value: {
id: 1,
displayName: 'App',
type: 5,
key: null,
props: { count: 1 },
state: null,
hooks: null,
},
});
await expect(pending).resolves.toMatchObject({
displayName: 'App',
props: { count: 1 },
source: undefined,
});
});
it('returns cached inspection data without a live connection', async () => {
(bridge as any).inspectionCache.set(1, {
id: 1,
displayName: 'App',
type: 'function',
key: null,
props: { cached: true },
state: null,
hooks: null,
renderedAt: null,
source: undefined,
});
await expect(bridge.inspectElement(1)).resolves.toMatchObject({
props: { cached: true },
});
});
it('supports concurrent inspections for the same component id', async () => {
const sent: Array<{ event: string; payload: { requestID: number } }> = [];
(bridge as any).connections.add({});
(bridge as any).sendToAll = (msg: { event: string; payload: { requestID: number } }) => {
sent.push(msg);
};
const first = bridge.inspectElement(1);
const second = bridge.inspectElement(1);
expect(sent).toHaveLength(2);
expect(sent[0].payload.requestID).not.toBe(sent[1].payload.requestID);
expect((bridge as any).pendingInspections.size).toBe(2);
(bridge as any).handleInspectedElement({
type: 'full-data',
id: 1,
responseID: sent[0].payload.requestID,
value: {
id: 1,
displayName: 'App',
type: 5,
key: null,
props: { source: 'first' },
state: null,
hooks: null,
},
});
(bridge as any).handleInspectedElement({
type: 'full-data',
id: 1,
responseID: sent[1].payload.requestID,
value: {
id: 1,
displayName: 'App',
type: 5,
key: null,
props: { source: 'second' },
state: null,
hooks: null,
},
});
await expect(first).resolves.toMatchObject({ props: { source: 'first' } });
await expect(second).resolves.toMatchObject({ props: { source: 'second' } });
});
});