Skip to content

Commit 3318e0d

Browse files
aduthnickserv
andauthored
fix: flush pending hooks effects in rerender (#32)
Call preact/test-utils act which manages the same setupRerender behavior implemented previously in preact-testing-library, but also flushes any pending hook effects Co-authored-by: Nick McCurdy <[email protected]>
1 parent d7364c5 commit 3318e0d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/__tests__/rerender.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import '@testing-library/jest-dom/extend-expect'
22
import { h } from 'preact'
3+
import { useState, useEffect } from 'preact/hooks'
34
import { render } from '..'
45

56
test('rerender will re-render the element', () => {
@@ -12,6 +13,23 @@ test('rerender will re-render the element', () => {
1213
expect(container.firstChild).toHaveTextContent('hey')
1314
})
1415

16+
test('rerender will flush pending hooks effects', async () => {
17+
const Component = () => {
18+
const [value, setValue] = useState(0)
19+
useEffect(() => {
20+
const timeoutId = setTimeout(() => setValue(1), 0)
21+
return () => clearTimeout(timeoutId)
22+
})
23+
24+
return value
25+
}
26+
27+
const { rerender, findByText } = render(<Component />)
28+
rerender(<Component />)
29+
30+
await findByText('1')
31+
})
32+
1533
test('hydrate will not update props until next render', () => {
1634
const initialInputElement = document.createElement('input')
1735
const container = document.createElement('div')

src/pure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function render (
6969
: console.log(prettyDOM(el, maxLength, options)),
7070
unmount: () => preactRender(null, container),
7171
rerender: (rerenderUi) => {
72-
setupRerender()()
72+
act(() => {})
7373
render(wrapUiIfNeeded(rerenderUi), { container, baseElement })
7474
// Intentionally do not return anything to avoid unnecessarily complicating the API.
7575
// folks can use all the same utilities we return in the first place that are bound to

0 commit comments

Comments
 (0)