Skip to content

Commit d072a30

Browse files
author
Brian Vaughn
committed
Alternate fix for DevTools+Refres bug
1 parent df7ee20 commit d072a30

16 files changed

+513
-522
lines changed

packages/react-devtools-shared/src/__tests__/FastRefreshDevToolsIntegration-test.js

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,48 @@ describe('Fast Refresh', () => {
131131
};
132132
133133
function Child() {
134-
return null;
134+
return <div />;
135135
};
136136
137137
export default Parent;
138138
`);
139+
expect(store).toMatchInlineSnapshot(`
140+
[root]
141+
▾ <Parent>
142+
<Child key="A">
143+
`);
144+
145+
let element = container.firstChild;
146+
expect(container.firstChild).not.toBe(null);
147+
148+
patch(`
149+
function Parent() {
150+
return <Child key="A" />;
151+
};
152+
153+
function Child() {
154+
return <div />;
155+
};
139156
157+
export default Parent;
158+
`);
140159
expect(store).toMatchInlineSnapshot(`
141160
[root]
142161
▾ <Parent>
143162
<Child key="A">
144163
`);
145164

165+
// State is preserved; this verifies that Fast Refresh is wired up.
166+
expect(container.firstChild).toBe(element);
167+
element = container.firstChild;
168+
146169
patch(`
147170
function Parent() {
148171
return <Child key="B" />;
149172
};
150173
151174
function Child() {
152-
return null;
175+
return <div />;
153176
};
154177
155178
export default Parent;
@@ -159,6 +182,9 @@ describe('Fast Refresh', () => {
159182
▾ <Parent>
160183
<Child key="B">
161184
`);
185+
186+
// State is reset because hooks changed.
187+
expect(container.firstChild).not.toBe(element);
162188
});
163189

164190
it('should not break when there are warnings in between patching', () => {
@@ -168,10 +194,8 @@ describe('Fast Refresh', () => {
168194
169195
export default function Component() {
170196
const [state, setState] = useState(1);
171-
172197
console.warn("Expected warning during render");
173-
174-
return <div />;
198+
return null;
175199
}
176200
`);
177201
});
@@ -181,56 +205,56 @@ describe('Fast Refresh', () => {
181205
<Component> ⚠
182206
`);
183207

184-
let element = container.firstChild;
185-
186208
withErrorsOrWarningsIgnored(['Expected warning during render'], () => {
187209
patch(`
188-
const {useState} = React;
210+
const {useEffect, useState} = React;
189211
190212
export default function Component() {
191213
const [state, setState] = useState(1);
192-
193214
console.warn("Expected warning during render");
194-
195-
return <div id="one" />;
215+
return null;
196216
}
197217
`);
198218
});
199-
200-
// This is the same component type, so the warning count carries over.
201219
expect(store).toMatchInlineSnapshot(`
202220
✕ 0, ⚠ 2
203221
[root]
204222
<Component> ⚠
205223
`);
206224

207-
// State is preserved; this verifies that Fast Refresh is wired up.
208-
expect(container.firstChild).toBe(element);
209-
element = container.firstChild;
210-
211225
withErrorsOrWarningsIgnored(['Expected warning during render'], () => {
212226
patch(`
213227
const {useEffect, useState} = React;
214228
215229
export default function Component() {
216-
const [state, setState] = useState(3);
230+
const [state, setState] = useState(1);
217231
useEffect(() => {});
218-
219232
console.warn("Expected warning during render");
220-
221-
return <div id="one" />;
233+
return null;
222234
}
223235
`);
224236
});
225-
226-
// This is a new component type, so the warning count has been reset.
227237
expect(store).toMatchInlineSnapshot(`
228238
✕ 0, ⚠ 1
229239
[root]
230240
<Component> ⚠
231241
`);
232242

233-
// State is reset because hooks changed.
234-
expect(container.firstChild).not.toBe(element);
243+
withErrorsOrWarningsIgnored(['Expected warning during render'], () => {
244+
patch(`
245+
const {useEffect, useState} = React;
246+
247+
export default function Component() {
248+
const [state, setState] = useState(1);
249+
console.warn("Expected warning during render");
250+
return null;
251+
}
252+
`);
253+
});
254+
expect(store).toMatchInlineSnapshot(`
255+
✕ 0, ⚠ 1
256+
[root]
257+
<Component> ⚠
258+
`);
235259
});
236260
});

0 commit comments

Comments
 (0)