Skip to content

Commit

Permalink
fix(unmount): Flush useEffect cleanup functions syncronously (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Jul 15, 2020
1 parent 240900c commit b82773c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/__tests__/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ test('renders options.wrapper around node', () => {
</div>
`)
})

test('flushes useEffect cleanup functions sync on unmount()', () => {
const spy = jest.fn()
function Component() {
React.useEffect(() => spy, [])
return null
}
const {unmount} = render(<Component />)
expect(spy).toHaveBeenCalledTimes(0)

unmount()

expect(spy).toHaveBeenCalledTimes(1)
})
6 changes: 5 additions & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ function render(
el.forEach(e => console.log(prettyDOM(e, maxLength, options)))
: // eslint-disable-next-line no-console,
console.log(prettyDOM(el, maxLength, options)),
unmount: () => ReactDOM.unmountComponentAtNode(container),
unmount: () => {
act(() => {
ReactDOM.unmountComponentAtNode(container)
})
},
rerender: rerenderUi => {
render(wrapUiIfNeeded(rerenderUi), {container, baseElement})
// Intentionally do not return anything to avoid unnecessarily complicating the API.
Expand Down

0 comments on commit b82773c

Please sign in to comment.