-
Notifications
You must be signed in to change notification settings - Fork 655
Expand file tree
/
Copy pathmemoized.test.ts
More file actions
180 lines (147 loc) · 4.97 KB
/
memoized.test.ts
File metadata and controls
180 lines (147 loc) · 4.97 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
import { test, expect } from '@playwright/test'
test('Only ListItem should not rerender when clicking force rerender', async ({
page,
}) => {
await page.route('/', async (route) => {
const request = route.request()
if (request.resourceType() !== 'document') return route.continue()
const response = await route.fetch()
let html = await response.text()
const scriptToInject = `
<script>
let internals
const PerformedWorkFlag = 0b000000000000000000000000001
const UserCodeFiberTags = new Set([0, 1, 9, 11, 15])
let activeFallbackComponentNames = null
function isUserCodeFiberTag(tag) {
return UserCodeFiberTags.has(tag)
}
function getDisplayNameFromType(type) {
if (!type) return null
if (typeof type === 'function') {
return type.displayName || type.name || null
}
if (typeof type === 'object') {
if (typeof type.displayName === 'string' && type.displayName) {
return type.displayName
}
if (typeof type.render === 'function') {
return type.render.displayName || type.render.name || null
}
if (type.type) {
return getDisplayNameFromType(type.type)
}
}
return null
}
function getFiberDisplayName(fiber) {
return getDisplayNameFromType(fiber.type) || getDisplayNameFromType(fiber.elementType)
}
function didFiberRender(previousFiber, nextFiber) {
if (!previousFiber) return true
return (nextFiber.flags & PerformedWorkFlag) === PerformedWorkFlag
}
function collectRenderedComponentsFromCommit(root) {
if (!activeFallbackComponentNames || !root?.current) return
const stack = [root.current]
while (stack.length > 0) {
const fiber = stack.pop()
if (!fiber) continue
if (fiber.sibling) stack.push(fiber.sibling)
if (fiber.child) stack.push(fiber.child)
if (!isUserCodeFiberTag(fiber.tag)) continue
if (!didFiberRender(fiber.alternate, fiber)) continue
const componentName = getFiberDisplayName(fiber)
if (componentName) {
activeFallbackComponentNames.push(componentName)
}
}
}
function enhanceExistingHook(existingHook) {
const originalInject = existingHook.inject
const originalCommitFiberRoot = existingHook.onCommitFiberRoot
existingHook.inject = (injectedInternals) => {
internals = injectedInternals
// Returning a number as React expects a renderer ID
return originalInject?.call(existingHook, injectedInternals) ?? 1
}
existingHook.onCommitFiberRoot = (...args) => {
const [, root] = args
collectRenderedComponentsFromCommit(root)
return originalCommitFiberRoot?.apply(existingHook, args)
}
return existingHook
}
function createMinimalHook() {
return {
renderers: [],
supportsFiber: true,
inject: (injectedInternals) => {
internals = injectedInternals
return 1 // Returning a number as React expects a renderer ID
},
onCommitFiberRoot: (_rendererId, root) => {
collectRenderedComponentsFromCommit(root)
},
onCommitFiberUnmount: () => {},
}
}
async function getComponentCalls(cb) {
const componentNames = []
if (!internals) {
throw new Error('🚨 React DevTools is not available')
}
const supportsInjectedProfilingHooks =
typeof internals.injectProfilingHooks === 'function'
if (supportsInjectedProfilingHooks) {
internals.enableProfilerTimer = true
internals.enableProfilerCommitHooks = true
internals.injectProfilingHooks({
markComponentRenderStarted: (fiber) => {
componentNames.push(fiber.type.name || 'Anonymous')
},
})
} else {
activeFallbackComponentNames = componentNames
}
try {
await cb()
} finally {
if (supportsInjectedProfilingHooks) {
internals.enableProfilerTimer = false
internals.enableProfilerCommitHooks = false
internals.injectProfilingHooks(null)
} else {
activeFallbackComponentNames = null
}
}
return componentNames
}
window.getComponentCalls = getComponentCalls
if (window.__REACT_DEVTOOLS_GLOBAL_HOOK__) {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = enhanceExistingHook(
window.__REACT_DEVTOOLS_GLOBAL_HOOK__,
)
} else {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = createMinimalHook()
}
</script>
`
html = html.replace('<head>', `<head>${scriptToInject}`)
await route.fulfill({
body: html,
headers: { 'content-type': 'text/html' },
})
})
await page.goto('/')
await page.waitForLoadState('networkidle')
const calledComponents: Array<string> = await page.evaluate(() =>
(window as any).getComponentCalls(() => {
document.querySelector('button')?.click()
}),
)
expect(
calledComponents,
'🚨 The ListItem component was rendered when clicking force render. Use the `memo` utility from React on the ListItem component to prevent this.',
).not.toContain('ListItem')
})