forked from reactjs/react-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.spec.js
More file actions
748 lines (636 loc) · 22.4 KB
/
Copy pathModal.spec.js
File metadata and controls
748 lines (636 loc) · 22.4 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* eslint-env mocha */
import should from "should";
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Modal from "react-modal";
import * as ariaAppHider from "react-modal/helpers/ariaAppHider";
import {
isBodyWithReactModalOpenClass,
isHtmlWithReactModalOpenClass,
htmlClassList,
contentAttribute,
mcontent,
moverlay,
escKeyDown,
renderModal,
unmountModal,
emptyDOM,
documentBodyClassList
} from "./helper";
export default () => {
afterEach("cleaned up all rendered modals", emptyDOM);
it("scopes tab navigation to the modal");
it("focuses the last focused element when tabbing in from browser chrome");
it("renders children [tested indirectly]");
it("can be open initially", () => {
const modal = renderModal({ isOpen: true }, "hello");
mcontent(modal).should.be.ok();
});
it("can be closed initially", () => {
const modal = renderModal({}, "hello");
should(ReactDOM.findDOMNode(mcontent(modal))).not.be.ok();
});
it("doesn't render the portal if modal is closed", () => {
const modal = renderModal({}, "hello");
should(ReactDOM.findDOMNode(modal.portal)).not.be.ok();
});
it("has default props", () => {
const node = document.createElement("div");
Modal.setAppElement(document.createElement("div"));
// eslint-disable-next-line react/no-render-return-value
const modal = ReactDOM.render(<Modal />, node);
const props = modal.props;
props.isOpen.should.not.be.ok();
props.ariaHideApp.should.be.ok();
props.closeTimeoutMS.should.be.eql(0);
props.shouldFocusAfterRender.should.be.ok();
props.shouldCloseOnOverlayClick.should.be.ok();
props.preventScroll.should.be.false();
ReactDOM.unmountComponentAtNode(node);
ariaAppHider.resetForTesting();
Modal.setAppElement(document.body); // restore default
});
it("accepts appElement as a prop", () => {
const el = document.createElement("div");
const node = document.createElement("div");
ReactDOM.render(<Modal isOpen={true} appElement={el} />, node);
el.getAttribute("aria-hidden").should.be.eql("true");
ReactDOM.unmountComponentAtNode(node);
});
it("renders into the body, not in context", () => {
const node = document.createElement("div");
class App extends Component {
render() {
return (
<div>
<Modal isOpen>
<span>hello</span>
</Modal>
</div>
);
}
}
Modal.setAppElement(node);
ReactDOM.render(<App />, node);
document.body
.querySelector(".ReactModalPortal")
.parentNode.should.be.eql(document.body);
ReactDOM.unmountComponentAtNode(node);
});
it("allow setting appElement of type string", () => {
const node = document.createElement("div");
class App extends Component {
render() {
return (
<div>
<Modal isOpen>
<span>hello</span>
</Modal>
</div>
);
}
}
const appElement = "body";
Modal.setAppElement(appElement);
ReactDOM.render(<App />, node);
document.body
.querySelector(".ReactModalPortal")
.parentNode.should.be.eql(document.body);
ReactDOM.unmountComponentAtNode(node);
});
it("default parentSelector should be document.body.", () => {
const modal = renderModal({ isOpen: true });
modal.props.parentSelector().should.be.eql(document.body);
});
it("renders the modal content with a dialog aria role when provided ", () => {
const child = "I am a child of Modal, and he has sent me here...";
const modal = renderModal({ isOpen: true, role: "dialog" }, child);
contentAttribute(modal, "role").should.be.eql("dialog");
});
// eslint-disable-next-line max-len
it("renders the modal content with the default aria role when not provided", () => {
const child = "I am a child of Modal, and he has sent me here...";
const modal = renderModal({ isOpen: true }, child);
contentAttribute(modal, "role").should.be.eql("dialog");
});
it("does not render the aria role when provided role with null", () => {
const child = "I am a child of Modal, and he has sent me here...";
const modal = renderModal({ isOpen: true, role: null }, child);
should(contentAttribute(modal, "role")).be.eql(null);
});
it("sets aria-label based on the contentLabel prop", () => {
const child = "I am a child of Modal, and he has sent me here...";
const modal = renderModal(
{
isOpen: true,
contentLabel: "Special Modal"
},
child
);
contentAttribute(modal, "aria-label").should.be.eql("Special Modal");
});
it("removes the portal node", () => {
renderModal({ isOpen: true }, "hello");
unmountModal();
should(document.querySelector(".ReactModalPortal")).not.be.ok();
});
it("removes the portal node after closeTimeoutMS", done => {
const closeTimeoutMS = 100;
renderModal({ isOpen: true, closeTimeoutMS }, "hello");
function checkDOM(count) {
const portal = document.querySelectorAll(".ReactModalPortal");
portal.length.should.be.eql(count);
}
unmountModal();
// content is still mounted after modal is gone
checkDOM(1);
setTimeout(() => {
// content is unmounted after specified timeout
checkDOM(0);
done();
}, closeTimeoutMS);
});
it("focuses the modal content by default", () => {
const modal = renderModal({ isOpen: true }, null);
document.activeElement.should.be.eql(mcontent(modal));
});
it("does not focus modal content if shouldFocusAfterRender is false", () => {
const modal = renderModal(
{ isOpen: true, shouldFocusAfterRender: false },
null
);
document.activeElement.should.not.be.eql(mcontent(modal));
});
it("give back focus to previous element or modal.", done => {
function cleanup() {
unmountModal();
done();
}
const modalA = renderModal(
{
isOpen: true,
className: "modal-a",
onRequestClose: cleanup
},
null
);
const modalContent = mcontent(modalA);
document.activeElement.should.be.eql(modalContent);
const modalB = renderModal(
{
isOpen: true,
className: "modal-b",
onRequestClose() {
const modalContent = mcontent(modalB);
document.activeElement.should.be.eql(mcontent(modalA));
escKeyDown(modalContent);
document.activeElement.should.be.eql(modalContent);
}
},
null
);
escKeyDown(modalContent);
});
xit("does not steel focus when a descendent is already focused", () => {
let content;
const input = (
<input
ref={el => {
el && el.focus();
content = el;
}}
/>
);
renderModal({ isOpen: true }, input, () => {
document.activeElement.should.be.eql(content);
});
});
it("supports id prop", () => {
const modal = renderModal({ isOpen: true, id: "id" });
mcontent(modal)
.id
.should.be.eql("id");
});
it("supports portalClassName", () => {
const modal = renderModal({
isOpen: true,
portalClassName: "myPortalClass"
});
modal.node.className.includes("myPortalClass").should.be.ok();
});
it("supports custom className", () => {
const modal = renderModal({ isOpen: true, className: "myClass" });
mcontent(modal)
.className.includes("myClass")
.should.be.ok();
});
it("supports custom overlayElement", () => {
const overlayElement = (props, contentElement) => (
<div {...props} id="custom">
{contentElement}
</div>
);
const modal = renderModal({ isOpen: true, overlayElement });
const modalOverlay = moverlay(modal);
modalOverlay.id.should.eql("custom");
});
it("supports custom contentElement", () => {
const contentElement = (props, children) => (
<div {...props} id="custom">
{children}
</div>
);
const modal = renderModal({ isOpen: true, contentElement }, "hello");
const modalContent = mcontent(modal);
modalContent.id.should.eql("custom");
modalContent.textContent.should.be.eql("hello");
});
it("supports overlayClassName", () => {
const modal = renderModal({
isOpen: true,
overlayClassName: "myOverlayClass"
});
moverlay(modal)
.className.includes("myOverlayClass")
.should.be.ok();
});
it("overrides content classes with custom object className", () => {
const modal = renderModal({
isOpen: true,
className: {
base: "myClass",
afterOpen: "myClass_after-open",
beforeClose: "myClass_before-close"
}
});
mcontent(modal).className.should.be.eql("myClass myClass_after-open");
unmountModal();
});
it("overrides overlay classes with custom object overlayClassName", () => {
const modal = renderModal({
isOpen: true,
overlayClassName: {
base: "myOverlayClass",
afterOpen: "myOverlayClass_after-open",
beforeClose: "myOverlayClass_before-close"
}
});
moverlay(modal).className.should.be.eql(
"myOverlayClass myOverlayClass_after-open"
);
unmountModal();
});
it("supports overriding react modal open class in document.body.", () => {
renderModal({ isOpen: true, bodyOpenClassName: "custom-modal-open" });
(document.body.className.indexOf("custom-modal-open") > -1).should.be.ok();
});
it("supports setting react modal open class in <html />.", () => {
renderModal({ isOpen: true, htmlOpenClassName: "custom-modal-open" });
isHtmlWithReactModalOpenClass("custom-modal-open").should.be.ok();
});
// eslint-disable-next-line max-len
it("don't append class to document.body if modal is closed.", () => {
renderModal({ isOpen: false });
isBodyWithReactModalOpenClass().should.not.be.ok();
});
// eslint-disable-next-line max-len
it("don't append any class to document.body when bodyOpenClassName is null.", () => {
renderModal({ isOpen: true, bodyOpenClassName: null });
documentBodyClassList().should.be.empty();
});
it("don't append class to <html /> if modal is closed.", () => {
renderModal({ isOpen: false, htmlOpenClassName: "custom-modal-open" });
isHtmlWithReactModalOpenClass().should.not.be.ok();
});
it("append class to document.body if modal is open.", () => {
renderModal({ isOpen: true });
isBodyWithReactModalOpenClass().should.be.ok();
});
it("don't append class to <html /> if not defined.", () => {
renderModal({ isOpen: true });
htmlClassList().should.be.empty();
});
// eslint-disable-next-line max-len
it("removes class from document.body when unmounted without closing", () => {
renderModal({ isOpen: true });
unmountModal();
isBodyWithReactModalOpenClass().should.not.be.ok();
});
it("remove class from document.body when no modals opened", () => {
renderModal({ isOpen: true });
renderModal({ isOpen: true });
isBodyWithReactModalOpenClass().should.be.ok();
unmountModal();
isBodyWithReactModalOpenClass().should.be.ok();
unmountModal();
isBodyWithReactModalOpenClass().should.not.be.ok();
isHtmlWithReactModalOpenClass().should.not.be.ok();
});
it("supports adding/removing multiple document.body classes", () => {
renderModal({
isOpen: true,
bodyOpenClassName: "A B C"
});
document.body.classList.contains("A", "B", "C").should.be.ok();
unmountModal();
document.body.classList.contains("A", "B", "C").should.not.be.ok();
});
it("does not remove shared classes if more than one modal is open", () => {
renderModal({
isOpen: true,
bodyOpenClassName: "A"
});
renderModal({
isOpen: true,
bodyOpenClassName: "A B"
});
isBodyWithReactModalOpenClass("A B").should.be.ok();
unmountModal();
isBodyWithReactModalOpenClass("A B").should.not.be.ok();
isBodyWithReactModalOpenClass("A").should.be.ok();
unmountModal();
isBodyWithReactModalOpenClass("A").should.not.be.ok();
});
it("should not add classes to document.body for unopened modals", () => {
renderModal({ isOpen: true });
isBodyWithReactModalOpenClass().should.be.ok();
renderModal({ isOpen: false, bodyOpenClassName: "testBodyClass" });
isBodyWithReactModalOpenClass("testBodyClass").should.not.be.ok();
});
it("should not remove classes from document.body if modal is closed", () => {
renderModal({ isOpen: true });
isBodyWithReactModalOpenClass().should.be.ok();
renderModal({ isOpen: false, bodyOpenClassName: "testBodyClass" });
renderModal({ isOpen: false });
isBodyWithReactModalOpenClass("testBodyClass").should.not.be.ok();
isBodyWithReactModalOpenClass().should.be.ok();
renderModal({ isOpen: false });
renderModal({ isOpen: false });
isBodyWithReactModalOpenClass().should.be.ok();
});
it("should not remove classes from <html /> if modal is closed", () => {
const modalA = renderModal({ isOpen: false });
isHtmlWithReactModalOpenClass().should.not.be.ok();
const modalB = renderModal({
isOpen: true,
htmlOpenClassName: "testHtmlClass"
});
modalA.portal.close();
isHtmlWithReactModalOpenClass("testHtmlClass").should.be.ok();
modalB.portal.close();
isHtmlWithReactModalOpenClass().should.not.be.ok();
renderModal({ isOpen: false });
});
it("additional aria attributes", () => {
const modal = renderModal(
{ isOpen: true, aria: { labelledby: "a" } },
"hello"
);
mcontent(modal)
.getAttribute("aria-labelledby")
.should.be.eql("a");
unmountModal();
});
it("additional data attributes", () => {
const modal = renderModal(
{ isOpen: true, data: { background: "green" } },
"hello"
);
mcontent(modal)
.getAttribute("data-background")
.should.be.eql("green");
unmountModal();
});
it("additional testId attribute", () => {
const modal = renderModal({ isOpen: true, testId: "foo-bar" }, "hello");
mcontent(modal)
.getAttribute("data-testid")
.should.be.eql("foo-bar");
unmountModal();
});
it("raises an exception if the appElement selector does not match", () => {
should(() => ariaAppHider.setElement(".test")).throw();
});
it("removes aria-hidden from appElement when unmounted w/o closing", () => {
const el = document.createElement("div");
const node = document.createElement("div");
ReactDOM.render(<Modal isOpen appElement={el} />, node);
el.getAttribute("aria-hidden").should.be.eql("true");
ReactDOM.unmountComponentAtNode(node);
should(el.getAttribute("aria-hidden")).not.be.ok();
});
// eslint-disable-next-line max-len
it("removes aria-hidden when closed and another modal with ariaHideApp set to false is open", () => {
const rootNode = document.createElement("div");
document.body.appendChild(rootNode);
const appElement = document.createElement("div");
document.body.appendChild(appElement);
Modal.setAppElement(appElement);
const initialState = (
<div>
<Modal isOpen={true} ariaHideApp={false} id="test-1-modal-1" />
<Modal isOpen={true} ariaHideApp={true} id="test-1-modal-2" />
</div>
);
ReactDOM.render(initialState, rootNode);
appElement.getAttribute("aria-hidden").should.be.eql("true");
const updatedState = (
<div>
<Modal isOpen={true} ariaHideApp={false} id="test-1-modal-1" />
<Modal isOpen={false} ariaHideApp={true} id="test-1-modal-2" />
</div>
);
ReactDOM.render(updatedState, rootNode);
should(appElement.getAttribute("aria-hidden")).not.be.ok();
ReactDOM.unmountComponentAtNode(rootNode);
});
// eslint-disable-next-line max-len
it("maintains aria-hidden when closed and another modal with ariaHideApp set to true is open", () => {
const rootNode = document.createElement("div");
document.body.appendChild(rootNode);
const appElement = document.createElement("div");
document.body.appendChild(appElement);
Modal.setAppElement(appElement);
const initialState = (
<div>
<Modal isOpen={true} ariaHideApp={true} id="test-1-modal-1" />
<Modal isOpen={true} ariaHideApp={true} id="test-1-modal-2" />
</div>
);
ReactDOM.render(initialState, rootNode);
appElement.getAttribute("aria-hidden").should.be.eql("true");
const updatedState = (
<div>
<Modal isOpen={true} ariaHideApp={true} id="test-1-modal-1" />
<Modal isOpen={false} ariaHideApp={true} id="test-1-modal-2" />
</div>
);
ReactDOM.render(updatedState, rootNode);
appElement.getAttribute("aria-hidden").should.be.eql("true");
ReactDOM.unmountComponentAtNode(rootNode);
});
// eslint-disable-next-line max-len
it("removes aria-hidden when unmounted without close and second modal with ariaHideApp=false is open", () => {
const appElement = document.createElement("div");
document.body.appendChild(appElement);
Modal.setAppElement(appElement);
renderModal({ isOpen: true, ariaHideApp: false, id: "test-2-modal-1" });
should(appElement.getAttribute("aria-hidden")).not.be.ok();
renderModal({ isOpen: true, ariaHideApp: true, id: "test-2-modal-2" });
appElement.getAttribute("aria-hidden").should.be.eql("true");
unmountModal();
should(appElement.getAttribute("aria-hidden")).not.be.ok();
});
// eslint-disable-next-line max-len
it("maintains aria-hidden when unmounted without close and second modal with ariaHideApp=true is open", () => {
const appElement = document.createElement("div");
document.body.appendChild(appElement);
Modal.setAppElement(appElement);
renderModal({ isOpen: true, ariaHideApp: true, id: "test-3-modal-1" });
appElement.getAttribute("aria-hidden").should.be.eql("true");
renderModal({ isOpen: true, ariaHideApp: true, id: "test-3-modal-2" });
appElement.getAttribute("aria-hidden").should.be.eql("true");
unmountModal();
appElement.getAttribute("aria-hidden").should.be.eql("true");
});
it("adds --after-open for animations", () => {
const modal = renderModal({ isOpen: true });
const rg = /--after-open/i;
rg.test(mcontent(modal).className).should.be.ok();
rg.test(moverlay(modal).className).should.be.ok();
});
it("adds --before-close for animations", () => {
const closeTimeoutMS = 50;
const modal = renderModal({
isOpen: true,
closeTimeoutMS
});
modal.portal.closeWithTimeout();
const rg = /--before-close/i;
rg.test(moverlay(modal).className).should.be.ok();
rg.test(mcontent(modal).className).should.be.ok();
modal.portal.closeWithoutTimeout();
});
it("should not be open after close with time out and reopen it", () => {
const modal = renderModal({
isOpen: true,
closeTimeoutMS: 2000,
onRequestClose() {}
});
modal.portal.closeWithTimeout();
modal.portal.open();
modal.portal.closeWithoutTimeout();
modal.portal.state.isOpen.should.not.be.ok();
});
it("verify default prop of shouldCloseOnOverlayClick", () => {
const modal = renderModal({ isOpen: true });
modal.props.shouldCloseOnOverlayClick.should.be.ok();
});
it("verify prop of shouldCloseOnOverlayClick", () => {
const modalOpts = { isOpen: true, shouldCloseOnOverlayClick: false };
const modal = renderModal(modalOpts);
modal.props.shouldCloseOnOverlayClick.should.not.be.ok();
});
it("keeps the modal in the DOM until closeTimeoutMS elapses", done => {
const closeTimeoutMS = 100;
const modal = renderModal({ isOpen: true, closeTimeoutMS });
modal.portal.closeWithTimeout();
function checkDOM(count) {
const overlay = document.querySelectorAll(".ReactModal__Overlay");
const content = document.querySelectorAll(".ReactModal__Content");
overlay.length.should.be.eql(count);
content.length.should.be.eql(count);
}
// content is still mounted after modal is gone
checkDOM(1);
setTimeout(() => {
// content is unmounted after specified timeout
checkDOM(0);
done();
}, closeTimeoutMS);
});
xit("shouldn't throw if forcibly unmounted during mounting", () => {
/* eslint-disable camelcase, react/prop-types */
class Wrapper extends Component {
constructor(props) {
super(props);
this.state = { error: false };
}
unstable_handleError() {
this.setState({ error: true });
}
render() {
return this.state.error ? null : <div>{this.props.children}</div>;
}
}
/* eslint-enable camelcase, react/prop-types */
const Throw = () => {
throw new Error("reason");
};
const TestCase = () => (
<Wrapper>
<Modal />
<Throw />
</Wrapper>
);
const currentDiv = document.createElement("div");
document.body.appendChild(currentDiv);
// eslint-disable-next-line react/no-render-return-value
const mount = () => ReactDOM.render(<TestCase />, currentDiv);
mount.should.not.throw();
document.body.removeChild(currentDiv);
});
it("verify that portalClassName is refreshed on component update", () => {
const node = document.createElement("div");
let modal = null;
class App extends Component {
constructor(props) {
super(props);
this.state = { testHasChanged: false };
}
componentDidMount() {
modal.node.className.should.be.eql("myPortalClass");
this.setState({
testHasChanged: true
});
}
componentDidUpdate() {
modal.node.className.should.be.eql("myPortalClass-modifier");
}
render() {
const portalClassName =
this.state.testHasChanged === true
? "myPortalClass-modifier"
: "myPortalClass";
return (
<div>
<Modal
ref={modalComponent => {
modal = modalComponent;
}}
isOpen
portalClassName={portalClassName}
>
<span>Test</span>
</Modal>
</div>
);
}
}
Modal.setAppElement(node);
ReactDOM.render(<App />, node);
});
it("use overlayRef and contentRef", () => {
let overlay = null;
let content = null;
renderModal({
isOpen: true,
overlayRef: node => (overlay = node),
contentRef: node => (content = node)
});
overlay.should.be.instanceOf(HTMLElement);
content.should.be.instanceOf(HTMLElement);
overlay.classList.contains("ReactModal__Overlay");
content.classList.contains("ReactModal__Content");
});
};