Skip to content

Commit 75abf37

Browse files
committed
Add test for isMounted hook
1 parent cf6e21b commit 75abf37

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useEffect } from 'react';
2+
import { act } from 'react-dom/test-utils';
3+
import { mount } from 'enzyme';
4+
import useIsMounted from '../../hooks';
5+
6+
describe('useIsMounted', () => {
7+
it('mounts and unmounts', async () => {
8+
expect.assertions(2);
9+
10+
let wrapper;
11+
const isMountedSpy = ({ current }) => current;
12+
13+
const Dummy = () => {
14+
const isMounted = useIsMounted();
15+
16+
useEffect(() => {
17+
expect(isMountedSpy(isMounted)).toEqual(true);
18+
19+
return () => {
20+
expect(isMountedSpy(isMounted)).toEqual(false);
21+
};
22+
});
23+
24+
return <span>Dummy</span>;
25+
};
26+
27+
await act(async () => {
28+
wrapper = mount(<Dummy />);
29+
});
30+
wrapper.update();
31+
32+
await act(async () => {
33+
wrapper.unmount();
34+
});
35+
wrapper.update();
36+
});
37+
});

0 commit comments

Comments
 (0)