|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 2 | +import { useModal } from './useModal'; |
| 3 | + |
| 4 | +describe('useModal', () => { |
| 5 | + it('onOpen: without data', () => { |
| 6 | + const { result } = renderHook(() => useModal()); |
| 7 | + |
| 8 | + // Open modal |
| 9 | + const { open } = result.current; |
| 10 | + act(() => open()); |
| 11 | + expect(result.current.isOpen).toEqual(true); |
| 12 | + expect(result.current.data).toBeUndefined(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('onOpen: with data', () => { |
| 16 | + const ENTITY = 'hello'; |
| 17 | + |
| 18 | + const { result } = renderHook(() => useModal<string>()); |
| 19 | + |
| 20 | + // Open modal |
| 21 | + const { open } = result.current; |
| 22 | + act(() => open(ENTITY)); |
| 23 | + |
| 24 | + expect(result.current.isOpen).toEqual(true); |
| 25 | + expect(result.current.data).toEqual(ENTITY); |
| 26 | + }); |
| 27 | + |
| 28 | + it('Close modal with data', () => { |
| 29 | + const ENTITY = 'hello'; |
| 30 | + |
| 31 | + const { result } = renderHook(() => useModal<string>()); |
| 32 | + const { open, close } = result.current; |
| 33 | + |
| 34 | + // Open modal |
| 35 | + act(() => open(ENTITY)); |
| 36 | + |
| 37 | + expect(result.current.isOpen).toEqual(true); |
| 38 | + expect(result.current.data).toEqual(ENTITY); |
| 39 | + |
| 40 | + // Close modal |
| 41 | + act(() => close()); |
| 42 | + |
| 43 | + expect(result.current.isOpen).toEqual(false); |
| 44 | + expect(result.current.data).toBeUndefined(); |
| 45 | + }); |
| 46 | +}); |
0 commit comments