-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathModal.test.tsx
More file actions
153 lines (128 loc) · 4.41 KB
/
Modal.test.tsx
File metadata and controls
153 lines (128 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React, { useCallback, useRef } from 'react'
import Button from '../Button'
import Modal from './'
import './dialog.mock'
const TestModal = ({ state }: { state: boolean }) => {
const [open, setOpen] = React.useState(state)
const toggleModal = () => setOpen((o) => !o)
return (
<>
<Button onClick={toggleModal}>Open Sesame</Button>
<Modal open={open}>
<Modal.Header className="font-bold">
These are not the droids you're looking for
</Modal.Header>
<Button onClick={toggleModal}>Do not click me</Button>
</Modal>
</>
)
}
describe('Modal', () => {
it('should not show modal', () => {
render(
<Modal>
{' '}
<Modal.Header className="font-bold">
These are not the droids you're looking for
</Modal.Header>
<Button>Do not click me</Button>
</Modal>
)
expect(
screen.queryByRole('button', { name: 'Do not click me' })
).not.toBeInTheDocument()
})
it('should show modal', () => {
render(
<Modal open>
{' '}
<Modal.Header className="font-bold">
These are not the droids you're looking for
</Modal.Header>
<Button>Do not click me</Button>
</Modal>
)
expect(
screen.getByRole('button', { name: 'Do not click me' })
).toBeInTheDocument()
})
it('should open modal with button', async () => {
const user = userEvent.setup()
render(<TestModal state={false} />)
expect(
screen.getByRole('button', { name: 'Open Sesame' })
).toBeInTheDocument()
expect(
screen.queryByRole('button', { name: 'Do not click me' })
).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Open Sesame' }))
expect(
screen.getByRole('button', { name: 'Do not click me' })
).toBeInTheDocument()
})
it('should close modal with button', async () => {
const user = userEvent.setup()
render(<TestModal state={true} />)
expect(
screen.getByRole('button', { name: 'Do not click me' })
).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Do not click me' }))
expect(
screen.queryByRole('button', { name: 'Do not click me' })
).not.toBeInTheDocument()
})
describe('dialog ref handlers', () => {
const TestDialogWithHandlers = () => {
const ref = useRef<HTMLDialogElement>(null)
const handleShow = useCallback(() => {
ref.current?.showModal()
}, [ref])
const handleClose = useCallback(() => {
ref.current?.close()
}, [ref])
return (
<div className="font-sans">
<Button onClick={handleShow}>Open Modal</Button>
<Modal ref={ref}>
<Modal.Header className="font-bold">Hello!</Modal.Header>
<Modal.Body>
Press ESC key or click the button below to close
</Modal.Body>
<Modal.Actions>
<Button onClick={handleClose}>Close</Button>
</Modal.Actions>
</Modal>
</div>
)
}
it('should show modal with button', async () => {
const user = userEvent.setup()
render(<TestDialogWithHandlers />)
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
const openButton = screen.getByRole('button', { name: 'Open Modal' })
expect(openButton).toBeInTheDocument()
await user.click(openButton)
const dialog = await screen.findByRole('dialog')
expect(dialog).toBeInTheDocument()
const closeButton = screen.getByRole('button', { name: 'Close' })
expect(closeButton).toBeInTheDocument()
await user.click(closeButton)
await waitFor(() => {
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
})
})
it('should handle cancel event when triggered manually', async () => {
const user = userEvent.setup()
render(<TestDialogWithHandlers />)
const openButton = screen.getByRole('button', { name: 'Open Modal' })
await user.click(openButton)
const dialog = await screen.findByRole<HTMLDialogElement>('dialog')
expect(dialog).toBeInTheDocument()
// Manually trigger the cancel event (simulating ESC key)
await dialog.triggerCancel()
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
})
})
})