-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathindex.test.ts
41 lines (37 loc) · 1.04 KB
/
index.test.ts
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 { renderHook } from '@testing-library/react';
import { act } from 'react';
import useSetState from '../index';
describe('useSetState', () => {
const setUp = <T extends object>(initialValue: T) =>
renderHook(() => {
const [state, setState] = useSetState<T>(initialValue);
return {
state,
setState,
} as const;
});
it('should support initialValue', () => {
const hook = setUp({
hello: 'world',
});
expect(hook.result.current.state).toEqual({ hello: 'world' });
});
it('should support object', () => {
const hook = setUp<any>({
hello: 'world',
});
act(() => {
hook.result.current.setState({ foo: 'bar' });
});
expect(hook.result.current.state).toEqual({ hello: 'world', foo: 'bar' });
});
it('should support function update', () => {
const hook = setUp({
count: 0,
});
act(() => {
hook.result.current.setState((prev) => ({ count: prev.count + 1 }));
});
expect(hook.result.current.state).toEqual({ count: 1 });
});
});