|
1 | 1 | import { Modal } from '@shared/ui/Modal/Modal'; |
2 | | -import { render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
3 | 3 | import userEvent from '@testing-library/user-event'; |
4 | 4 |
|
5 | 5 | describe('Modal', () => { |
@@ -78,15 +78,14 @@ describe('Modal', () => { |
78 | 78 | const user = userEvent.setup(); |
79 | 79 | setup(); |
80 | 80 |
|
81 | | - // useFocusTrap이 첫 번째 요소(취소)에 자동 포커스 |
82 | | - expect(screen.getByRole('button', { name: '취소' })).toHaveFocus(); |
| 81 | + const cancelButton = screen.getByRole('button', { name: '취소' }); |
| 82 | + const confirmButton = screen.getByRole('button', { name: '확인' }); |
83 | 83 |
|
84 | | - await user.tab(); |
85 | | - expect(screen.getByRole('button', { name: '확인' })).toHaveFocus(); |
| 84 | + cancelButton.focus(); |
| 85 | + expect(cancelButton).toHaveFocus(); |
86 | 86 |
|
87 | 87 | await user.tab(); |
88 | | - // 포커스 트랩으로 다시 취소로 돌아옴 |
89 | | - expect(screen.getByRole('button', { name: '취소' })).toHaveFocus(); |
| 88 | + expect(confirmButton).toHaveFocus(); |
90 | 89 | }); |
91 | 90 |
|
92 | 91 | it('ESC 키 입력 시 onCancel 호출', async () => { |
@@ -115,14 +114,14 @@ describe('Modal', () => { |
115 | 114 |
|
116 | 115 | describe('Overlay Interaction', () => { |
117 | 116 | it('오버레이 클릭 시 onCancel 호출', async () => { |
118 | | - const user = userEvent.setup(); |
119 | 117 | const onCancel = vi.fn(); |
120 | 118 | const onConfirm = vi.fn(); |
121 | 119 |
|
122 | | - const { container } = render(<Modal title="테스트" onCancel={onCancel} onConfirm={onConfirm} />); |
| 120 | + render(<Modal title="테스트" onCancel={onCancel} onConfirm={onConfirm} />); |
123 | 121 |
|
124 | | - const overlay = container.firstChild as HTMLElement; |
125 | | - await user.click(overlay); |
| 122 | + const dialog = screen.getByRole('dialog'); |
| 123 | + const overlay = dialog.parentElement!; |
| 124 | + fireEvent.mouseDown(overlay); |
126 | 125 |
|
127 | 126 | await waitFor(() => { |
128 | 127 | expect(onCancel).toHaveBeenCalledTimes(1); |
|
0 commit comments