@@ -17,6 +17,7 @@ function applyTransform(source, options = {}) {
17
17
test ( "act in test" , ( ) => {
18
18
expect (
19
19
applyTransform ( `
20
+ import { act } from "@testing-library/react"
20
21
test("void works", () => {
21
22
act()
22
23
})
@@ -25,7 +26,8 @@ test("act in test", () => {
25
26
})
26
27
` )
27
28
) . toMatchInlineSnapshot ( `
28
- "test("void works", async () => {
29
+ "import { act } from "@testing-library/react"
30
+ test("void works", async () => {
29
31
await act()
30
32
})
31
33
test("return works", () => {
@@ -34,9 +36,26 @@ test("act in test", () => {
34
36
` ) ;
35
37
} ) ;
36
38
39
+ test ( "local act untouched" , ( ) => {
40
+ expect (
41
+ applyTransform ( `
42
+ function act() {}
43
+ test("void works", () => {
44
+ act()
45
+ })
46
+ ` )
47
+ ) . toMatchInlineSnapshot ( `
48
+ "function act() {}
49
+ test("void works", () => {
50
+ act()
51
+ })"
52
+ ` ) ;
53
+ } ) ;
54
+
37
55
test ( "act in utils #1" , ( ) => {
38
56
expect (
39
57
applyTransform ( `
58
+ import { act } from "@testing-library/react"
40
59
function caseA() {
41
60
return act()
42
61
}
@@ -51,7 +70,8 @@ test("act in utils #1", () => {
51
70
}
52
71
` )
53
72
) . toMatchInlineSnapshot ( `
54
- "function caseA() {
73
+ "import { act } from "@testing-library/react"
74
+ function caseA() {
55
75
return act()
56
76
}
57
77
@@ -69,6 +89,7 @@ test("act in utils #1", () => {
69
89
test ( "act in utils #2" , ( ) => {
70
90
expect (
71
91
applyTransform ( `
92
+ import { act } from "@testing-library/react"
72
93
function caseA() {
73
94
act()
74
95
}
@@ -84,7 +105,8 @@ test("act in utils #2", () => {
84
105
}
85
106
` )
86
107
) . toMatchInlineSnapshot ( `
87
- "async function caseA() {
108
+ "import { act } from "@testing-library/react"
109
+ async function caseA() {
88
110
await act()
89
111
}
90
112
@@ -103,6 +125,7 @@ test("act in utils #2", () => {
103
125
test ( "act in utils #3" , ( ) => {
104
126
expect (
105
127
applyTransform ( `
128
+ import { act } from "@testing-library/react"
106
129
const caseA = () => {
107
130
act()
108
131
}
@@ -116,7 +139,8 @@ test("act in utils #3", () => {
116
139
}
117
140
` )
118
141
) . toMatchInlineSnapshot ( `
119
- "const caseA = async () => {
142
+ "import { act } from "@testing-library/react"
143
+ const caseA = async () => {
120
144
await act()
121
145
}
122
146
@@ -217,3 +241,137 @@ test("React Testing Library api", () => {
217
241
});"
218
242
` ) ;
219
243
} ) ;
244
+
245
+ test ( "React Testing Library api as namespace" , ( ) => {
246
+ expect (
247
+ applyTransform ( `
248
+ import * as RTL from "@testing-library/react";
249
+
250
+ beforeEach(() => {
251
+ RTL.cleanup();
252
+ });
253
+
254
+ function renderWithProviders(element) {
255
+ const { rerender, unmount } = RTL.render(<TestProvider>{element}</TestProvider>);
256
+
257
+ return { rerender, unmount };
258
+ }
259
+
260
+ test("test", () => {
261
+ const { rerender, unmount } = renderWithProviders(<button>Test</button>);
262
+
263
+ RTL.fireEvent.click(screen.getByRole("button"));
264
+
265
+ rerender(<span />);
266
+
267
+ RTL.fireEvent(
268
+ screen.getByRole("button"),
269
+ new MouseEvent("click", {
270
+ bubbles: true,
271
+ cancelable: true,
272
+ })
273
+ );
274
+
275
+ unmount();
276
+ });
277
+
278
+ test("renderHook", () => {
279
+ const { result, unmount } = renderHook(() => useHook());
280
+
281
+ unmount();
282
+ });
283
+
284
+ ` )
285
+ ) . toMatchInlineSnapshot ( `
286
+ "import * as RTL from "@testing-library/react";
287
+
288
+ beforeEach(async () => {
289
+ await RTL.cleanup();
290
+ });
291
+
292
+ async function renderWithProviders(element) {
293
+ const { rerender, unmount } = await RTL.render(<TestProvider>{element}</TestProvider>);
294
+
295
+ return { rerender, unmount };
296
+ }
297
+
298
+ test("test", async () => {
299
+ const { rerender, unmount } = await renderWithProviders(<button>Test</button>);
300
+
301
+ RTL.fireEvent.click(screen.getByRole("button"));
302
+
303
+ await rerender(<span />);
304
+
305
+ await RTL.fireEvent(screen.getByRole("button"), new MouseEvent("click", {
306
+ bubbles: true,
307
+ cancelable: true,
308
+ }));
309
+
310
+ await unmount();
311
+ });
312
+
313
+ test("renderHook", async () => {
314
+ const { result, unmount } = renderHook(() => useHook());
315
+
316
+ await unmount();
317
+ });"
318
+ ` ) ;
319
+ } ) ;
320
+
321
+ test ( "react API" , ( ) => {
322
+ expect (
323
+ applyTransform ( `
324
+ import * as React from 'react'
325
+
326
+ test('test', () => {
327
+ React.unstable_act()
328
+ })
329
+ ` )
330
+ ) . toMatchInlineSnapshot ( `
331
+ "import * as React from 'react'
332
+
333
+ test('test', async () => {
334
+ await React.unstable_act()
335
+ })"
336
+ ` ) ;
337
+ } ) ;
338
+
339
+ test ( "react-test-renderer API" , ( ) => {
340
+ expect (
341
+ applyTransform ( `
342
+ import { act } from 'react-test-renderer'
343
+
344
+ test('test', () => {
345
+ act()
346
+ })
347
+ ` )
348
+ ) . toMatchInlineSnapshot ( `
349
+ "import { act } from 'react-test-renderer'
350
+
351
+ test('test', async () => {
352
+ await act()
353
+ })"
354
+ ` ) ;
355
+ } ) ;
356
+
357
+ test ( "react-dom API" , ( ) => {
358
+ expect (
359
+ applyTransform ( `
360
+ import { act } from 'react-dom/test-utils'
361
+ import { flushSync } from 'react-dom'
362
+
363
+ test('test', () => {
364
+ act()
365
+ flushSync()
366
+ })
367
+ ` )
368
+ ) . toMatchInlineSnapshot ( `
369
+ "import { act } from 'react-dom/test-utils'
370
+ import { flushSync } from 'react-dom'
371
+
372
+ test('test', async () => {
373
+ await act()
374
+ flushSync()
375
+ })"
376
+ ` ) ;
377
+ } ) ;
0 commit comments