Skip to content

Commit 3a7f185

Browse files
committed
feat: Use React scheduler to flush instead of custom implementation
1 parent 7b686e4 commit 3a7f185

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

Diff for: src/__tests__/cleanup.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
2-
import {render, cleanup, act} from '../'
2+
import {render, cleanup} from '../'
33

4-
test('cleans up the document', async () => {
4+
test('cleans up the document', () => {
55
const spy = jest.fn()
66
const divId = 'my-div'
77

@@ -17,17 +17,17 @@ test('cleans up the document', async () => {
1717
}
1818

1919
render(<Test />)
20-
await cleanup()
20+
cleanup()
2121
expect(document.body).toBeEmptyDOMElement()
2222
expect(spy).toHaveBeenCalledTimes(1)
2323
})
2424

25-
test('cleanup does not error when an element is not a child', async () => {
25+
test('cleanup does not error when an element is not a child', () => {
2626
render(<div />, {container: document.createElement('div')})
27-
await cleanup()
27+
cleanup()
2828
})
2929

30-
test('cleanup runs effect cleanup functions', async () => {
30+
test('cleanup runs effect cleanup functions', () => {
3131
const spy = jest.fn()
3232

3333
const Test = () => {
@@ -37,7 +37,7 @@ test('cleanup runs effect cleanup functions', async () => {
3737
}
3838

3939
render(<Test />)
40-
await cleanup()
40+
cleanup()
4141
expect(spy).toHaveBeenCalledTimes(1)
4242
})
4343

@@ -54,7 +54,7 @@ describe('fake timers and missing act warnings', () => {
5454
jest.useRealTimers()
5555
})
5656

57-
test('cleanup does not flush immediates', async () => {
57+
test('cleanup does not flush immediates', () => {
5858
const microTaskSpy = jest.fn()
5959
function Test() {
6060
const counter = 1
@@ -77,15 +77,15 @@ describe('fake timers and missing act warnings', () => {
7777
}
7878
render(<Test />)
7979

80-
await cleanup()
80+
cleanup()
8181

8282
expect(microTaskSpy).toHaveBeenCalledTimes(0)
8383
// console.error is mocked
8484
// eslint-disable-next-line no-console
8585
expect(console.error).toHaveBeenCalledTimes(0)
8686
})
8787

88-
test('cleanup does not swallow missing act warnings', async () => {
88+
test('cleanup does not swallow missing act warnings', () => {
8989
const deferredStateUpdateSpy = jest.fn()
9090
function Test() {
9191
const counter = 1
@@ -109,7 +109,7 @@ describe('fake timers and missing act warnings', () => {
109109
render(<Test />)
110110

111111
jest.runAllImmediates()
112-
await cleanup()
112+
cleanup()
113113

114114
expect(deferredStateUpdateSpy).toHaveBeenCalledTimes(1)
115115
// console.error is mocked

Diff for: src/pure.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@testing-library/dom'
88
import act, {asyncAct} from './act-compat'
99
import {fireEvent} from './fire-event'
10-
import flush from './flush-microtasks'
1110

1211
configureDTL({
1312
asyncWrapper: async cb => {
@@ -100,17 +99,16 @@ function render(
10099
}
101100
}
102101

103-
async function cleanup() {
102+
function cleanup() {
104103
mountedContainers.forEach(cleanupAtContainer)
105-
// flush microtask queue after unmounting in case
106-
// unmount sequence generates new microtasks
107-
await flush()
108104
}
109105

110106
// maybe one day we'll expose this (perhaps even as a utility returned by render).
111107
// but let's wait until someone asks for it.
112108
function cleanupAtContainer(container) {
113-
ReactDOM.unmountComponentAtNode(container)
109+
act(() => {
110+
ReactDOM.unmountComponentAtNode(container)
111+
})
114112
if (container.parentNode === document.body) {
115113
document.body.removeChild(container)
116114
}

0 commit comments

Comments
 (0)