Skip to content

Commit e0236a5

Browse files
authored
fix: wrap unmount in act (#706)
1 parent bc3d1bc commit e0236a5

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/__tests__/render.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,20 @@ test('unmount', () => {
282282
expect(fn).toHaveBeenCalled();
283283
});
284284

285+
test('unmount should handle cleanup functions', () => {
286+
const cleanup = jest.fn();
287+
const Component = () => {
288+
React.useEffect(() => cleanup);
289+
return null;
290+
};
291+
292+
const { unmount } = render(<Component />);
293+
294+
unmount();
295+
296+
expect(cleanup).toHaveBeenCalledTimes(1);
297+
});
298+
285299
test('toJSON', () => {
286300
const { toJSON } = render(<MyButton>press me</MyButton>);
287301

src/render.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@ export default function render<T>(
4646
);
4747
const update = updateWithAct(renderer, wrap);
4848
const instance = renderer.root;
49+
const unmount = () => {
50+
act(() => {
51+
renderer.unmount();
52+
});
53+
};
4954

50-
addToCleanupQueue(renderer.unmount);
55+
addToCleanupQueue(unmount);
5156

5257
return {
5358
...getByAPI(instance),
5459
...queryByAPI(instance),
5560
...findByAPI(instance),
5661
...a11yAPI(instance),
5762
update,
63+
unmount,
5864
container: instance,
5965
rerender: update, // alias for `update`
60-
unmount: renderer.unmount,
6166
toJSON: renderer.toJSON,
6267
debug: debug(instance, renderer),
6368
};

0 commit comments

Comments
 (0)