-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Dialog.test.js
332 lines (287 loc) · 9.55 KB
/
Dialog.test.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { describeConformance, act, createRenderer, fireEvent, screen } from 'test/utils';
import Modal from '@mui/material/Modal';
import Dialog, { dialogClasses as classes } from '@mui/material/Dialog';
import { ThemeProvider, createTheme } from '@mui/material/styles';
/**
* more comprehensive simulation of a user click (mousedown + click)
* @param {HTMLElement} element
*/
function userClick(element) {
act(() => {
fireEvent.mouseDown(element);
fireEvent.mouseUp(element);
element.click();
});
}
/**
* @param {typeof import('test/utils').screen} view
*/
function findBackdrop(view) {
return view.getByRole('dialog').parentElement;
}
/**
* @param {typeof import('test/utils').screen} view
*/
function clickBackdrop(view) {
userClick(findBackdrop(view));
}
describe('<Dialog />', () => {
const { clock, render } = createRenderer({ clock: 'fake' });
describeConformance(
<Dialog open disablePortal>
foo
</Dialog>,
() => ({
classes,
inheritComponent: Modal,
muiName: 'MuiDialog',
render,
testVariantProps: { variant: 'foo' },
testDeepOverrides: { slotName: 'paper', slotClassName: classes.paper },
refInstanceof: window.HTMLDivElement,
skip: [
'componentProp',
'componentsProp',
'themeVariants',
// react-transition-group issue
'reactTestRenderer',
],
}),
);
it('should render with a TransitionComponent', () => {
const Transition = React.forwardRef(() => <div data-testid="Transition" tabIndex={-1} />);
const { getAllByTestId } = render(
<Dialog open TransitionComponent={Transition}>
foo
</Dialog>,
);
expect(getAllByTestId('Transition')).to.have.lengthOf(1);
});
it('calls onClose when pressing Esc and removes the content after the specified duration', () => {
const onClose = spy();
function TestCase() {
const [open, close] = React.useReducer(() => false, true);
const handleClose = (...args) => {
close();
onClose(...args);
};
return (
<Dialog open={open} transitionDuration={100} onClose={handleClose}>
foo
</Dialog>
);
}
const { getByRole, queryByRole } = render(<TestCase />);
const dialog = getByRole('dialog');
expect(dialog).not.to.equal(null);
act(() => {
dialog.click();
});
// keyDown not targetted at anything specific
// eslint-disable-next-line material-ui/disallow-active-element-as-key-event-target
fireEvent.keyDown(document.activeElement, { key: 'Esc' });
expect(onClose.calledOnce).to.equal(true);
clock.tick(100);
expect(queryByRole('dialog')).to.equal(null);
});
it('can ignore backdrop click and Esc keydown', () => {
function DialogWithBackdropClickDisabled(props) {
const { onClose, ...other } = props;
function handleClose(event, reason) {
if (reason !== 'backdropClick') {
onClose(event, reason);
}
}
return <Dialog onClose={handleClose} {...other} />;
}
const onClose = spy();
const { getByRole } = render(
<DialogWithBackdropClickDisabled
open
disableEscapeKeyDown
onClose={onClose}
transitionDuration={0}
>
foo
</DialogWithBackdropClickDisabled>,
);
const dialog = getByRole('dialog');
expect(dialog).not.to.equal(null);
act(() => {
dialog.click();
// keyDown is not targetted at anything specific.
// eslint-disable-next-line material-ui/disallow-active-element-as-key-event-target
fireEvent.keyDown(document.activeElement, { key: 'Esc' });
});
expect(onClose.callCount).to.equal(0);
clickBackdrop(screen);
expect(onClose.callCount).to.equal(0);
});
describe('backdrop', () => {
it('does have `role` `presentation`', () => {
render(<Dialog open>foo</Dialog>);
expect(findBackdrop(screen)).to.have.attribute('role', 'presentation');
});
it('calls onBackdropClick and onClose when clicked', () => {
const onBackdropClick = spy();
const onClose = spy();
render(
<Dialog onBackdropClick={onBackdropClick} onClose={onClose} open>
foo
</Dialog>,
);
clickBackdrop(screen);
expect(onBackdropClick.callCount).to.equal(1);
expect(onClose.callCount).to.equal(1);
});
it('should ignore the backdrop click if the event did not come from the backdrop', () => {
const onBackdropClick = spy();
const { getByRole } = render(
<Dialog onBackdropClick={onBackdropClick} open>
<div tabIndex={-1}>
<h2>my dialog</h2>
</div>
</Dialog>,
);
userClick(getByRole('heading'));
expect(onBackdropClick.callCount).to.equal(0);
});
it('should not close if the target changes between the mousedown and the click', () => {
const { getByRole } = render(
<Dialog open>
<h2>my dialog</h2>
</Dialog>,
);
fireEvent.mouseDown(getByRole('heading'));
findBackdrop(screen).click();
expect(getByRole('dialog')).not.to.equal(null);
});
});
describe('prop: classes', () => {
it('should add the class on the Paper element', () => {
const { getByTestId } = render(
<Dialog open classes={{ paper: 'my-paperclass' }} PaperProps={{ 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).to.have.class('my-paperclass');
});
});
describe('prop: maxWidth', () => {
it('should use the right className', () => {
const { getByTestId } = render(
<Dialog open maxWidth="xs" PaperProps={{ 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).to.have.class(classes.paperWidthXs);
});
});
describe('prop: fullWidth', () => {
it('should set `fullWidth` class if specified', () => {
const { getByTestId } = render(
<Dialog open fullWidth PaperProps={{ 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).to.have.class(classes.paperFullWidth);
});
it('should not set `fullWidth` class if not specified', () => {
const { getByTestId } = render(
<Dialog open PaperProps={{ 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).not.to.have.class(classes.paperFullWidth);
});
});
describe('prop: fullScreen', () => {
it('can render fullScreen if true', () => {
const { getByTestId } = render(
<Dialog open fullScreen PaperProps={{ 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).to.have.class(classes.paperFullScreen);
});
it('does not render fullScreen by default', () => {
const { getByTestId } = render(
<Dialog open PaperProps={{ 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).not.to.have.class(classes.paperFullScreen);
});
});
describe('prop: PaperProps.className', () => {
it('should merge the className', () => {
const { getByTestId } = render(
<Dialog open PaperProps={{ className: 'custom-paper-class', 'data-testid': 'paper' }}>
foo
</Dialog>,
);
expect(getByTestId('paper')).to.have.class(classes.paper);
expect(getByTestId('paper')).to.have.class('custom-paper-class');
});
});
describe('a11y', () => {
it('can be labelled by another element', () => {
const { getByRole } = render(
<Dialog open aria-labelledby="dialog-title">
<h1 id="dialog-title">Choose either one</h1>
<div>Actually you cant</div>
</Dialog>,
);
const dialog = getByRole('dialog');
expect(dialog).to.have.attr('aria-labelledby', 'dialog-title');
const label = document.getElementById(dialog.getAttribute('aria-labelledby'));
expect(label).to.have.text('Choose either one');
});
});
describe('prop: transitionDuration', () => {
it('should render the default theme values by default', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const theme = createTheme();
const enteringScreenDurationInSeconds = theme.transitions.duration.enteringScreen / 1000;
render(<Dialog open />);
const container = document.querySelector(`.${classes.container}`);
expect(container).toHaveComputedStyle({
transitionDuration: `${enteringScreenDurationInSeconds}s`,
});
});
it('should render the custom theme values', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const theme = createTheme({
transitions: {
duration: {
enteringScreen: 1,
},
},
});
render(
<ThemeProvider theme={theme}>
<Dialog open />
</ThemeProvider>,
);
const container = document.querySelector(`.${classes.container}`);
expect(container).toHaveComputedStyle({ transitionDuration: '0.001s' });
});
it('should render the values provided via prop', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
render(<Dialog open transitionDuration={{ enter: 1 }} />);
const container = document.querySelector(`.${classes.container}`);
expect(container).toHaveComputedStyle({
transitionDuration: '0.001s',
});
});
});
});