|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { fireEvent, render, waitFor } from "@testing-library/react" |
|
4
|
import { ButtonProps } from "../../Button" |
|
5
|
|
|
6
|
// mock functions |
|
7
|
const mockCloseModal = vi.fn() |
|
8
|
const mockUseKeyboardShortcut = vi.fn() |
|
9
|
const mockCloseDialog = vi.fn() |
|
10
|
|
|
11
|
// mock components |
|
12
|
vi.mock("@/providers/Modal", () => ({ |
|
13
|
useModal: () => ({ closeModal: mockCloseModal }), |
|
14
|
})) |
|
15
|
|
|
16
|
vi.mock("@/hooks/use-keyboard-shortcut", () => ({ |
|
17
|
useKeyboardShortcut: (options: unknown) => mockUseKeyboardShortcut(options), |
|
18
|
})) |
|
19
|
|
|
20
|
vi.mock("@/components/Modal/Header", () => ({ |
|
21
|
ModalHeader: ({ title }: { title?: React.ReactNode }) => ( |
|
22
|
<div data-testid="modal-header">{title}</div> |
|
23
|
), |
|
24
|
})) |
|
25
|
|
|
26
|
vi.mock("@/components/Modal/Footer", () => ({ |
|
27
|
ModalFooter: ({ |
|
28
|
actions, |
|
29
|
children, |
|
30
|
}: { |
|
31
|
actions?: ButtonProps[] |
|
32
|
children?: React.ReactNode |
|
33
|
}) => ( |
|
34
|
<div data-testid="modal-footer"> |
|
35
|
{actions?.map((action, index) => ( |
|
36
|
<button key={index} data-testid="footer-action"> |
|
37
|
{action.children} |
|
38
|
</button> |
|
39
|
))} |
|
40
|
{children} |
|
41
|
</div> |
|
42
|
), |
|
43
|
})) |
|
44
|
|
|
45
|
import { Modal } from "../../Modal" |
|
46
|
|
|
47
|
beforeEach(() => { |
|
48
|
vi.clearAllMocks() |
|
49
|
// Reset body attribute |
|
50
|
document.body.removeAttribute("data-modal") |
|
51
|
// close method on modals doesn't work in tests, so we need to mock it |
|
52
|
window.HTMLDialogElement.prototype.close = mockCloseDialog |
|
53
|
}) |
|
54
|
|
|
55
|
describe("rendering", () => { |
|
56
|
test("renders modal dialog", () => { |
|
57
|
const { container } = render(<Modal>Content</Modal>) |
|
58
|
const dialog = container.querySelector("dialog") |
|
59
|
expect(dialog).toBeInTheDocument() |
|
60
|
expect(dialog).toHaveTextContent("Content") |
|
61
|
}) |
|
62
|
|
|
63
|
test("dialog is the overlay and sits above every other layer", () => { |
|
64
|
const { container } = render(<Modal>Content</Modal>) |
|
65
|
const dialog = container.querySelector("dialog") |
|
66
|
expect(dialog).toHaveClass("bg-medusa-bg-overlay") |
|
67
|
expect(dialog).toHaveClass("z-[499]") |
|
68
|
}) |
|
69
|
|
|
70
|
test("renders with title", () => { |
|
71
|
const { container } = render(<Modal title="Modal Title">Content</Modal>) |
|
72
|
const header = container.querySelector("[data-testid='modal-header']") |
|
73
|
expect(header).toBeInTheDocument() |
|
74
|
expect(header).toHaveTextContent("Modal Title") |
|
75
|
}) |
|
76
|
|
|
77
|
test("does not render header when title is not provided", () => { |
|
78
|
const { container } = render(<Modal>Content</Modal>) |
|
79
|
const header = container.querySelector("[data-testid='modal-header']") |
|
80
|
expect(header).not.toBeInTheDocument() |
|
81
|
}) |
|
82
|
|
|
83
|
test("renders footer with actions", () => { |
|
84
|
const actions: ButtonProps[] = [ |
|
85
|
{ children: "Cancel" }, |
|
86
|
{ children: "Save" }, |
|
87
|
] |
|
88
|
const { container } = render(<Modal actions={actions}>Content</Modal>) |
|
89
|
const footer = container.querySelector("[data-testid='modal-footer']") |
|
90
|
expect(footer).toBeInTheDocument() |
|
91
|
const footerActions = container.querySelectorAll( |
|
92
|
"[data-testid='footer-action']" |
|
93
|
) |
|
94
|
expect(footerActions).toHaveLength(2) |
|
95
|
expect(footerActions[0]).toHaveTextContent("Cancel") |
|
96
|
expect(footerActions[1]).toHaveTextContent("Save") |
|
97
|
}) |
|
98
|
|
|
99
|
test("renders footer with footerContent", () => { |
|
100
|
const { container } = render( |
|
101
|
<Modal footerContent={<div>Custom Footer</div>}>Content</Modal> |
|
102
|
) |
|
103
|
const footer = container.querySelector("[data-testid='modal-footer']") |
|
104
|
expect(footer).toBeInTheDocument() |
|
105
|
expect(footer).toHaveTextContent("Custom Footer") |
|
106
|
}) |
|
107
|
|
|
108
|
test("does not render footer when actions and footerContent are not provided", () => { |
|
109
|
const { container } = render(<Modal>Content</Modal>) |
|
110
|
const footer = container.querySelector("[data-testid='modal-footer']") |
|
111
|
expect(footer).not.toBeInTheDocument() |
|
112
|
}) |
|
113
|
|
|
114
|
test("does not render footer when actions array is empty", () => { |
|
115
|
const { container } = render(<Modal actions={[]}>Content</Modal>) |
|
116
|
const footer = container.querySelector("[data-testid='modal-footer']") |
|
117
|
expect(footer).not.toBeInTheDocument() |
|
118
|
}) |
|
119
|
|
|
120
|
test("applies custom className", () => { |
|
121
|
const { container } = render( |
|
122
|
<Modal className="custom-class">Content</Modal> |
|
123
|
) |
|
124
|
const dialog = container.querySelector("dialog") |
|
125
|
expect(dialog).toHaveClass("custom-class") |
|
126
|
}) |
|
127
|
|
|
128
|
test("applies custom modalContainerClassName", () => { |
|
129
|
const { container } = render( |
|
130
|
<Modal modalContainerClassName="custom-container">Content</Modal> |
|
131
|
) |
|
132
|
const containerDiv = container.querySelector( |
|
133
|
"[data-testid='modal-container']" |
|
134
|
) |
|
135
|
expect(containerDiv).toHaveClass("custom-container") |
|
136
|
}) |
|
137
|
|
|
138
|
test("applies custom contentClassName", () => { |
|
139
|
const { container } = render( |
|
140
|
<Modal contentClassName="custom-content">Content</Modal> |
|
141
|
) |
|
142
|
const contentDiv = container.querySelector("[data-testid='modal-content']") |
|
143
|
expect(contentDiv).toHaveClass("custom-content") |
|
144
|
}) |
|
145
|
}) |
|
146
|
|
|
147
|
describe("open state", () => { |
|
148
|
test("renders as open by default", () => { |
|
149
|
const { container } = render(<Modal>Content</Modal>) |
|
150
|
const dialog = container.querySelector("dialog") |
|
151
|
expect(dialog).toHaveAttribute("open") |
|
152
|
}) |
|
153
|
|
|
154
|
test("renders as open when open prop is true", () => { |
|
155
|
const { container } = render(<Modal open={true}>Content</Modal>) |
|
156
|
const dialog = container.querySelector("dialog") |
|
157
|
expect(dialog).toHaveAttribute("open") |
|
158
|
}) |
|
159
|
|
|
160
|
test("does not render as open when open prop is false", () => { |
|
161
|
const { container } = render(<Modal open={false}>Content</Modal>) |
|
162
|
const dialog = container.querySelector("dialog") |
|
163
|
expect(dialog).not.toHaveAttribute("open") |
|
164
|
}) |
|
165
|
|
|
166
|
test("sets data-modal attribute on body when open", () => { |
|
167
|
render(<Modal open={true}>Content</Modal>) |
|
168
|
expect(document.body).toHaveAttribute("data-modal", "opened") |
|
169
|
}) |
|
170
|
|
|
171
|
test("removes data-modal attribute from body when closed", () => { |
|
172
|
const { rerender } = render(<Modal open={true}>Content</Modal>) |
|
173
|
expect(document.body).toHaveAttribute("data-modal", "opened") |
|
174
|
rerender(<Modal open={false}>Content</Modal>) |
|
175
|
expect(document.body).not.toHaveAttribute("data-modal") |
|
176
|
}) |
|
177
|
}) |
|
178
|
|
|
179
|
describe("interactions", () => { |
|
180
|
test("calls closeModal and onClose when clicking outside content", () => { |
|
181
|
const mockOnClose = vi.fn() |
|
182
|
const { container } = render(<Modal onClose={mockOnClose}>Content</Modal>) |
|
183
|
const dialog = container.querySelector("dialog") |
|
184
|
// Simulate click on dialog backdrop (the dialog element itself) |
|
185
|
fireEvent.click(dialog!) |
|
186
|
expect(mockCloseModal).toHaveBeenCalledTimes(1) |
|
187
|
expect(mockOnClose).toHaveBeenCalledTimes(1) |
|
188
|
}) |
|
189
|
|
|
190
|
test("does not close when clicking inside content", () => { |
|
191
|
const mockOnClose = vi.fn() |
|
192
|
const { container } = render(<Modal onClose={mockOnClose}>Content</Modal>) |
|
193
|
const contentDiv = container.querySelector("[data-testid='modal-content']") |
|
194
|
fireEvent.click(contentDiv!) |
|
195
|
expect(mockCloseModal).not.toHaveBeenCalled() |
|
196
|
expect(mockOnClose).not.toHaveBeenCalled() |
|
197
|
}) |
|
198
|
|
|
199
|
test("calls closeModal and onClose when dialog closes", async () => { |
|
200
|
const mockOnClose = vi.fn() |
|
201
|
const { container } = render(<Modal onClose={mockOnClose}>Content</Modal>) |
|
202
|
const dialog = container.querySelector("dialog") |
|
203
|
// click dialog to close |
|
204
|
fireEvent.click(dialog!) |
|
205
|
await waitFor(() => { |
|
206
|
expect(mockCloseModal).toHaveBeenCalledTimes(1) |
|
207
|
expect(mockOnClose).toHaveBeenCalledTimes(1) |
|
208
|
}) |
|
209
|
}) |
|
210
|
|
|
211
|
test("calls closeModal when dialog closes even without onClose prop", async () => { |
|
212
|
const { container } = render(<Modal>Content</Modal>) |
|
213
|
const dialog = container.querySelector("dialog") |
|
214
|
// click dialog to close |
|
215
|
fireEvent.click(dialog!) |
|
216
|
await waitFor(() => { |
|
217
|
expect(mockCloseModal).toHaveBeenCalledTimes(1) |
|
218
|
}) |
|
219
|
}) |
|
220
|
}) |
|
221
|
|
|
222
|
describe("keyboard shortcuts", () => { |
|
223
|
test("sets up escape key shortcut", () => { |
|
224
|
render(<Modal open={true}>Content</Modal>) |
|
225
|
expect(mockUseKeyboardShortcut).toHaveBeenCalledWith({ |
|
226
|
metakey: false, |
|
227
|
checkEditing: false, |
|
228
|
shortcutKeys: ["escape"], |
|
229
|
action: expect.any(Function), |
|
230
|
}) |
|
231
|
}) |
|
232
|
|
|
233
|
test("closes dialog when escape key is pressed and modal is open", () => { |
|
234
|
render(<Modal open={true}>Content</Modal>) |
|
235
|
// Get the action function from the last call |
|
236
|
const lastCall = |
|
237
|
mockUseKeyboardShortcut.mock.calls[ |
|
238
|
mockUseKeyboardShortcut.mock.calls.length - 1 |
|
239
|
] |
|
240
|
const action = lastCall[0].action |
|
241
|
// Simulate escape key press |
|
242
|
action() |
|
243
|
expect(mockCloseDialog).toHaveBeenCalledTimes(1) |
|
244
|
}) |
|
245
|
|
|
246
|
test("does not close dialog when escape key is pressed and modal is closed", () => { |
|
247
|
render(<Modal open={false}>Content</Modal>) |
|
248
|
const lastCall = |
|
249
|
mockUseKeyboardShortcut.mock.calls[ |
|
250
|
mockUseKeyboardShortcut.mock.calls.length - 1 |
|
251
|
] |
|
252
|
const action = lastCall[0].action |
|
253
|
action() |
|
254
|
// Dialog should remain closed |
|
255
|
expect(mockCloseDialog).not.toHaveBeenCalled() |
|
256
|
}) |
|
257
|
}) |
|
258
|
|
|
259
|
describe("ref handling", () => { |
|
260
|
test("supports function ref", () => { |
|
261
|
const refCallback = vi.fn() |
|
262
|
render(<Modal passedRef={refCallback}>Content</Modal>) |
|
263
|
expect(refCallback).toHaveBeenCalled() |
|
264
|
}) |
|
265
|
|
|
266
|
test("supports object ref", () => { |
|
267
|
const ref = React.createRef<HTMLDialogElement>() |
|
268
|
render(<Modal passedRef={ref}>Content</Modal>) |
|
269
|
expect(ref.current).toBeInstanceOf(HTMLDialogElement) |
|
270
|
}) |
|
271
|
}) |
|
272
|
|
|
273
|
describe("dialog props", () => { |
|
274
|
test("passes through dialog props", () => { |
|
275
|
const { container } = render(<Modal aria-label="Test Modal">Content</Modal>) |
|
276
|
const dialog = container.querySelector("dialog") |
|
277
|
expect(dialog).toHaveAttribute("aria-label", "Test Modal") |
|
278
|
}) |
|
279
|
}) |