|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { render, screen } from '@testing-library/react' |
| 3 | +import userEvent from '@testing-library/user-event' |
| 4 | +import { ConfirmDialog } from './ConfirmDialog' |
| 5 | + |
| 6 | +const base = { |
| 7 | + title: '삭제하시겠습니까?', |
| 8 | + description: '되돌릴 수 없습니다.', |
| 9 | + onConfirm: () => {}, |
| 10 | + onCancel: () => {}, |
| 11 | +} |
| 12 | + |
| 13 | +describe('ConfirmDialog', () => { |
| 14 | + it('open=false면 렌더되지 않는다', () => { |
| 15 | + render(<ConfirmDialog {...base} open={false} />) |
| 16 | + expect(screen.queryByText('삭제하시겠습니까?')).toBeNull() |
| 17 | + }) |
| 18 | + |
| 19 | + it('open이면 제목·설명을 보여준다', () => { |
| 20 | + render(<ConfirmDialog {...base} open />) |
| 21 | + expect(screen.getByText('삭제하시겠습니까?')).toBeTruthy() |
| 22 | + expect(screen.getByText('되돌릴 수 없습니다.')).toBeTruthy() |
| 23 | + }) |
| 24 | + |
| 25 | + it('확인/취소 버튼이 각각 콜백을 호출한다', async () => { |
| 26 | + const onConfirm = vi.fn() |
| 27 | + const onCancel = vi.fn() |
| 28 | + render( |
| 29 | + <ConfirmDialog |
| 30 | + {...base} |
| 31 | + open |
| 32 | + confirmLabel="삭제" |
| 33 | + cancelLabel="취소" |
| 34 | + onConfirm={onConfirm} |
| 35 | + onCancel={onCancel} |
| 36 | + />, |
| 37 | + ) |
| 38 | + await userEvent.click(screen.getByRole('button', { name: '삭제' })) |
| 39 | + expect(onConfirm).toHaveBeenCalledOnce() |
| 40 | + await userEvent.click(screen.getByRole('button', { name: '취소' })) |
| 41 | + expect(onCancel).toHaveBeenCalledOnce() |
| 42 | + }) |
| 43 | + |
| 44 | + it('loading이면 취소가 비활성화되고 확인은 막힌다', async () => { |
| 45 | + const onConfirm = vi.fn() |
| 46 | + render( |
| 47 | + <ConfirmDialog |
| 48 | + {...base} |
| 49 | + open |
| 50 | + loading |
| 51 | + confirmLabel="삭제" |
| 52 | + cancelLabel="취소" |
| 53 | + onConfirm={onConfirm} |
| 54 | + />, |
| 55 | + ) |
| 56 | + expect(screen.getByRole('button', { name: '취소' })).toBeDisabled() |
| 57 | + // loading 시 Spinner가 접근성 이름에 더해질 수 있어 부분 일치로 찾는다. |
| 58 | + const confirm = screen.getByRole('button', { name: /삭제/ }) |
| 59 | + expect(confirm).toBeDisabled() |
| 60 | + await userEvent.click(confirm) |
| 61 | + expect(onConfirm).not.toHaveBeenCalled() |
| 62 | + }) |
| 63 | +}) |
0 commit comments