forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseEvent.spec.tsx
41 lines (36 loc) · 1.36 KB
/
useEvent.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { fireEvent, render, screen } from '@testing-library/react';
import * as React from 'react';
import { useEvent } from './useEvent';
describe('useEvent', () => {
const Parent = () => {
const [value, setValue] = React.useState(0);
const handler = useEvent(() => {
return 1;
});
return (
<>
<span>Parent {value}</span>;
<button onClick={() => setValue(val => val + 1)}>Click</button>
<Child handler={handler} />
</>
);
};
const Child = React.memo(({ handler }: { handler: () => number }) => {
const [value, setValue] = React.useState(0);
React.useEffect(() => {
setValue(val => val + 1);
}, [handler]);
return <span>Child {value}</span>;
});
it('should be referentially stable', async () => {
render(<Parent />);
expect(screen.getByText('Parent 0')).not.toBeNull();
expect(screen.getByText('Child 1')).not.toBeNull();
fireEvent.click(screen.getByText('Click'));
expect(screen.getByText('Parent 1')).not.toBeNull();
expect(screen.getByText('Child 1')).not.toBeNull();
fireEvent.click(screen.getByText('Click'));
expect(screen.getByText('Parent 2')).not.toBeNull();
expect(screen.getByText('Child 1')).not.toBeNull();
});
});