|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 2 | +import { useCheckboxState } from '../Checkbox'; |
| 3 | + |
| 4 | +describe('useCheckboxState tests', () => { |
| 5 | + it('should update when change is called', () => { |
| 6 | + const { result } = renderHook(() => useCheckboxState()); |
| 7 | + |
| 8 | + expect(result.current).toMatchObject({ |
| 9 | + state: expect.any(Boolean), |
| 10 | + setState: expect.any(Function), |
| 11 | + onChange: expect.any(Function), |
| 12 | + }); |
| 13 | + |
| 14 | + act(() => { |
| 15 | + result.current.onChange({ currentTarget: { value: '' } } as any); |
| 16 | + }); |
| 17 | + |
| 18 | + expect(result.current.state).toBe(true); |
| 19 | + |
| 20 | + act(() => { |
| 21 | + result.current.onChange({ currentTarget: { value: '' } } as any); |
| 22 | + }); |
| 23 | + |
| 24 | + expect(result.current.state).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should handle arrays of items', () => { |
| 28 | + const { result } = renderHook(() => useCheckboxState({ state: [] })); |
| 29 | + |
| 30 | + act(() => { |
| 31 | + result.current.onChange({ currentTarget: { value: 'oranges' } } as any); |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result.current.state).toBeInstanceOf(Array); |
| 35 | + expect(result.current.state).toEqual(['oranges']); |
| 36 | + |
| 37 | + act(() => { |
| 38 | + result.current.onChange({ currentTarget: { value: 'apples' } } as any); |
| 39 | + }); |
| 40 | + |
| 41 | + expect(result.current.state).toEqual(['oranges', 'apples']); |
| 42 | + |
| 43 | + act(() => { |
| 44 | + result.current.onChange({ currentTarget: { value: 'oranges' } } as any); |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result.current.state).toEqual(['apples']); |
| 48 | + }); |
| 49 | +}); |
0 commit comments