|
| 1 | +import { Modal } from '@shared/ui/Modal/Modal'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | + |
| 5 | +describe('Modal', () => { |
| 6 | + const setup = (props?: Partial<React.ComponentProps<typeof Modal>>) => { |
| 7 | + const onCancel = vi.fn(); |
| 8 | + const onConfirm = vi.fn(); |
| 9 | + |
| 10 | + render( |
| 11 | + <Modal |
| 12 | + title="해당 게시물을 삭제하시겠어요?" |
| 13 | + subtitle="subtitle 1줄" |
| 14 | + onCancel={onCancel} |
| 15 | + onConfirm={onConfirm} |
| 16 | + {...props} |
| 17 | + /> |
| 18 | + ); |
| 19 | + |
| 20 | + return { onCancel, onConfirm }; |
| 21 | + }; |
| 22 | + |
| 23 | + describe('Rendering', () => { |
| 24 | + it('title 렌더링', () => { |
| 25 | + setup(); |
| 26 | + expect(screen.getByText('해당 게시물을 삭제하시겠어요?')).toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('subtitle 렌더링', () => { |
| 30 | + setup(); |
| 31 | + expect(screen.getByText('subtitle 1줄')).toBeInTheDocument(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('subtitle이 없을 경우 렌더링되지 않음', () => { |
| 35 | + render(<Modal title="제목만 있는 모달" onCancel={vi.fn()} onConfirm={vi.fn()} />); |
| 36 | + expect(screen.queryByText('subtitle 1줄')).not.toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('취소 / 확인 버튼 렌더링', () => { |
| 40 | + setup(); |
| 41 | + expect(screen.getByRole('button', { name: '취소' })).toBeInTheDocument(); |
| 42 | + expect(screen.getByRole('button', { name: '확인' })).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Interaction', () => { |
| 47 | + it('취소 버튼 클릭 시 onCancel 호출', async () => { |
| 48 | + const user = userEvent.setup(); |
| 49 | + const { onCancel } = setup(); |
| 50 | + |
| 51 | + await user.click(screen.getByRole('button', { name: '취소' })); |
| 52 | + |
| 53 | + await waitFor(() => { |
| 54 | + expect(onCancel).toHaveBeenCalledTimes(1); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('확인 버튼 클릭 시 onConfirm 호출', async () => { |
| 59 | + const user = userEvent.setup(); |
| 60 | + const { onConfirm } = setup(); |
| 61 | + |
| 62 | + await user.click(screen.getByRole('button', { name: '확인' })); |
| 63 | + |
| 64 | + await waitFor(() => { |
| 65 | + expect(onConfirm).toHaveBeenCalledTimes(1); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('Accessibility', () => { |
| 71 | + it('버튼은 접근 가능한 role을 가짐', () => { |
| 72 | + setup(); |
| 73 | + expect(screen.getAllByRole('button')).toHaveLength(2); |
| 74 | + }); |
| 75 | + |
| 76 | + it('Tab 키로 버튼 포커스 이동 가능', async () => { |
| 77 | + const user = userEvent.setup(); |
| 78 | + setup(); |
| 79 | + |
| 80 | + await user.tab(); |
| 81 | + expect(screen.getByRole('button', { name: '취소' })).toHaveFocus(); |
| 82 | + |
| 83 | + await user.tab(); |
| 84 | + expect(screen.getByRole('button', { name: '확인' })).toHaveFocus(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments