@instructure/ui-modal 8.53.3-snapshot-10 → 8.53.3-snapshot-13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/es/Modal/ModalBody/__new-tests__/ModalBody.test.js +101 -0
  3. package/es/Modal/ModalFooter/__new-tests__/ModalFooter.test.js +55 -0
  4. package/es/Modal/ModalHeader/__new-tests__/ModalHeader.test.js +91 -0
  5. package/es/Modal/__new-tests__/Modal.test.js +369 -0
  6. package/lib/Modal/ModalBody/__new-tests__/ModalBody.test.js +103 -0
  7. package/lib/Modal/ModalFooter/__new-tests__/ModalFooter.test.js +57 -0
  8. package/lib/Modal/ModalHeader/__new-tests__/ModalHeader.test.js +93 -0
  9. package/lib/Modal/__new-tests__/Modal.test.js +371 -0
  10. package/package.json +22 -21
  11. package/src/Modal/ModalBody/__new-tests__/ModalBody.test.tsx +117 -0
  12. package/src/Modal/ModalFooter/__new-tests__/ModalFooter.test.tsx +65 -0
  13. package/src/Modal/ModalHeader/__new-tests__/ModalHeader.test.tsx +107 -0
  14. package/src/Modal/__new-tests__/Modal.test.tsx +457 -0
  15. package/tsconfig.build.json +0 -2
  16. package/tsconfig.build.tsbuildinfo +1 -1
  17. package/types/Modal/ModalBody/__new-tests__/ModalBody.test.d.ts +2 -0
  18. package/types/Modal/ModalBody/__new-tests__/ModalBody.test.d.ts.map +1 -0
  19. package/types/Modal/ModalFooter/__new-tests__/ModalFooter.test.d.ts +2 -0
  20. package/types/Modal/ModalFooter/__new-tests__/ModalFooter.test.d.ts.map +1 -0
  21. package/types/Modal/ModalHeader/__new-tests__/ModalHeader.test.d.ts +2 -0
  22. package/types/Modal/ModalHeader/__new-tests__/ModalHeader.test.d.ts.map +1 -0
  23. package/types/Modal/__new-tests__/Modal.test.d.ts +2 -0
  24. package/types/Modal/__new-tests__/Modal.test.d.ts.map +1 -0
  25. package/es/Modal/ModalLocator.js +0 -46
  26. package/es/Modal/locator.js +0 -27
  27. package/lib/Modal/ModalLocator.js +0 -51
  28. package/lib/Modal/locator.js +0 -37
  29. package/src/Modal/ModalLocator.ts +0 -47
  30. package/src/Modal/locator.ts +0 -28
  31. package/types/Modal/ModalLocator.d.ts +0 -2656
  32. package/types/Modal/ModalLocator.d.ts.map +0 -1
  33. package/types/Modal/locator.d.ts +0 -4
  34. package/types/Modal/locator.d.ts.map +0 -1
@@ -0,0 +1,457 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { render, waitFor } from '@testing-library/react'
27
+ import userEvent from '@testing-library/user-event'
28
+ import '@testing-library/jest-dom'
29
+
30
+ import { Modal, ModalHeader, ModalBody, ModalFooter } from '../index'
31
+ import type { ModalProps } from '../props'
32
+
33
+ describe('<Modal />', () => {
34
+ afterEach(() => {
35
+ jest.clearAllMocks()
36
+ jest.restoreAllMocks()
37
+ })
38
+
39
+ it('should render nothing and have a node with no parent when closed', () => {
40
+ const { container } = render(
41
+ <Modal label="Modal Dialog" shouldReturnFocus={false}>
42
+ <Modal.Body>Foo Bar Baz</Modal.Body>
43
+ </Modal>
44
+ )
45
+ expect(container.firstChild).not.toBeInTheDocument()
46
+ })
47
+
48
+ it('should apply theme overrides when open', async () => {
49
+ const testFont = 'test-font'
50
+ const bodyText = 'Modal-body-text'
51
+ const { findByText, findByRole } = render(
52
+ <Modal
53
+ open
54
+ size="small"
55
+ label="Modal Dialog"
56
+ shouldReturnFocus={false}
57
+ themeOverride={{ fontFamily: testFont }}
58
+ >
59
+ <Modal.Body>{bodyText}</Modal.Body>
60
+ </Modal>
61
+ )
62
+ const modalBody = await findByText(bodyText)
63
+ const dialog = await findByRole('dialog')
64
+ const dialogStyle = window.getComputedStyle(dialog)
65
+
66
+ expect(modalBody).toBeInTheDocument()
67
+ expect(dialogStyle.fontFamily).toBe(testFont)
68
+ })
69
+
70
+ it('should render its own positioning context if constrained to parent', async () => {
71
+ const { findByRole } = render(
72
+ <Modal
73
+ open
74
+ label="Modal Dialog"
75
+ shouldReturnFocus={false}
76
+ constrain="parent"
77
+ >
78
+ <Modal.Body>Foo Bar Baz</Modal.Body>
79
+ </Modal>
80
+ )
81
+ const dialog = await findByRole('dialog')
82
+ const constrain = document.querySelector("[class*='constrainContext']")
83
+
84
+ expect(dialog).toBeInTheDocument()
85
+ expect(constrain).toBeInTheDocument()
86
+ })
87
+
88
+ it("should not inherit its parent's font color", async () => {
89
+ const { findByRole } = render(
90
+ <div style={{ color: 'rgb(255, 255, 255)' }}>
91
+ <Modal
92
+ open
93
+ label="Modal Dialog"
94
+ shouldReturnFocus={false}
95
+ constrain="parent"
96
+ themeOverride={{ textColor: 'rgb(0, 0, 0)' }}
97
+ >
98
+ <Modal.Body>Foo Bar Baz</Modal.Body>
99
+ </Modal>
100
+ </div>
101
+ )
102
+ const dialog = await findByRole('dialog')
103
+ const dialogStyle = window.getComputedStyle(dialog)
104
+
105
+ expect(dialog).toBeInTheDocument()
106
+ expect(dialogStyle.color).toBe('rgb(0, 0, 0)')
107
+ })
108
+
109
+ it('should pass `as` prop to the dialog', async () => {
110
+ const { findByRole, rerender } = render(
111
+ <Modal open label="Modal Dialog" shouldReturnFocus={false}>
112
+ <Modal.Body>Foo Bar Baz</Modal.Body>
113
+ </Modal>
114
+ )
115
+ const dialog = await findByRole('dialog')
116
+
117
+ expect(dialog).toBeInTheDocument()
118
+ expect(dialog.tagName).toBe('SPAN')
119
+
120
+ rerender(
121
+ <Modal as="form" open label="Modal Dialog" shouldReturnFocus={false}>
122
+ <Modal.Body>Foo Bar Baz</Modal.Body>
123
+ </Modal>
124
+ )
125
+ const dialogForm = await findByRole('dialog')
126
+
127
+ expect(dialogForm.tagName).toBe('FORM')
128
+ })
129
+
130
+ it('should handle null children', async () => {
131
+ const bodyText = 'Modal-body-text'
132
+ const { findByText } = render(
133
+ <Modal open label="Modal Dialog" shouldReturnFocus={false}>
134
+ {null}
135
+ <Modal.Body>{bodyText}</Modal.Body>
136
+ {null}
137
+ </Modal>
138
+ )
139
+ const modalBody = await findByText(bodyText)
140
+
141
+ expect(modalBody).toBeInTheDocument()
142
+ })
143
+
144
+ it('should apply the aria attributes', async () => {
145
+ const { findByRole } = render(
146
+ <Modal open label="Modal Dialog" shouldReturnFocus={false}>
147
+ <Modal.Body>Foo Bar Baz</Modal.Body>
148
+ </Modal>
149
+ )
150
+ const dialog = await findByRole('dialog')
151
+
152
+ expect(dialog).toBeInTheDocument()
153
+ expect(dialog).toHaveAttribute('aria-label', 'Modal Dialog')
154
+ })
155
+
156
+ it('should use transition', async () => {
157
+ const onEnter = jest.fn()
158
+ const onEntering = jest.fn()
159
+ const onEntered = jest.fn()
160
+
161
+ const { findByRole } = render(
162
+ <Modal
163
+ open
164
+ onEnter={onEnter}
165
+ onEntering={onEntering}
166
+ onEntered={onEntered}
167
+ transition="fade"
168
+ label="Modal Dialog"
169
+ shouldReturnFocus={false}
170
+ >
171
+ <Modal.Body>Foo Bar Baz</Modal.Body>
172
+ </Modal>
173
+ )
174
+ const dialog = await findByRole('dialog')
175
+
176
+ expect(dialog).toBeInTheDocument()
177
+
178
+ await waitFor(() => {
179
+ expect(onEnter).toHaveBeenCalled()
180
+ expect(onEntering).toHaveBeenCalled()
181
+ expect(onEntered).toHaveBeenCalled()
182
+ })
183
+ })
184
+
185
+ it('should support onOpen prop', async () => {
186
+ const onOpen = jest.fn()
187
+ const { findByRole } = render(
188
+ <Modal
189
+ open
190
+ onOpen={onOpen}
191
+ label="Modal Dialog"
192
+ shouldReturnFocus={false}
193
+ >
194
+ <Modal.Body>Foo Bar Baz</Modal.Body>
195
+ </Modal>
196
+ )
197
+ const dialog = await findByRole('dialog')
198
+
199
+ expect(dialog).toBeInTheDocument()
200
+
201
+ await waitFor(() => {
202
+ expect(onOpen).toHaveBeenCalled()
203
+ })
204
+ })
205
+
206
+ it('should support onClose prop', async () => {
207
+ const onClose = jest.fn()
208
+
209
+ const { findByRole, rerender } = render(
210
+ <Modal
211
+ open
212
+ onClose={onClose}
213
+ label="Modal Dialog"
214
+ shouldReturnFocus={false}
215
+ >
216
+ <Modal.Body>Foo Bar Baz</Modal.Body>
217
+ </Modal>
218
+ )
219
+ const dialog = await findByRole('dialog')
220
+
221
+ expect(dialog).toBeInTheDocument()
222
+
223
+ rerender(
224
+ <Modal
225
+ open={false}
226
+ onClose={onClose}
227
+ label="Modal Dialog"
228
+ shouldReturnFocus={false}
229
+ >
230
+ <Modal.Body>Foo Bar Baz</Modal.Body>
231
+ </Modal>
232
+ )
233
+
234
+ await waitFor(() => {
235
+ expect(onClose).toHaveBeenCalled()
236
+ })
237
+ })
238
+
239
+ it('should dismiss when overlay clicked by default', async () => {
240
+ const onDismiss = jest.fn()
241
+ const { findByText } = render(
242
+ <Modal
243
+ open
244
+ onDismiss={onDismiss}
245
+ label="Modal Dialog"
246
+ shouldReturnFocus={false}
247
+ >
248
+ <Modal.Body>Modal Text</Modal.Body>
249
+ </Modal>
250
+ )
251
+ const modalBody = await findByText('Modal Text')
252
+
253
+ expect(modalBody).toBeInTheDocument()
254
+
255
+ await waitFor(() => {
256
+ userEvent.click(document.body)
257
+ expect(onDismiss).toHaveBeenCalled()
258
+ })
259
+ })
260
+
261
+ it('should NOT dismiss when overlay clicked with shouldCloseOnDocumentClick=false', async () => {
262
+ const onDismiss = jest.fn()
263
+ const onClickOuter = jest.fn()
264
+
265
+ const { findByRole, getByTestId } = render(
266
+ <div>
267
+ <button data-testid="outer-element" onClick={onClickOuter}>
268
+ for dismiss
269
+ </button>
270
+ <Modal
271
+ open
272
+ onDismiss={onDismiss}
273
+ label="Modal Dialog"
274
+ shouldReturnFocus={false}
275
+ shouldCloseOnDocumentClick={false}
276
+ >
277
+ <Modal.Body>
278
+ Foo Bar Baz <button>click me</button>
279
+ </Modal.Body>
280
+ </Modal>
281
+ </div>
282
+ )
283
+ const dialog = await findByRole('dialog')
284
+
285
+ expect(dialog).toBeInTheDocument()
286
+
287
+ userEvent.click(getByTestId('outer-element'))
288
+
289
+ await waitFor(() => {
290
+ expect(onClickOuter).toHaveBeenCalled()
291
+ expect(onDismiss).not.toHaveBeenCalled()
292
+ expect(dialog).toBeInTheDocument()
293
+ })
294
+ })
295
+
296
+ it('should render children', async () => {
297
+ const { findByText } = render(
298
+ <Modal open label="Modal Dialog" shouldReturnFocus={false}>
299
+ <Modal.Body>
300
+ <button>Cancel</button>
301
+ </Modal.Body>
302
+ </Modal>
303
+ )
304
+ const cancelButton = await findByText('Cancel')
305
+
306
+ expect(cancelButton).toBeInTheDocument()
307
+ })
308
+
309
+ describe('children validation', () => {
310
+ it('should pass validation when children are valid', async () => {
311
+ const consoleErrorSpy = jest
312
+ .spyOn(console, 'error')
313
+ .mockImplementation(() => {})
314
+
315
+ const { findByRole } = render(
316
+ <Modal open label="Modal Dialog" shouldReturnFocus={false}>
317
+ <Modal.Header>Hello World</Modal.Header>
318
+ <Modal.Body>Foo Bar Baz</Modal.Body>
319
+ <Modal.Footer>
320
+ <button>Cancel</button>
321
+ </Modal.Footer>
322
+ </Modal>
323
+ )
324
+ const dialog = await findByRole('dialog')
325
+
326
+ expect(dialog).toBeInTheDocument()
327
+ expect(consoleErrorSpy).not.toHaveBeenCalled()
328
+
329
+ consoleErrorSpy.mockRestore()
330
+ })
331
+
332
+ it('should not pass validation when children are invalid', async () => {
333
+ const consoleErrorSpy = jest
334
+ .spyOn(console, 'error')
335
+ .mockImplementation(() => {})
336
+
337
+ const { findByRole } = render(
338
+ <Modal open label="Modal Dialog" shouldReturnFocus={false}>
339
+ <Modal.Body>Foo Bar Baz</Modal.Body>
340
+ <Modal.Footer>
341
+ <button>Cancel</button>
342
+ </Modal.Footer>
343
+ <Modal.Header>Hello World</Modal.Header>
344
+ </Modal>
345
+ )
346
+ const dialog = await findByRole('dialog')
347
+ const expectedErrorMessage =
348
+ 'Expected children of Modal in one of the following formats:'
349
+
350
+ expect(dialog).toBeInTheDocument()
351
+ expect(consoleErrorSpy).toHaveBeenCalledWith(
352
+ expect.any(String),
353
+ expect.any(String),
354
+ expect.stringContaining(expectedErrorMessage),
355
+ expect.any(String)
356
+ )
357
+
358
+ consoleErrorSpy.mockRestore()
359
+ })
360
+
361
+ it('should pass inverse variant to children when set', async () => {
362
+ let headerRef: ModalHeader | null = null
363
+ let bodyRef: ModalBody | null = null
364
+ let footerRef: ModalFooter | null = null
365
+
366
+ const { findByRole } = render(
367
+ <Modal
368
+ open
369
+ label="Dark Modal"
370
+ shouldReturnFocus={false}
371
+ variant="inverse"
372
+ >
373
+ <Modal.Header ref={(el) => (headerRef = el)}>header</Modal.Header>
374
+ <Modal.Body ref={(el) => (bodyRef = el)}>body</Modal.Body>
375
+ <Modal.Footer ref={(el) => (footerRef = el)}>footer</Modal.Footer>
376
+ </Modal>
377
+ )
378
+ const dialog = await findByRole('dialog')
379
+
380
+ expect(dialog).toBeInTheDocument()
381
+ expect(headerRef!.props.variant).toBe('inverse')
382
+ expect(bodyRef!.props.variant).toBe('inverse')
383
+ expect(footerRef!.props.variant).toBe('inverse')
384
+ })
385
+
386
+ it('should pass overflow to Modal.Body', async () => {
387
+ let bodyRef: ModalBody | null = null
388
+
389
+ const { findByRole } = render(
390
+ <Modal open label="Modal" shouldReturnFocus={false} overflow="fit">
391
+ <Modal.Body ref={(el) => (bodyRef = el)}>body</Modal.Body>
392
+ </Modal>
393
+ )
394
+ const dialog = await findByRole('dialog')
395
+
396
+ expect(dialog).toBeInTheDocument()
397
+ expect(bodyRef!.props.overflow).toBe('fit')
398
+ })
399
+ })
400
+
401
+ describe('managed focus', () => {
402
+ class ModalExample extends React.Component<Partial<ModalProps>> {
403
+ static propTypes = {
404
+ // eslint-disable-next-line react/forbid-foreign-prop-types
405
+ ...Modal.propTypes
406
+ }
407
+
408
+ render() {
409
+ const { label, ...props } = this.props
410
+
411
+ return (
412
+ <div>
413
+ <input type="text" />
414
+ <Modal label={label!} {...props}>
415
+ <Modal.Header>
416
+ <button>Close</button>
417
+ </Modal.Header>
418
+ <Modal.Body>
419
+ <input type="text" id="input-one" data-testid="input-first" />
420
+ <input type="text" id="input-two" data-testid="input-second" />
421
+ </Modal.Body>
422
+ <Modal.Footer>
423
+ <button>Cancel</button>
424
+ </Modal.Footer>
425
+ </Modal>
426
+ </div>
427
+ )
428
+ }
429
+ }
430
+
431
+ it('should focus closeButton by default', async () => {
432
+ const { findByText } = render(<ModalExample open label="A Modal" />)
433
+ const closeButton = await findByText('Close')
434
+
435
+ expect(closeButton).toBeInTheDocument()
436
+
437
+ await waitFor(() => {
438
+ expect(document.activeElement).toBe(closeButton)
439
+ })
440
+ })
441
+
442
+ it('should take a prop for finding default focus', async () => {
443
+ const { findByTestId } = render(
444
+ <ModalExample
445
+ open
446
+ label="A Modal"
447
+ defaultFocusElement={() => document.getElementById('input-one')}
448
+ />
449
+ )
450
+ const input = await findByTestId('input-first')
451
+
452
+ await waitFor(() => {
453
+ expect(input).toHaveFocus()
454
+ })
455
+ })
456
+ })
457
+ })
@@ -24,8 +24,6 @@
24
24
  { "path": "../ui-babel-preset/tsconfig.build.json" },
25
25
  { "path": "../ui-color-utils/tsconfig.build.json" },
26
26
  { "path": "../ui-position/tsconfig.build.json" },
27
- { "path": "../ui-test-locator/tsconfig.build.json" },
28
- { "path": "../ui-test-utils/tsconfig.build.json" },
29
27
  { "path": "../ui-themes/tsconfig.build.json" }
30
28
  ]
31
29
  }