Skip to content

Commit

Permalink
fix(cleanup): Cleanup should flush microtask queue after unmount (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed Jun 25, 2020
1 parent 5814b29 commit aac2e44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/__tests__/cleanup.js
Expand Up @@ -26,3 +26,17 @@ test('cleanup does not error when an element is not a child', async () => {
render(<div />, {container: document.createElement('div')})
await cleanup()
})

test('cleanup waits for queued microtasks during unmount sequence', async () => {
const spy = jest.fn()

const Test = () => {
React.useEffect(() => () => setImmediate(spy))

return null
}

render(<Test />)
await cleanup()
expect(spy).toHaveBeenCalledTimes(1)
})
4 changes: 3 additions & 1 deletion src/pure.js
Expand Up @@ -97,8 +97,10 @@ function render(
}

async function cleanup() {
await flush()
mountedContainers.forEach(cleanupAtContainer)
// flush microtask queue after unmounting in case
// unmount sequence generates new microtasks
await flush()
}

// maybe one day we'll expose this (perhaps even as a utility returned by render).
Expand Down

0 comments on commit aac2e44

Please sign in to comment.