Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unmount): Flush useEffect cleanup functions syncronously #746

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/__tests__/render.js
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
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