@collana-solutions/reactstrap 10.0.0 → 10.0.2
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.
- package/CHANGELOG.md +32 -7
- package/LICENSE +21 -21
- package/README.md +178 -178
- package/esm/AccordionContext.js +6 -6
- package/esm/Carousel.js +2 -2
- package/esm/CarouselContext.js +5 -5
- package/esm/Dropdown.js +1 -1
- package/esm/DropdownContext.js +9 -9
- package/esm/DropdownMenu.js +2 -2
- package/esm/DropdownToggle.js +1 -1
- package/esm/PopperContent.js +1 -1
- package/esm/TabContext.js +5 -5
- package/esm/__tests__/DropdownMenu.spec.js +1 -1
- package/esm/__tests__/PopperContent.spec.js +1 -1
- package/esm/__tests__/Tabs.spec.js +4 -4
- package/esm/__tests__/TooltipPopoverWrapper.spec.js +1 -1
- package/esm/utils.js +4 -4
- package/lib/AccordionContext.js +6 -6
- package/lib/Carousel.js +2 -2
- package/lib/CarouselContext.js +5 -5
- package/lib/Dropdown.js +1 -1
- package/lib/DropdownContext.js +9 -9
- package/lib/DropdownMenu.js +2 -2
- package/lib/DropdownToggle.js +1 -1
- package/lib/PopperContent.js +1 -1
- package/lib/TabContext.js +5 -5
- package/lib/utils.js +4 -4
- package/package.json +331 -333
- package/src/Accordion.js +60 -60
- package/src/AccordionBody.js +60 -60
- package/src/AccordionContext.js +10 -10
- package/src/AccordionHeader.js +65 -65
- package/src/AccordionItem.js +39 -39
- package/src/Alert.js +101 -101
- package/src/Badge.js +56 -56
- package/src/Breadcrumb.js +51 -51
- package/src/BreadcrumbItem.js +41 -41
- package/src/Button.js +106 -106
- package/src/ButtonDropdown.js +15 -15
- package/src/ButtonGroup.js +47 -47
- package/src/ButtonToggle.js +73 -73
- package/src/ButtonToolbar.js +32 -32
- package/src/Card.js +56 -56
- package/src/CardBody.js +38 -38
- package/src/CardColumns.js +24 -24
- package/src/CardDeck.js +24 -24
- package/src/CardFooter.js +27 -27
- package/src/CardGroup.js +24 -24
- package/src/CardHeader.js +27 -27
- package/src/CardImg.js +47 -47
- package/src/CardImgOverlay.js +24 -24
- package/src/CardLink.js +35 -35
- package/src/CardSubtitle.js +27 -27
- package/src/CardText.js +27 -27
- package/src/CardTitle.js +27 -27
- package/src/Carousel.js +323 -323
- package/src/CarouselCaption.js +32 -32
- package/src/CarouselContext.js +9 -9
- package/src/CarouselControl.js +66 -66
- package/src/CarouselIndicators.js +60 -60
- package/src/CarouselItem.js +139 -139
- package/src/CloseButton.js +43 -43
- package/src/Col.js +120 -120
- package/src/Collapse.js +175 -175
- package/src/Container.js +39 -39
- package/src/Dropdown.js +408 -408
- package/src/DropdownContext.js +13 -13
- package/src/DropdownItem.js +119 -119
- package/src/DropdownMenu.js +162 -162
- package/src/DropdownToggle.js +137 -137
- package/src/Fade.js +83 -83
- package/src/Form.js +53 -53
- package/src/FormFeedback.js +40 -40
- package/src/FormGroup.js +60 -60
- package/src/FormText.js +39 -39
- package/src/Input.js +142 -142
- package/src/InputGroup.js +47 -47
- package/src/InputGroupContext.js +3 -3
- package/src/InputGroupText.js +28 -28
- package/src/Label.js +123 -123
- package/src/List.js +30 -30
- package/src/ListGroup.js +60 -60
- package/src/ListGroupItem.js +59 -59
- package/src/ListGroupItemHeading.js +27 -27
- package/src/ListGroupItemText.js +27 -27
- package/src/ListInlineItem.js +28 -28
- package/src/Media.js +83 -83
- package/src/Modal.js +578 -578
- package/src/ModalBody.js +27 -27
- package/src/ModalFooter.js +27 -27
- package/src/ModalHeader.js +64 -64
- package/src/Nav.js +81 -81
- package/src/NavItem.js +36 -36
- package/src/NavLink.js +80 -80
- package/src/Navbar.js +81 -81
- package/src/NavbarBrand.js +28 -28
- package/src/NavbarText.js +35 -35
- package/src/NavbarToggler.js +47 -47
- package/src/Offcanvas.js +473 -473
- package/src/OffcanvasBody.js +24 -24
- package/src/OffcanvasHeader.js +59 -59
- package/src/Pagination.js +53 -53
- package/src/PaginationItem.js +43 -43
- package/src/PaginationLink.js +95 -95
- package/src/Placeholder.js +59 -59
- package/src/PlaceholderButton.js +35 -35
- package/src/Popover.js +30 -30
- package/src/PopoverBody.js +25 -25
- package/src/PopoverHeader.js +25 -25
- package/src/PopperContent.js +238 -238
- package/src/PopperTargetHelper.js +17 -17
- package/src/Portal.js +38 -38
- package/src/Progress.js +104 -104
- package/src/Row.js +66 -66
- package/src/Spinner.js +67 -67
- package/src/TabContent.js +51 -51
- package/src/TabContext.js +9 -9
- package/src/TabPane.js +35 -35
- package/src/Table.js +86 -86
- package/src/Toast.js +64 -64
- package/src/ToastBody.js +35 -35
- package/src/ToastHeader.js +89 -89
- package/src/Tooltip.js +32 -32
- package/src/TooltipPopoverWrapper.js +403 -403
- package/src/UncontrolledAccordion.js +43 -43
- package/src/UncontrolledAlert.js +23 -23
- package/src/UncontrolledButtonDropdown.js +34 -34
- package/src/UncontrolledCarousel.js +134 -134
- package/src/UncontrolledCollapse.js +69 -69
- package/src/UncontrolledDropdown.js +42 -42
- package/src/UncontrolledPopover.js +34 -34
- package/src/UncontrolledTooltip.js +34 -34
- package/src/__tests__/.eslintrc +26 -26
- package/src/__tests__/Accordion.spec.js +148 -148
- package/src/__tests__/AccordionBody.spec.js +52 -52
- package/src/__tests__/AccordionHeader.spec.js +60 -60
- package/src/__tests__/AccordionItem.spec.js +21 -21
- package/src/__tests__/Alert.spec.js +145 -145
- package/src/__tests__/Badge.spec.js +54 -54
- package/src/__tests__/Breadcrumb.spec.js +32 -32
- package/src/__tests__/BreadcrumbList.spec.js +38 -38
- package/src/__tests__/Button.spec.js +129 -129
- package/src/__tests__/ButtonDropdown.spec.js +38 -38
- package/src/__tests__/ButtonGroup.spec.js +32 -32
- package/src/__tests__/ButtonToggle.spec.js +59 -59
- package/src/__tests__/ButtonToolbar.spec.js +20 -20
- package/src/__tests__/Card.spec.js +56 -56
- package/src/__tests__/CardBody.spec.js +20 -20
- package/src/__tests__/CardColumns.spec.js +20 -20
- package/src/__tests__/CardDeck.spec.js +20 -20
- package/src/__tests__/CardFooter.spec.js +20 -20
- package/src/__tests__/CardGroup.spec.js +20 -20
- package/src/__tests__/CardHeader.spec.js +20 -20
- package/src/__tests__/CardImg.spec.js +36 -36
- package/src/__tests__/CardImgOverlay.spec.js +20 -20
- package/src/__tests__/CardLink.spec.js +20 -20
- package/src/__tests__/CardSubtitle.spec.js +25 -25
- package/src/__tests__/CardText.spec.js +20 -20
- package/src/__tests__/CardTitle.spec.js +25 -25
- package/src/__tests__/Carousel.spec.js +769 -769
- package/src/__tests__/CloseButton.spec.js +46 -46
- package/src/__tests__/Col.spec.js +102 -102
- package/src/__tests__/Collapse.spec.js +124 -124
- package/src/__tests__/Container.spec.js +39 -39
- package/src/__tests__/Dropdown.spec.js +1185 -1185
- package/src/__tests__/DropdownItem.spec.js +142 -142
- package/src/__tests__/DropdownMenu.spec.js +215 -215
- package/src/__tests__/DropdownToggle.spec.js +177 -177
- package/src/__tests__/Fade.spec.js +130 -130
- package/src/__tests__/Form.spec.js +25 -25
- package/src/__tests__/FormFeedback.spec.js +48 -48
- package/src/__tests__/FormGroup.spec.js +158 -158
- package/src/__tests__/FormText.spec.js +57 -57
- package/src/__tests__/Input.spec.js +250 -250
- package/src/__tests__/InputGroup.spec.js +73 -73
- package/src/__tests__/InputGroupText.spec.js +25 -25
- package/src/__tests__/Label.spec.js +148 -148
- package/src/__tests__/List.spec.js +34 -34
- package/src/__tests__/ListGroup.spec.js +60 -60
- package/src/__tests__/ListGroupItem.spec.js +47 -47
- package/src/__tests__/ListGroupItemHeading.spec.js +12 -12
- package/src/__tests__/ListGroupItemText.spec.js +16 -16
- package/src/__tests__/ListInlineItem.spec.js +25 -25
- package/src/__tests__/Media.spec.js +121 -121
- package/src/__tests__/Modal.spec.js +1137 -1137
- package/src/__tests__/ModalBody.spec.js +20 -20
- package/src/__tests__/ModalFooter.spec.js +20 -20
- package/src/__tests__/ModalHeader.spec.js +36 -36
- package/src/__tests__/Nav.spec.js +84 -84
- package/src/__tests__/NavBrand.spec.js +27 -27
- package/src/__tests__/NavItem.spec.js +32 -32
- package/src/__tests__/NavLink.spec.js +76 -76
- package/src/__tests__/Navbar.spec.js +109 -109
- package/src/__tests__/NavbarText.spec.js +25 -25
- package/src/__tests__/NavbarToggler.spec.js +25 -25
- package/src/__tests__/Offcanvas.spec.js +840 -840
- package/src/__tests__/OffcanvasBody.spec.js +20 -20
- package/src/__tests__/OffcanvasHeader.spec.js +35 -35
- package/src/__tests__/Pagination.spec.js +47 -47
- package/src/__tests__/PaginationItem.spec.js +32 -32
- package/src/__tests__/PaginationLink.spec.js +114 -114
- package/src/__tests__/Placeholder.spec.js +67 -67
- package/src/__tests__/PlaceholderButton.spec.js +16 -16
- package/src/__tests__/Popover.spec.js +39 -39
- package/src/__tests__/PopoverBody.spec.js +9 -9
- package/src/__tests__/PopoverHeader.spec.js +9 -9
- package/src/__tests__/PopperContent.spec.js +246 -246
- package/src/__tests__/Progress.spec.js +233 -233
- package/src/__tests__/Row.spec.js +38 -38
- package/src/__tests__/Spinner.spec.js +53 -53
- package/src/__tests__/Table.spec.js +68 -68
- package/src/__tests__/Tabs.spec.js +77 -77
- package/src/__tests__/Toast.spec.js +58 -58
- package/src/__tests__/ToastBody.spec.js +20 -20
- package/src/__tests__/ToastHeader.spec.js +30 -30
- package/src/__tests__/Tooltip.spec.js +38 -38
- package/src/__tests__/TooltipPopoverWrapper.spec.js +777 -777
- package/src/__tests__/Uncontrolled.spec.js +298 -298
- package/src/__tests__/UncontrolledAccordion.spec.js +120 -120
- package/src/__tests__/UncontrolledCarousel.spec.js +136 -136
- package/src/__tests__/UncontrolledCollapse.spec.js +74 -74
- package/src/__tests__/utils.spec.js +399 -399
- package/src/index.js +101 -101
- package/src/polyfill.js +26 -26
- package/src/testUtils.js +50 -50
- package/src/utils.js +403 -403
- package/types/index.d.ts +195 -195
- package/types/lib/Accordion.d.ts +19 -19
- package/types/lib/AccordionBody.d.ts +12 -12
- package/types/lib/AccordionHeader.d.ts +12 -12
- package/types/lib/AccordionItem.d.ts +11 -11
- package/types/lib/Alert.d.ts +23 -23
- package/types/lib/Badge.d.ts +14 -14
- package/types/lib/Breadcrumb.d.ts +13 -13
- package/types/lib/BreadcrumbItem.d.ts +12 -12
- package/types/lib/Button.d.ts +19 -19
- package/types/lib/ButtonDropdown.d.ts +9 -9
- package/types/lib/ButtonGroup.d.ts +13 -13
- package/types/lib/ButtonToggle.d.ts +12 -12
- package/types/lib/ButtonToolbar.d.ts +11 -11
- package/types/lib/Card.d.ts +16 -16
- package/types/lib/CardBody.d.ts +12 -12
- package/types/lib/CardColumns.d.ts +11 -11
- package/types/lib/CardDeck.d.ts +11 -11
- package/types/lib/CardFooter.d.ts +11 -11
- package/types/lib/CardGroup.d.ts +11 -11
- package/types/lib/CardHeader.d.ts +11 -11
- package/types/lib/CardImg.d.ts +14 -14
- package/types/lib/CardImgOverlay.d.ts +11 -11
- package/types/lib/CardLink.d.ts +13 -13
- package/types/lib/CardSubtitle.d.ts +11 -11
- package/types/lib/CardText.d.ts +11 -11
- package/types/lib/CardTitle.d.ts +11 -11
- package/types/lib/Carousel.d.ts +35 -35
- package/types/lib/CarouselCaption.d.ts +13 -13
- package/types/lib/CarouselControl.d.ts +15 -15
- package/types/lib/CarouselIndicators.d.ts +16 -16
- package/types/lib/CarouselItem.d.ts +19 -19
- package/types/lib/CloseButton.d.ts +11 -11
- package/types/lib/Col.d.ts +30 -30
- package/types/lib/Collapse.d.ts +32 -32
- package/types/lib/Container.d.ts +12 -12
- package/types/lib/Dropdown.d.ts +35 -35
- package/types/lib/DropdownItem.d.ts +18 -18
- package/types/lib/DropdownMenu.d.ts +20 -20
- package/types/lib/DropdownToggle.d.ts +20 -20
- package/types/lib/Fade.d.ts +23 -23
- package/types/lib/Form.d.ts +12 -12
- package/types/lib/FormFeedback.d.ts +13 -13
- package/types/lib/FormGroup.d.ts +19 -19
- package/types/lib/FormText.d.ts +13 -13
- package/types/lib/Input.d.ts +47 -47
- package/types/lib/InputGroup.d.ts +12 -12
- package/types/lib/InputGroupText.d.ts +11 -11
- package/types/lib/Label.d.ts +27 -27
- package/types/lib/List.d.ts +12 -12
- package/types/lib/ListGroup.d.ts +14 -14
- package/types/lib/ListGroupItem.d.ts +16 -16
- package/types/lib/ListGroupItemHeading.d.ts +14 -14
- package/types/lib/ListGroupItemText.d.ts +14 -14
- package/types/lib/ListInlineItem.d.ts +11 -11
- package/types/lib/Media.d.ts +23 -23
- package/types/lib/Modal.d.ts +39 -39
- package/types/lib/ModalBody.d.ts +11 -11
- package/types/lib/ModalFooter.d.ts +11 -11
- package/types/lib/ModalHeader.d.ts +13 -13
- package/types/lib/Nav.d.ts +19 -19
- package/types/lib/NavItem.d.ts +12 -12
- package/types/lib/NavLink.d.ts +15 -15
- package/types/lib/Navbar.d.ts +19 -19
- package/types/lib/NavbarBrand.d.ts +12 -12
- package/types/lib/NavbarText.d.ts +11 -11
- package/types/lib/NavbarToggler.d.ts +12 -12
- package/types/lib/Offcanvas.d.ts +38 -38
- package/types/lib/OffcanvasBody.d.ts +11 -11
- package/types/lib/OffcanvasHeader.d.ts +13 -13
- package/types/lib/Pagination.d.ts +15 -15
- package/types/lib/PaginationItem.d.ts +13 -13
- package/types/lib/PaginationLink.d.ts +17 -17
- package/types/lib/Placeholder.d.ts +16 -16
- package/types/lib/PlaceholderButton.d.ts +13 -13
- package/types/lib/Popover.d.ts +34 -34
- package/types/lib/PopoverBody.d.ts +11 -11
- package/types/lib/PopoverHeader.d.ts +11 -11
- package/types/lib/Progress.d.ts +23 -23
- package/types/lib/Row.d.ts +21 -21
- package/types/lib/Spinner.d.ts +14 -14
- package/types/lib/TabContent.d.ts +12 -12
- package/types/lib/TabPane.d.ts +12 -12
- package/types/lib/Table.d.ts +22 -22
- package/types/lib/Tag.d.ts +13 -13
- package/types/lib/Toast.d.ts +16 -16
- package/types/lib/ToastBody.d.ts +12 -12
- package/types/lib/ToastHeader.d.ts +16 -16
- package/types/lib/Tooltip.d.ts +31 -31
- package/types/lib/Uncontrolled.d.ts +41 -41
- package/types/lib/utils.d.ts +5 -5
- package/types/reactstrap-tests.tsx +5465 -5465
- package/types/tsconfig.json +20 -20
- package/types/tslint.json +1 -1
|
@@ -1,1137 +1,1137 @@
|
|
|
1
|
-
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { act, fireEvent, render, screen } from '@testing-library/react';
|
|
4
|
-
import user from '@testing-library/user-event';
|
|
5
|
-
import '@testing-library/jest-dom';
|
|
6
|
-
import { Modal, ModalBody, ModalHeader, ModalFooter, Button } from '..';
|
|
7
|
-
|
|
8
|
-
describe('Modal', () => {
|
|
9
|
-
const toggle = () => {};
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
jest.useFakeTimers();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
afterEach(() => {
|
|
16
|
-
jest.clearAllTimers();
|
|
17
|
-
// rtl doesn't clear attributes added to body, so manually clearing them
|
|
18
|
-
document.body.removeAttribute('style');
|
|
19
|
-
document.body.removeAttribute('class');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should render modal portal into DOM', () => {
|
|
23
|
-
render(
|
|
24
|
-
<Modal isOpen toggle={toggle}>
|
|
25
|
-
Yo!
|
|
26
|
-
</Modal>,
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
expect(screen.getByText(/yo/i)).toBeInTheDocument();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should render with the class "modal-dialog"', () => {
|
|
33
|
-
render(
|
|
34
|
-
<Modal isOpen toggle={toggle}>
|
|
35
|
-
Yo!
|
|
36
|
-
</Modal>,
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
expect(screen.getByText(/yo/i).parentElement).toHaveClass('modal-dialog');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should render external content when present', () => {
|
|
43
|
-
render(
|
|
44
|
-
<Modal
|
|
45
|
-
isOpen
|
|
46
|
-
toggle={toggle}
|
|
47
|
-
external={<button className="cool-close-button">crazy button</button>}
|
|
48
|
-
>
|
|
49
|
-
Yo!
|
|
50
|
-
</Modal>,
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
expect(screen.getByText(/crazy button/i)).toBeInTheDocument();
|
|
54
|
-
expect(
|
|
55
|
-
screen
|
|
56
|
-
.getByText(/crazy button/i)
|
|
57
|
-
.nextElementSibling.className.split(' ')
|
|
58
|
-
.indexOf('modal-dialog') > -1,
|
|
59
|
-
).toBe(true);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should render with the backdrop with the class "modal-backdrop" by default', () => {
|
|
63
|
-
render(
|
|
64
|
-
<Modal isOpen toggle={toggle}>
|
|
65
|
-
Yo!
|
|
66
|
-
</Modal>,
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should render with the backdrop with the class "modal-backdrop" when backdrop is "static"', () => {
|
|
73
|
-
render(
|
|
74
|
-
<Modal isOpen toggle={toggle} backdrop="static">
|
|
75
|
-
Yo!
|
|
76
|
-
</Modal>,
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should not render with the backdrop with the class "modal-backdrop" when backdrop is "false"', () => {
|
|
83
|
-
render(
|
|
84
|
-
<Modal isOpen toggle={toggle} backdrop={false}>
|
|
85
|
-
Yo!
|
|
86
|
-
</Modal>,
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
90
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(0);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('should render with the class "modal-dialog-scrollable" when scrollable is "true"', () => {
|
|
94
|
-
render(
|
|
95
|
-
<Modal isOpen toggle={toggle} scrollable>
|
|
96
|
-
Yo!
|
|
97
|
-
</Modal>,
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
expect(
|
|
101
|
-
document.getElementsByClassName('modal-dialog-scrollable').length,
|
|
102
|
-
).toBe(1);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('should render with class "modal-dialog" and have custom class name if provided', () => {
|
|
106
|
-
render(
|
|
107
|
-
<Modal isOpen toggle={toggle} className="my-custom-modal">
|
|
108
|
-
Yo!
|
|
109
|
-
</Modal>,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
113
|
-
expect(document.getElementsByClassName('my-custom-modal').length).toBe(1);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('should render with class "modal-dialog" w/o centered class if not provided', () => {
|
|
117
|
-
render(
|
|
118
|
-
<Modal isOpen toggle={toggle}>
|
|
119
|
-
Yo!
|
|
120
|
-
</Modal>,
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
jest.advanceTimersByTime(300);
|
|
124
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
125
|
-
expect(
|
|
126
|
-
document.getElementsByClassName('modal-dialog-centered').length,
|
|
127
|
-
).toBe(0);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('should render with class "modal-dialog" and centered class if provided', () => {
|
|
131
|
-
render(
|
|
132
|
-
<Modal isOpen toggle={toggle} centered>
|
|
133
|
-
Yo!
|
|
134
|
-
</Modal>,
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
138
|
-
expect(
|
|
139
|
-
document.getElementsByClassName('modal-dialog-centered').length,
|
|
140
|
-
).toBe(1);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe('fullscreen', () => {
|
|
144
|
-
it('should render non fullscreen by default', () => {
|
|
145
|
-
render(
|
|
146
|
-
<Modal isOpen toggle={toggle}>
|
|
147
|
-
Yo!
|
|
148
|
-
</Modal>,
|
|
149
|
-
);
|
|
150
|
-
|
|
151
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
152
|
-
expect(document.getElementsByClassName('modal-fullscreen').length).toBe(
|
|
153
|
-
0,
|
|
154
|
-
);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should always render fullscreen if true', () => {
|
|
158
|
-
render(
|
|
159
|
-
<Modal isOpen toggle={toggle} fullscreen>
|
|
160
|
-
Yo!
|
|
161
|
-
</Modal>,
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
165
|
-
expect(document.getElementsByClassName('modal-fullscreen').length).toBe(
|
|
166
|
-
1,
|
|
167
|
-
);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('should render fullscreen below breakpoint if breakpoint is provided', () => {
|
|
171
|
-
render(
|
|
172
|
-
<Modal isOpen toggle={toggle} fullscreen="lg">
|
|
173
|
-
Yo!
|
|
174
|
-
</Modal>,
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
178
|
-
expect(document.getElementsByClassName('modal-fullscreen').length).toBe(
|
|
179
|
-
0,
|
|
180
|
-
);
|
|
181
|
-
expect(
|
|
182
|
-
document.getElementsByClassName('modal-fullscreen-lg-down').length,
|
|
183
|
-
).toBe(1);
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('should render with additional props if provided', () => {
|
|
188
|
-
render(
|
|
189
|
-
<Modal isOpen toggle={toggle} style={{ maxWidth: '95%' }}>
|
|
190
|
-
Yo!
|
|
191
|
-
</Modal>,
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
195
|
-
expect(
|
|
196
|
-
document.getElementsByClassName('modal-dialog')[0].style.maxWidth,
|
|
197
|
-
).toBe('95%');
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
it('should render without fade transition if provided with fade={false}', () => {
|
|
201
|
-
render(
|
|
202
|
-
<Modal
|
|
203
|
-
isOpen
|
|
204
|
-
toggle={toggle}
|
|
205
|
-
fade={false}
|
|
206
|
-
modalClassName="fadeless-modal"
|
|
207
|
-
>
|
|
208
|
-
Howdy!
|
|
209
|
-
</Modal>,
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
const matchedModals = document.getElementsByClassName('fadeless-modal');
|
|
213
|
-
const matchedModal = matchedModals[0];
|
|
214
|
-
|
|
215
|
-
expect(matchedModals.length).toBe(1);
|
|
216
|
-
// Modal should not have the 'fade' class
|
|
217
|
-
expect(matchedModal.className.split(' ').indexOf('fade') < 0).toBe(true);
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it('should render when expected when passed modalTransition and backdropTransition props', () => {
|
|
221
|
-
render(
|
|
222
|
-
<Modal
|
|
223
|
-
isOpen
|
|
224
|
-
toggle={toggle}
|
|
225
|
-
modalTransition={{ timeout: 2 }}
|
|
226
|
-
backdropTransition={{ timeout: 10 }}
|
|
227
|
-
modalClassName="custom-timeout-modal"
|
|
228
|
-
>
|
|
229
|
-
Hello, world!
|
|
230
|
-
</Modal>,
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
expect(document.getElementsByClassName('custom-timeout-modal').length).toBe(
|
|
234
|
-
1,
|
|
235
|
-
);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
it('should render with class "modal" and have custom class name if provided with modalClassName', () => {
|
|
239
|
-
render(
|
|
240
|
-
<Modal isOpen toggle={toggle} modalClassName="my-custom-modal">
|
|
241
|
-
Yo!
|
|
242
|
-
</Modal>,
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
expect(document.querySelectorAll('.modal.my-custom-modal').length).toBe(1);
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it('should render with custom class name if provided with wrapClassName', () => {
|
|
249
|
-
render(
|
|
250
|
-
<Modal isOpen toggle={toggle} wrapClassName="my-custom-modal">
|
|
251
|
-
Yo!
|
|
252
|
-
</Modal>,
|
|
253
|
-
);
|
|
254
|
-
|
|
255
|
-
expect(document.getElementsByClassName('my-custom-modal').length).toBe(1);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('should render with class "modal-content" and have custom class name if provided with contentClassName', () => {
|
|
259
|
-
render(
|
|
260
|
-
<Modal isOpen toggle={toggle} contentClassName="my-custom-modal">
|
|
261
|
-
Yo!
|
|
262
|
-
</Modal>,
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
expect(
|
|
266
|
-
document.querySelectorAll('.modal-content.my-custom-modal').length,
|
|
267
|
-
).toBe(1);
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
it('should render with class "modal-backdrop" and have custom class name if provided with backdropClassName', () => {
|
|
271
|
-
render(
|
|
272
|
-
<Modal isOpen toggle={toggle} backdropClassName="my-custom-modal">
|
|
273
|
-
Yo!
|
|
274
|
-
</Modal>,
|
|
275
|
-
);
|
|
276
|
-
|
|
277
|
-
expect(
|
|
278
|
-
document.querySelectorAll('.modal-backdrop.my-custom-modal').length,
|
|
279
|
-
).toBe(1);
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
it('should render with the class "modal-${size}" when size is passed', () => {
|
|
283
|
-
render(
|
|
284
|
-
<Modal isOpen toggle={toggle} size="crazy">
|
|
285
|
-
Yo!
|
|
286
|
-
</Modal>,
|
|
287
|
-
);
|
|
288
|
-
|
|
289
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
290
|
-
expect(document.getElementsByClassName('modal-crazy').length).toBe(1);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it('should render modal when isOpen is true', () => {
|
|
294
|
-
render(
|
|
295
|
-
<Modal isOpen toggle={toggle}>
|
|
296
|
-
Yo!
|
|
297
|
-
</Modal>,
|
|
298
|
-
);
|
|
299
|
-
|
|
300
|
-
expect(document.getElementsByClassName('modal').length).toBe(1);
|
|
301
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
it('should render modal with default role of "dialog"', () => {
|
|
305
|
-
render(
|
|
306
|
-
<Modal isOpen toggle={toggle}>
|
|
307
|
-
Yo!
|
|
308
|
-
</Modal>,
|
|
309
|
-
);
|
|
310
|
-
|
|
311
|
-
expect(
|
|
312
|
-
document.getElementsByClassName('modal')[0].getAttribute('role'),
|
|
313
|
-
).toBe('dialog');
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it('should render modal with provided role', () => {
|
|
317
|
-
render(
|
|
318
|
-
<Modal isOpen toggle={toggle} role="alert">
|
|
319
|
-
Yo!
|
|
320
|
-
</Modal>,
|
|
321
|
-
);
|
|
322
|
-
|
|
323
|
-
expect(
|
|
324
|
-
document.getElementsByClassName('modal')[0].getAttribute('role'),
|
|
325
|
-
).toBe('alert');
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
it('should render modal with aria-labelledby provided labelledBy', () => {
|
|
329
|
-
render(
|
|
330
|
-
<Modal isOpen toggle={toggle} labelledBy="myModalTitle">
|
|
331
|
-
Yo!
|
|
332
|
-
</Modal>,
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
expect(
|
|
336
|
-
document
|
|
337
|
-
.getElementsByClassName('modal')[0]
|
|
338
|
-
.getAttribute('aria-labelledby'),
|
|
339
|
-
).toBe('myModalTitle');
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it('should not render modal when isOpen is false', () => {
|
|
343
|
-
render(
|
|
344
|
-
<Modal isOpen={false} toggle={toggle}>
|
|
345
|
-
Yo!
|
|
346
|
-
</Modal>,
|
|
347
|
-
);
|
|
348
|
-
|
|
349
|
-
expect(document.getElementsByClassName('modal').length).toBe(0);
|
|
350
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(0);
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
it('should toggle modal', () => {
|
|
354
|
-
const { rerender } = render(
|
|
355
|
-
<Modal isOpen={false} toggle={toggle}>
|
|
356
|
-
Yo!
|
|
357
|
-
</Modal>,
|
|
358
|
-
);
|
|
359
|
-
|
|
360
|
-
expect(document.getElementsByClassName('modal').length).toBe(0);
|
|
361
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(0);
|
|
362
|
-
|
|
363
|
-
rerender(
|
|
364
|
-
<Modal isOpen toggle={toggle}>
|
|
365
|
-
Yo!
|
|
366
|
-
</Modal>,
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
expect(document.getElementsByClassName('modal').length).toBe(1);
|
|
370
|
-
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
it('should call onClosed & onOpened', () => {
|
|
374
|
-
const onOpened = jest.fn();
|
|
375
|
-
const onClosed = jest.fn();
|
|
376
|
-
const { rerender } = render(
|
|
377
|
-
<Modal
|
|
378
|
-
isOpen={false}
|
|
379
|
-
onOpened={onOpened}
|
|
380
|
-
onClosed={onClosed}
|
|
381
|
-
toggle={toggle}
|
|
382
|
-
>
|
|
383
|
-
Yo!
|
|
384
|
-
</Modal>,
|
|
385
|
-
);
|
|
386
|
-
|
|
387
|
-
expect(onOpened).not.toHaveBeenCalled();
|
|
388
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
389
|
-
rerender(
|
|
390
|
-
<Modal isOpen onOpened={onOpened} onClosed={onClosed} toggle={toggle}>
|
|
391
|
-
Yo!
|
|
392
|
-
</Modal>,
|
|
393
|
-
);
|
|
394
|
-
act(() => jest.advanceTimersByTime(300));
|
|
395
|
-
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
396
|
-
|
|
397
|
-
rerender(
|
|
398
|
-
<Modal
|
|
399
|
-
isOpen={false}
|
|
400
|
-
onOpened={onOpened}
|
|
401
|
-
onClosed={onClosed}
|
|
402
|
-
toggle={toggle}
|
|
403
|
-
>
|
|
404
|
-
Yo!
|
|
405
|
-
</Modal>,
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
act(() => jest.advanceTimersByTime(300));
|
|
409
|
-
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
410
|
-
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
it('should call onClosed & onOpened when fade={false}', () => {
|
|
414
|
-
const onOpened = jest.fn();
|
|
415
|
-
const onClosed = jest.fn();
|
|
416
|
-
const { rerender } = render(
|
|
417
|
-
<Modal
|
|
418
|
-
isOpen={false}
|
|
419
|
-
onOpened={onOpened}
|
|
420
|
-
onClosed={onClosed}
|
|
421
|
-
toggle={toggle}
|
|
422
|
-
fade={false}
|
|
423
|
-
>
|
|
424
|
-
Yo!
|
|
425
|
-
</Modal>,
|
|
426
|
-
);
|
|
427
|
-
|
|
428
|
-
expect(onOpened).not.toHaveBeenCalled();
|
|
429
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
430
|
-
|
|
431
|
-
rerender(
|
|
432
|
-
<Modal
|
|
433
|
-
isOpen
|
|
434
|
-
onOpened={onOpened}
|
|
435
|
-
onClosed={onClosed}
|
|
436
|
-
toggle={toggle}
|
|
437
|
-
fade={false}
|
|
438
|
-
>
|
|
439
|
-
Yo!
|
|
440
|
-
</Modal>,
|
|
441
|
-
);
|
|
442
|
-
act(() => jest.advanceTimersByTime(1));
|
|
443
|
-
|
|
444
|
-
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
445
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
446
|
-
|
|
447
|
-
rerender(
|
|
448
|
-
<Modal
|
|
449
|
-
isOpen={false}
|
|
450
|
-
onOpened={onOpened}
|
|
451
|
-
onClosed={onClosed}
|
|
452
|
-
toggle={toggle}
|
|
453
|
-
fade={false}
|
|
454
|
-
>
|
|
455
|
-
Yo!
|
|
456
|
-
</Modal>,
|
|
457
|
-
);
|
|
458
|
-
act(() => jest.advanceTimersByTime(1));
|
|
459
|
-
|
|
460
|
-
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
461
|
-
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
it('should call toggle when escape key pressed and not for enter key', () => {
|
|
465
|
-
const toggle = jest.fn();
|
|
466
|
-
render(
|
|
467
|
-
<Modal isOpen toggle={toggle}>
|
|
468
|
-
Yo!
|
|
469
|
-
</Modal>,
|
|
470
|
-
);
|
|
471
|
-
|
|
472
|
-
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Enter', keyCode: 13 });
|
|
473
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
474
|
-
|
|
475
|
-
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
476
|
-
expect(toggle).toHaveBeenCalled();
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
it('should not call toggle when escape key pressed when keyboard is false', () => {
|
|
480
|
-
const toggle = jest.fn();
|
|
481
|
-
render(
|
|
482
|
-
<Modal isOpen toggle={toggle} keyboard={false}>
|
|
483
|
-
Yo!
|
|
484
|
-
</Modal>,
|
|
485
|
-
);
|
|
486
|
-
|
|
487
|
-
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
488
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
it('should call toggle when clicking backdrop', () => {
|
|
492
|
-
const toggle = jest.fn();
|
|
493
|
-
render(
|
|
494
|
-
<Modal isOpen toggle={toggle}>
|
|
495
|
-
<button id="clicker">Does Nothing</button>
|
|
496
|
-
</Modal>,
|
|
497
|
-
);
|
|
498
|
-
|
|
499
|
-
const buttonEl = screen.getByText(/does nothing/i);
|
|
500
|
-
fireEvent.mouseDown(buttonEl);
|
|
501
|
-
fireEvent.click(buttonEl);
|
|
502
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
503
|
-
|
|
504
|
-
const modalEl = document.body.getElementsByClassName('modal')[0];
|
|
505
|
-
fireEvent.mouseDown(modalEl);
|
|
506
|
-
fireEvent.click(modalEl);
|
|
507
|
-
expect(toggle).toHaveBeenCalled();
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
it('should not call toggle when clicking backdrop and backdrop is "static"', () => {
|
|
511
|
-
const toggle = jest.fn();
|
|
512
|
-
render(
|
|
513
|
-
<Modal isOpen toggle={toggle} backdrop="static">
|
|
514
|
-
<button id="clicker">Does Nothing</button>
|
|
515
|
-
</Modal>,
|
|
516
|
-
);
|
|
517
|
-
|
|
518
|
-
fireEvent.click(document.getElementsByClassName('modal-backdrop')[0]);
|
|
519
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
520
|
-
});
|
|
521
|
-
|
|
522
|
-
it('should not call toggle when escape key pressed and backdrop is "static" and keyboard=false', () => {
|
|
523
|
-
const toggle = jest.fn();
|
|
524
|
-
render(
|
|
525
|
-
<Modal isOpen toggle={toggle} backdrop="static" keyboard={false}>
|
|
526
|
-
Yo!
|
|
527
|
-
</Modal>,
|
|
528
|
-
);
|
|
529
|
-
|
|
530
|
-
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
531
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
it('should call toggle when escape key pressed and backdrop is "static" and keyboard=true', () => {
|
|
535
|
-
const toggle = jest.fn();
|
|
536
|
-
render(
|
|
537
|
-
<Modal isOpen toggle={toggle} backdrop="static" keyboard>
|
|
538
|
-
<button id="clicker">Does Nothing</button>
|
|
539
|
-
</Modal>,
|
|
540
|
-
);
|
|
541
|
-
|
|
542
|
-
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
543
|
-
expect(toggle).toHaveBeenCalled();
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
it('should animate when backdrop is "static" and escape key pressed and keyboard=false', () => {
|
|
547
|
-
render(
|
|
548
|
-
<Modal
|
|
549
|
-
isOpen
|
|
550
|
-
toggle={toggle}
|
|
551
|
-
backdrop="static"
|
|
552
|
-
keyboard={false}
|
|
553
|
-
data-testid="mandalorian"
|
|
554
|
-
>
|
|
555
|
-
<button id="clicker">Does Nothing</button>
|
|
556
|
-
</Modal>,
|
|
557
|
-
);
|
|
558
|
-
|
|
559
|
-
act(() => {
|
|
560
|
-
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
expect(screen.getByTestId('mandalorian').parentElement).toHaveClass(
|
|
564
|
-
'modal-static',
|
|
565
|
-
);
|
|
566
|
-
|
|
567
|
-
act(() => jest.advanceTimersByTime(300));
|
|
568
|
-
|
|
569
|
-
expect(screen.getByTestId('mandalorian').parentElement).not.toHaveClass(
|
|
570
|
-
'modal-static',
|
|
571
|
-
);
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
it('should animate when backdrop is "static" and backdrop is clicked', () => {
|
|
575
|
-
render(
|
|
576
|
-
<Modal isOpen toggle={toggle} backdrop="static" data-testid="mandalorian">
|
|
577
|
-
<button id="clicker">Does Nothing</button>
|
|
578
|
-
</Modal>,
|
|
579
|
-
);
|
|
580
|
-
|
|
581
|
-
const backdropModalEl = document.getElementsByClassName('modal')[0];
|
|
582
|
-
act(() => {
|
|
583
|
-
fireEvent.mouseDown(backdropModalEl);
|
|
584
|
-
fireEvent.click(backdropModalEl);
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
expect(screen.getByTestId('mandalorian').parentElement).toHaveClass(
|
|
588
|
-
'modal-static',
|
|
589
|
-
);
|
|
590
|
-
|
|
591
|
-
act(() => jest.advanceTimersByTime(300));
|
|
592
|
-
|
|
593
|
-
expect(screen.getByTestId('mandalorian').parentElement).not.toHaveClass(
|
|
594
|
-
'modal-static',
|
|
595
|
-
);
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
it('should not animate when backdrop is "static" and modal is clicked', () => {
|
|
599
|
-
render(
|
|
600
|
-
<Modal isOpen toggle={toggle} backdrop="static" data-testid="mandalorian">
|
|
601
|
-
<button id="clicker">Does Nothing</button>
|
|
602
|
-
</Modal>,
|
|
603
|
-
);
|
|
604
|
-
|
|
605
|
-
const modalDialogEl = document.getElementsByClassName('modal-dialog')[0];
|
|
606
|
-
fireEvent.mouseDown(modalDialogEl);
|
|
607
|
-
fireEvent.click(modalDialogEl);
|
|
608
|
-
|
|
609
|
-
expect(screen.getByTestId('mandalorian').parentElement).not.toHaveClass(
|
|
610
|
-
'modal-static',
|
|
611
|
-
);
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
it('should destroy this._element', () => {
|
|
615
|
-
const { rerender } = render(
|
|
616
|
-
<Modal isOpen toggle={toggle} wrapClassName="weird-class">
|
|
617
|
-
<button id="clicker">Does Nothing</button>
|
|
618
|
-
</Modal>,
|
|
619
|
-
);
|
|
620
|
-
|
|
621
|
-
const element =
|
|
622
|
-
document.getElementsByClassName('weird-class')[0].parentElement;
|
|
623
|
-
expect(element).toBeInTheDocument();
|
|
624
|
-
|
|
625
|
-
rerender(
|
|
626
|
-
<Modal isOpen={false} toggle={toggle} wrapClassName="weird-class">
|
|
627
|
-
<button id="clicker">Does Nothing</button>
|
|
628
|
-
</Modal>,
|
|
629
|
-
);
|
|
630
|
-
act(() => jest.advanceTimersByTime(300));
|
|
631
|
-
expect(element).not.toBeInTheDocument();
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
it('should destroy this._element when unmountOnClose prop set to true', () => {
|
|
635
|
-
const { rerender } = render(
|
|
636
|
-
<Modal isOpen toggle={toggle} unmountOnClose wrapClassName="weird-class">
|
|
637
|
-
<button id="clicker">Does Nothing</button>
|
|
638
|
-
</Modal>,
|
|
639
|
-
);
|
|
640
|
-
|
|
641
|
-
const element =
|
|
642
|
-
document.getElementsByClassName('weird-class')[0].parentElement;
|
|
643
|
-
expect(element).toBeInTheDocument();
|
|
644
|
-
|
|
645
|
-
rerender(
|
|
646
|
-
<Modal
|
|
647
|
-
isOpen={false}
|
|
648
|
-
toggle={toggle}
|
|
649
|
-
unmountOnClose
|
|
650
|
-
wrapClassName="weird-class"
|
|
651
|
-
>
|
|
652
|
-
<button id="clicker">Does Nothing</button>
|
|
653
|
-
</Modal>,
|
|
654
|
-
);
|
|
655
|
-
act(() => jest.advanceTimersByTime(300));
|
|
656
|
-
expect(element).not.toBeInTheDocument();
|
|
657
|
-
});
|
|
658
|
-
|
|
659
|
-
it('should not destroy this._element when unmountOnClose prop set to false', () => {
|
|
660
|
-
const { rerender } = render(
|
|
661
|
-
<Modal
|
|
662
|
-
isOpen
|
|
663
|
-
toggle={toggle}
|
|
664
|
-
unmountOnClose={false}
|
|
665
|
-
wrapClassName="weird-class"
|
|
666
|
-
>
|
|
667
|
-
<button id="clicker">Does Nothing</button>
|
|
668
|
-
</Modal>,
|
|
669
|
-
);
|
|
670
|
-
|
|
671
|
-
const element =
|
|
672
|
-
document.getElementsByClassName('weird-class')[0].parentElement;
|
|
673
|
-
expect(element).toBeInTheDocument();
|
|
674
|
-
|
|
675
|
-
rerender(
|
|
676
|
-
<Modal
|
|
677
|
-
isOpen={false}
|
|
678
|
-
toggle={toggle}
|
|
679
|
-
unmountOnClose={false}
|
|
680
|
-
wrapClassName="weird-class"
|
|
681
|
-
>
|
|
682
|
-
<button id="clicker">Does Nothing</button>
|
|
683
|
-
</Modal>,
|
|
684
|
-
);
|
|
685
|
-
expect(element).toBeInTheDocument();
|
|
686
|
-
});
|
|
687
|
-
|
|
688
|
-
it('should destroy this._element on unmount', () => {
|
|
689
|
-
const { unmount } = render(
|
|
690
|
-
<Modal isOpen toggle={toggle} wrapClassName="weird-class">
|
|
691
|
-
<button id="clicker">Does Nothing</button>
|
|
692
|
-
</Modal>,
|
|
693
|
-
);
|
|
694
|
-
unmount();
|
|
695
|
-
jest.advanceTimersByTime(300);
|
|
696
|
-
expect(document.getElementsByClassName('modal').length).toBe(0);
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
it('should render nested modals', () => {
|
|
700
|
-
const { unmount } = render(
|
|
701
|
-
<Modal isOpen toggle={toggle}>
|
|
702
|
-
<ModalBody>
|
|
703
|
-
<Modal isOpen toggle={() => {}}>
|
|
704
|
-
Yo!
|
|
705
|
-
</Modal>
|
|
706
|
-
</ModalBody>
|
|
707
|
-
</Modal>,
|
|
708
|
-
);
|
|
709
|
-
|
|
710
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(2);
|
|
711
|
-
expect(document.body.className).toBe('modal-open');
|
|
712
|
-
|
|
713
|
-
unmount();
|
|
714
|
-
|
|
715
|
-
expect(document.getElementsByClassName('modal-dialog').length).toBe(0);
|
|
716
|
-
});
|
|
717
|
-
|
|
718
|
-
it('should remove exactly modal-open class from body', () => {
|
|
719
|
-
// set a body class which includes modal-open
|
|
720
|
-
document.body.className = 'my-modal-opened';
|
|
721
|
-
|
|
722
|
-
const { rerender } = render(
|
|
723
|
-
<Modal isOpen={false} toggle={toggle}>
|
|
724
|
-
Yo!
|
|
725
|
-
</Modal>,
|
|
726
|
-
);
|
|
727
|
-
|
|
728
|
-
expect(document.body.className).toBe('my-modal-opened');
|
|
729
|
-
|
|
730
|
-
rerender(
|
|
731
|
-
<Modal isOpen toggle={toggle}>
|
|
732
|
-
Yo!
|
|
733
|
-
</Modal>,
|
|
734
|
-
);
|
|
735
|
-
|
|
736
|
-
expect(document.body.className).toBe('my-modal-opened modal-open');
|
|
737
|
-
|
|
738
|
-
// using this to test if replace will leave a space when removing modal-open
|
|
739
|
-
document.body.className += ' modal-opened';
|
|
740
|
-
expect(document.body.className).toBe(
|
|
741
|
-
'my-modal-opened modal-open modal-opened',
|
|
742
|
-
);
|
|
743
|
-
|
|
744
|
-
rerender(
|
|
745
|
-
<Modal isOpen={false} toggle={toggle}>
|
|
746
|
-
Yo!
|
|
747
|
-
</Modal>,
|
|
748
|
-
);
|
|
749
|
-
|
|
750
|
-
act(() => jest.advanceTimersByTime(300));
|
|
751
|
-
expect(document.body.className).toBe('my-modal-opened modal-opened');
|
|
752
|
-
});
|
|
753
|
-
|
|
754
|
-
it('should call onEnter & onExit props if provided', () => {
|
|
755
|
-
const onEnter = jest.fn();
|
|
756
|
-
const onExit = jest.fn();
|
|
757
|
-
const { rerender, unmount } = render(
|
|
758
|
-
<Modal isOpen={false} onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
759
|
-
Yo!
|
|
760
|
-
</Modal>,
|
|
761
|
-
);
|
|
762
|
-
|
|
763
|
-
expect(onEnter).toHaveBeenCalled();
|
|
764
|
-
expect(onExit).not.toHaveBeenCalled();
|
|
765
|
-
|
|
766
|
-
onEnter.mockReset();
|
|
767
|
-
onExit.mockReset();
|
|
768
|
-
|
|
769
|
-
rerender(
|
|
770
|
-
<Modal isOpen onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
771
|
-
Yo!
|
|
772
|
-
</Modal>,
|
|
773
|
-
);
|
|
774
|
-
expect(onEnter).not.toHaveBeenCalled();
|
|
775
|
-
expect(onExit).not.toHaveBeenCalled();
|
|
776
|
-
|
|
777
|
-
onEnter.mockReset();
|
|
778
|
-
onExit.mockReset();
|
|
779
|
-
|
|
780
|
-
rerender(
|
|
781
|
-
<Modal isOpen={false} onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
782
|
-
Yo!
|
|
783
|
-
</Modal>,
|
|
784
|
-
);
|
|
785
|
-
unmount();
|
|
786
|
-
expect(onEnter).not.toHaveBeenCalled();
|
|
787
|
-
expect(onExit).toHaveBeenCalled();
|
|
788
|
-
});
|
|
789
|
-
|
|
790
|
-
it('should update element z index when prop changes', () => {
|
|
791
|
-
const { rerender } = render(
|
|
792
|
-
<Modal isOpen zIndex={0} wrapClassName="sandman">
|
|
793
|
-
Yo!
|
|
794
|
-
</Modal>,
|
|
795
|
-
);
|
|
796
|
-
|
|
797
|
-
expect(
|
|
798
|
-
document.getElementsByClassName('sandman')[0].parentElement.style.zIndex,
|
|
799
|
-
).toBe('0');
|
|
800
|
-
|
|
801
|
-
rerender(
|
|
802
|
-
<Modal isOpen zIndex={1} wrapClassName="sandman">
|
|
803
|
-
Yo!
|
|
804
|
-
</Modal>,
|
|
805
|
-
);
|
|
806
|
-
|
|
807
|
-
expect(
|
|
808
|
-
document.getElementsByClassName('sandman')[0].parentElement.style.zIndex,
|
|
809
|
-
).toBe('1');
|
|
810
|
-
});
|
|
811
|
-
|
|
812
|
-
it('should allow focus on only focusable elements and tab through them', async () => {
|
|
813
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
814
|
-
render(
|
|
815
|
-
<Modal isOpen toggle={toggle}>
|
|
816
|
-
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
|
|
817
|
-
<ModalBody>
|
|
818
|
-
<a alt="test" href="/">
|
|
819
|
-
First Test
|
|
820
|
-
</a>
|
|
821
|
-
<map name="test">
|
|
822
|
-
<area alt="test" href="/" coords="200,5,200,30" />
|
|
823
|
-
</map>
|
|
824
|
-
<input type="text" aria-label="test text input" />
|
|
825
|
-
<input type="hidden" />
|
|
826
|
-
<input type="text" disabled value="Test" />
|
|
827
|
-
<select name="test" id="select_test">
|
|
828
|
-
<option>Second item</option>
|
|
829
|
-
</select>
|
|
830
|
-
<select name="test" id="select_test_disabled" disabled>
|
|
831
|
-
<option>Third item</option>
|
|
832
|
-
</select>
|
|
833
|
-
<textarea
|
|
834
|
-
name="textarea_test"
|
|
835
|
-
id="textarea_test"
|
|
836
|
-
cols="30"
|
|
837
|
-
rows="10"
|
|
838
|
-
aria-label="test text area"
|
|
839
|
-
/>
|
|
840
|
-
<textarea
|
|
841
|
-
name="textarea_test_disabled"
|
|
842
|
-
id="textarea_test_disabled"
|
|
843
|
-
cols="30"
|
|
844
|
-
rows="10"
|
|
845
|
-
disabled
|
|
846
|
-
/>
|
|
847
|
-
<object>Test</object>
|
|
848
|
-
<span tabIndex="0">test tab index</span>
|
|
849
|
-
</ModalBody>
|
|
850
|
-
<ModalFooter>
|
|
851
|
-
<Button disabled color="primary" onClick={toggle}>
|
|
852
|
-
Do Something
|
|
853
|
-
</Button>{' '}
|
|
854
|
-
<Button color="secondary" onClick={toggle}>
|
|
855
|
-
Cancel
|
|
856
|
-
</Button>
|
|
857
|
-
</ModalFooter>
|
|
858
|
-
</Modal>,
|
|
859
|
-
);
|
|
860
|
-
|
|
861
|
-
await localUser.tab();
|
|
862
|
-
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
863
|
-
await localUser.tab();
|
|
864
|
-
expect(screen.getByText(/first test/i)).toHaveFocus();
|
|
865
|
-
await localUser.tab();
|
|
866
|
-
expect(screen.getByLabelText(/test text input/i)).toHaveFocus();
|
|
867
|
-
await localUser.tab();
|
|
868
|
-
expect(screen.getByText(/second item/i).parentElement).toHaveFocus();
|
|
869
|
-
await localUser.tab();
|
|
870
|
-
expect(screen.getByLabelText(/test text area/i)).toHaveFocus();
|
|
871
|
-
await localUser.tab();
|
|
872
|
-
expect(screen.getByText(/test tab index/i)).toHaveFocus();
|
|
873
|
-
await localUser.tab();
|
|
874
|
-
expect(screen.getByText(/cancel/i)).toHaveFocus();
|
|
875
|
-
await localUser.tab();
|
|
876
|
-
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
877
|
-
});
|
|
878
|
-
|
|
879
|
-
it('should return the focus to the last focused element before the modal has opened', async () => {
|
|
880
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
881
|
-
const { rerender } = render(
|
|
882
|
-
<>
|
|
883
|
-
<button className="focus">Focused</button>
|
|
884
|
-
<Modal isOpen={false}>
|
|
885
|
-
<ModalBody>Whatever</ModalBody>
|
|
886
|
-
</Modal>
|
|
887
|
-
</>,
|
|
888
|
-
);
|
|
889
|
-
|
|
890
|
-
await localUser.tab();
|
|
891
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
892
|
-
|
|
893
|
-
rerender(
|
|
894
|
-
<>
|
|
895
|
-
<button className="focus">Focused</button>
|
|
896
|
-
<Modal isOpen>
|
|
897
|
-
<ModalBody>Whatever</ModalBody>
|
|
898
|
-
</Modal>
|
|
899
|
-
</>,
|
|
900
|
-
);
|
|
901
|
-
|
|
902
|
-
rerender(
|
|
903
|
-
<>
|
|
904
|
-
<button className="focus">Focused</button>
|
|
905
|
-
<Modal isOpen={false}>
|
|
906
|
-
<ModalBody>Whatever</ModalBody>
|
|
907
|
-
</Modal>
|
|
908
|
-
</>,
|
|
909
|
-
);
|
|
910
|
-
|
|
911
|
-
act(() => jest.runAllTimers());
|
|
912
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
913
|
-
});
|
|
914
|
-
|
|
915
|
-
it('should not return the focus to the last focused element before the modal has opened when "returnFocusAfterClose" is false', async () => {
|
|
916
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
917
|
-
const { rerender } = render(
|
|
918
|
-
<>
|
|
919
|
-
<button className="focus">Focused</button>
|
|
920
|
-
<Modal returnFocusAfterClose={false} isOpen={false}>
|
|
921
|
-
<ModalBody>Whatever</ModalBody>
|
|
922
|
-
</Modal>
|
|
923
|
-
</>,
|
|
924
|
-
);
|
|
925
|
-
|
|
926
|
-
await localUser.tab();
|
|
927
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
928
|
-
|
|
929
|
-
rerender(
|
|
930
|
-
<>
|
|
931
|
-
<button className="focus">Focused</button>
|
|
932
|
-
<Modal returnFocusAfterClose={false} isOpen>
|
|
933
|
-
<ModalBody>Whatever</ModalBody>
|
|
934
|
-
</Modal>
|
|
935
|
-
</>,
|
|
936
|
-
);
|
|
937
|
-
|
|
938
|
-
rerender(
|
|
939
|
-
<>
|
|
940
|
-
<button className="focus">Focused</button>
|
|
941
|
-
<Modal returnFocusAfterClose={false} isOpen={false}>
|
|
942
|
-
<ModalBody>Whatever</ModalBody>
|
|
943
|
-
</Modal>
|
|
944
|
-
</>,
|
|
945
|
-
);
|
|
946
|
-
|
|
947
|
-
act(() => jest.runAllTimers());
|
|
948
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
949
|
-
});
|
|
950
|
-
|
|
951
|
-
it('should return the focus to the last focused element before the modal has opened when "unmountOnClose" is false', async () => {
|
|
952
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
953
|
-
const { rerender } = render(
|
|
954
|
-
<>
|
|
955
|
-
<button className="focus">Focused</button>
|
|
956
|
-
<Modal unmountOnClose={false} isOpen={false}>
|
|
957
|
-
<ModalBody>Whatever</ModalBody>
|
|
958
|
-
</Modal>
|
|
959
|
-
</>,
|
|
960
|
-
);
|
|
961
|
-
|
|
962
|
-
await localUser.tab();
|
|
963
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
964
|
-
|
|
965
|
-
rerender(
|
|
966
|
-
<>
|
|
967
|
-
<button className="focus">Focused</button>
|
|
968
|
-
<Modal unmountOnClose={false} isOpen>
|
|
969
|
-
<ModalBody>Whatever</ModalBody>
|
|
970
|
-
</Modal>
|
|
971
|
-
</>,
|
|
972
|
-
);
|
|
973
|
-
|
|
974
|
-
rerender(
|
|
975
|
-
<>
|
|
976
|
-
<button className="focus">Focused</button>
|
|
977
|
-
<Modal unmountOnClose={false} isOpen={false}>
|
|
978
|
-
<ModalBody>Whatever</ModalBody>
|
|
979
|
-
</Modal>
|
|
980
|
-
</>,
|
|
981
|
-
);
|
|
982
|
-
|
|
983
|
-
act(() => jest.runAllTimers());
|
|
984
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
985
|
-
});
|
|
986
|
-
|
|
987
|
-
it('should not return the focus to the last focused element before the modal has opened when "returnFocusAfterClose" is false and "unmountOnClose" is false', async () => {
|
|
988
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
989
|
-
const { rerender } = render(
|
|
990
|
-
<>
|
|
991
|
-
<button className="focus">Focused</button>
|
|
992
|
-
<Modal
|
|
993
|
-
unmountOnClose={false}
|
|
994
|
-
returnFocusAfterClose={false}
|
|
995
|
-
isOpen={false}
|
|
996
|
-
>
|
|
997
|
-
<ModalBody>Whatever</ModalBody>
|
|
998
|
-
</Modal>
|
|
999
|
-
</>,
|
|
1000
|
-
);
|
|
1001
|
-
|
|
1002
|
-
await localUser.tab();
|
|
1003
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
1004
|
-
|
|
1005
|
-
rerender(
|
|
1006
|
-
<>
|
|
1007
|
-
<button className="focus">Focused</button>
|
|
1008
|
-
<Modal unmountOnClose={false} returnFocusAfterClose={false} isOpen>
|
|
1009
|
-
<ModalBody>Whatever</ModalBody>
|
|
1010
|
-
</Modal>
|
|
1011
|
-
</>,
|
|
1012
|
-
);
|
|
1013
|
-
|
|
1014
|
-
rerender(
|
|
1015
|
-
<>
|
|
1016
|
-
<button className="focus">Focused</button>
|
|
1017
|
-
<Modal
|
|
1018
|
-
unmountOnClose={false}
|
|
1019
|
-
returnFocusAfterClose={false}
|
|
1020
|
-
isOpen={false}
|
|
1021
|
-
>
|
|
1022
|
-
<ModalBody>Whatever</ModalBody>
|
|
1023
|
-
</Modal>
|
|
1024
|
-
</>,
|
|
1025
|
-
);
|
|
1026
|
-
|
|
1027
|
-
act(() => jest.runAllTimers());
|
|
1028
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
1029
|
-
});
|
|
1030
|
-
|
|
1031
|
-
it('should attach/detach trapFocus for dialogs', () => {
|
|
1032
|
-
const addEventListener = jest.spyOn(document, 'addEventListener');
|
|
1033
|
-
const removeEventListener = jest.spyOn(document, 'removeEventListener');
|
|
1034
|
-
|
|
1035
|
-
const { unmount } = render(
|
|
1036
|
-
<Modal isOpen>
|
|
1037
|
-
<ModalBody>
|
|
1038
|
-
<Button className="focus">focusable element</Button>
|
|
1039
|
-
</ModalBody>
|
|
1040
|
-
</Modal>,
|
|
1041
|
-
);
|
|
1042
|
-
|
|
1043
|
-
expect(addEventListener).toHaveBeenCalledTimes(1);
|
|
1044
|
-
expect(addEventListener).toHaveBeenCalledWith(
|
|
1045
|
-
'focus',
|
|
1046
|
-
expect.any(Function),
|
|
1047
|
-
true,
|
|
1048
|
-
);
|
|
1049
|
-
|
|
1050
|
-
unmount();
|
|
1051
|
-
|
|
1052
|
-
expect(removeEventListener).toHaveBeenCalledTimes(1);
|
|
1053
|
-
expect(removeEventListener).toHaveBeenCalledWith(
|
|
1054
|
-
'focus',
|
|
1055
|
-
expect.any(Function),
|
|
1056
|
-
true,
|
|
1057
|
-
);
|
|
1058
|
-
|
|
1059
|
-
addEventListener.mockRestore();
|
|
1060
|
-
removeEventListener.mockRestore();
|
|
1061
|
-
});
|
|
1062
|
-
|
|
1063
|
-
it('should trap focus inside the open dialog', async () => {
|
|
1064
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
1065
|
-
const { rerender } = render(
|
|
1066
|
-
<>
|
|
1067
|
-
<Button className="first">Focused</Button>
|
|
1068
|
-
<Modal isOpen={false} trapFocus>
|
|
1069
|
-
<ModalBody>
|
|
1070
|
-
Something else to see
|
|
1071
|
-
<Button className="focus">focusable element</Button>
|
|
1072
|
-
</ModalBody>
|
|
1073
|
-
</Modal>
|
|
1074
|
-
</>,
|
|
1075
|
-
);
|
|
1076
|
-
|
|
1077
|
-
screen.getByText(/focused/i).focus();
|
|
1078
|
-
|
|
1079
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
1080
|
-
|
|
1081
|
-
rerender(
|
|
1082
|
-
<>
|
|
1083
|
-
<Button className="first">Focused</Button>
|
|
1084
|
-
<Modal isOpen trapFocus data-testid="modal">
|
|
1085
|
-
<ModalBody>
|
|
1086
|
-
Something else to see
|
|
1087
|
-
<Button className="focus">focusable element</Button>
|
|
1088
|
-
</ModalBody>
|
|
1089
|
-
</Modal>
|
|
1090
|
-
</>,
|
|
1091
|
-
);
|
|
1092
|
-
|
|
1093
|
-
act(() => jest.runAllTimers());
|
|
1094
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
1095
|
-
|
|
1096
|
-
expect(screen.getByTestId('modal').parentElement).toHaveFocus();
|
|
1097
|
-
// pressing tab shouldn't move focus outside the modal
|
|
1098
|
-
await localUser.tab();
|
|
1099
|
-
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
1100
|
-
await localUser.tab();
|
|
1101
|
-
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
1102
|
-
});
|
|
1103
|
-
|
|
1104
|
-
it('tab should focus on inside modal children for nested modal', async () => {
|
|
1105
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
1106
|
-
render(
|
|
1107
|
-
<Modal isOpen toggle={toggle}>
|
|
1108
|
-
<ModalBody>
|
|
1109
|
-
<Button className="b0" onClick={toggle}>
|
|
1110
|
-
Cancel
|
|
1111
|
-
</Button>
|
|
1112
|
-
<Modal isOpen>
|
|
1113
|
-
<ModalBody>
|
|
1114
|
-
<Button className="b1">Click 1</Button>
|
|
1115
|
-
</ModalBody>
|
|
1116
|
-
</Modal>
|
|
1117
|
-
</ModalBody>
|
|
1118
|
-
</Modal>,
|
|
1119
|
-
);
|
|
1120
|
-
|
|
1121
|
-
await localUser.tab();
|
|
1122
|
-
expect(screen.getByText(/click 1/i)).toHaveFocus();
|
|
1123
|
-
// pressing tab doesn't take focus out of inside modal
|
|
1124
|
-
await localUser.tab();
|
|
1125
|
-
expect(screen.getByText(/click 1/i)).toHaveFocus();
|
|
1126
|
-
});
|
|
1127
|
-
|
|
1128
|
-
it('works with strict mode', () => {
|
|
1129
|
-
const spy = jest.spyOn(console, 'error');
|
|
1130
|
-
render(
|
|
1131
|
-
<React.StrictMode>
|
|
1132
|
-
<Modal isOpen>Hello</Modal>
|
|
1133
|
-
</React.StrictMode>,
|
|
1134
|
-
);
|
|
1135
|
-
expect(spy).not.toHaveBeenCalled();
|
|
1136
|
-
});
|
|
1137
|
-
});
|
|
1
|
+
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { act, fireEvent, render, screen } from '@testing-library/react';
|
|
4
|
+
import user from '@testing-library/user-event';
|
|
5
|
+
import '@testing-library/jest-dom';
|
|
6
|
+
import { Modal, ModalBody, ModalHeader, ModalFooter, Button } from '..';
|
|
7
|
+
|
|
8
|
+
describe('Modal', () => {
|
|
9
|
+
const toggle = () => {};
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
jest.useFakeTimers();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
jest.clearAllTimers();
|
|
17
|
+
// rtl doesn't clear attributes added to body, so manually clearing them
|
|
18
|
+
document.body.removeAttribute('style');
|
|
19
|
+
document.body.removeAttribute('class');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should render modal portal into DOM', () => {
|
|
23
|
+
render(
|
|
24
|
+
<Modal isOpen toggle={toggle}>
|
|
25
|
+
Yo!
|
|
26
|
+
</Modal>,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
expect(screen.getByText(/yo/i)).toBeInTheDocument();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should render with the class "modal-dialog"', () => {
|
|
33
|
+
render(
|
|
34
|
+
<Modal isOpen toggle={toggle}>
|
|
35
|
+
Yo!
|
|
36
|
+
</Modal>,
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
expect(screen.getByText(/yo/i).parentElement).toHaveClass('modal-dialog');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should render external content when present', () => {
|
|
43
|
+
render(
|
|
44
|
+
<Modal
|
|
45
|
+
isOpen
|
|
46
|
+
toggle={toggle}
|
|
47
|
+
external={<button className="cool-close-button">crazy button</button>}
|
|
48
|
+
>
|
|
49
|
+
Yo!
|
|
50
|
+
</Modal>,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(screen.getByText(/crazy button/i)).toBeInTheDocument();
|
|
54
|
+
expect(
|
|
55
|
+
screen
|
|
56
|
+
.getByText(/crazy button/i)
|
|
57
|
+
.nextElementSibling.className.split(' ')
|
|
58
|
+
.indexOf('modal-dialog') > -1,
|
|
59
|
+
).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should render with the backdrop with the class "modal-backdrop" by default', () => {
|
|
63
|
+
render(
|
|
64
|
+
<Modal isOpen toggle={toggle}>
|
|
65
|
+
Yo!
|
|
66
|
+
</Modal>,
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should render with the backdrop with the class "modal-backdrop" when backdrop is "static"', () => {
|
|
73
|
+
render(
|
|
74
|
+
<Modal isOpen toggle={toggle} backdrop="static">
|
|
75
|
+
Yo!
|
|
76
|
+
</Modal>,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should not render with the backdrop with the class "modal-backdrop" when backdrop is "false"', () => {
|
|
83
|
+
render(
|
|
84
|
+
<Modal isOpen toggle={toggle} backdrop={false}>
|
|
85
|
+
Yo!
|
|
86
|
+
</Modal>,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
90
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(0);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should render with the class "modal-dialog-scrollable" when scrollable is "true"', () => {
|
|
94
|
+
render(
|
|
95
|
+
<Modal isOpen toggle={toggle} scrollable>
|
|
96
|
+
Yo!
|
|
97
|
+
</Modal>,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(
|
|
101
|
+
document.getElementsByClassName('modal-dialog-scrollable').length,
|
|
102
|
+
).toBe(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should render with class "modal-dialog" and have custom class name if provided', () => {
|
|
106
|
+
render(
|
|
107
|
+
<Modal isOpen toggle={toggle} className="my-custom-modal">
|
|
108
|
+
Yo!
|
|
109
|
+
</Modal>,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
113
|
+
expect(document.getElementsByClassName('my-custom-modal').length).toBe(1);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should render with class "modal-dialog" w/o centered class if not provided', () => {
|
|
117
|
+
render(
|
|
118
|
+
<Modal isOpen toggle={toggle}>
|
|
119
|
+
Yo!
|
|
120
|
+
</Modal>,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
jest.advanceTimersByTime(300);
|
|
124
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
125
|
+
expect(
|
|
126
|
+
document.getElementsByClassName('modal-dialog-centered').length,
|
|
127
|
+
).toBe(0);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should render with class "modal-dialog" and centered class if provided', () => {
|
|
131
|
+
render(
|
|
132
|
+
<Modal isOpen toggle={toggle} centered>
|
|
133
|
+
Yo!
|
|
134
|
+
</Modal>,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
138
|
+
expect(
|
|
139
|
+
document.getElementsByClassName('modal-dialog-centered').length,
|
|
140
|
+
).toBe(1);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('fullscreen', () => {
|
|
144
|
+
it('should render non fullscreen by default', () => {
|
|
145
|
+
render(
|
|
146
|
+
<Modal isOpen toggle={toggle}>
|
|
147
|
+
Yo!
|
|
148
|
+
</Modal>,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
152
|
+
expect(document.getElementsByClassName('modal-fullscreen').length).toBe(
|
|
153
|
+
0,
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should always render fullscreen if true', () => {
|
|
158
|
+
render(
|
|
159
|
+
<Modal isOpen toggle={toggle} fullscreen>
|
|
160
|
+
Yo!
|
|
161
|
+
</Modal>,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
165
|
+
expect(document.getElementsByClassName('modal-fullscreen').length).toBe(
|
|
166
|
+
1,
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should render fullscreen below breakpoint if breakpoint is provided', () => {
|
|
171
|
+
render(
|
|
172
|
+
<Modal isOpen toggle={toggle} fullscreen="lg">
|
|
173
|
+
Yo!
|
|
174
|
+
</Modal>,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
178
|
+
expect(document.getElementsByClassName('modal-fullscreen').length).toBe(
|
|
179
|
+
0,
|
|
180
|
+
);
|
|
181
|
+
expect(
|
|
182
|
+
document.getElementsByClassName('modal-fullscreen-lg-down').length,
|
|
183
|
+
).toBe(1);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should render with additional props if provided', () => {
|
|
188
|
+
render(
|
|
189
|
+
<Modal isOpen toggle={toggle} style={{ maxWidth: '95%' }}>
|
|
190
|
+
Yo!
|
|
191
|
+
</Modal>,
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
195
|
+
expect(
|
|
196
|
+
document.getElementsByClassName('modal-dialog')[0].style.maxWidth,
|
|
197
|
+
).toBe('95%');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should render without fade transition if provided with fade={false}', () => {
|
|
201
|
+
render(
|
|
202
|
+
<Modal
|
|
203
|
+
isOpen
|
|
204
|
+
toggle={toggle}
|
|
205
|
+
fade={false}
|
|
206
|
+
modalClassName="fadeless-modal"
|
|
207
|
+
>
|
|
208
|
+
Howdy!
|
|
209
|
+
</Modal>,
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const matchedModals = document.getElementsByClassName('fadeless-modal');
|
|
213
|
+
const matchedModal = matchedModals[0];
|
|
214
|
+
|
|
215
|
+
expect(matchedModals.length).toBe(1);
|
|
216
|
+
// Modal should not have the 'fade' class
|
|
217
|
+
expect(matchedModal.className.split(' ').indexOf('fade') < 0).toBe(true);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should render when expected when passed modalTransition and backdropTransition props', () => {
|
|
221
|
+
render(
|
|
222
|
+
<Modal
|
|
223
|
+
isOpen
|
|
224
|
+
toggle={toggle}
|
|
225
|
+
modalTransition={{ timeout: 2 }}
|
|
226
|
+
backdropTransition={{ timeout: 10 }}
|
|
227
|
+
modalClassName="custom-timeout-modal"
|
|
228
|
+
>
|
|
229
|
+
Hello, world!
|
|
230
|
+
</Modal>,
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
expect(document.getElementsByClassName('custom-timeout-modal').length).toBe(
|
|
234
|
+
1,
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('should render with class "modal" and have custom class name if provided with modalClassName', () => {
|
|
239
|
+
render(
|
|
240
|
+
<Modal isOpen toggle={toggle} modalClassName="my-custom-modal">
|
|
241
|
+
Yo!
|
|
242
|
+
</Modal>,
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
expect(document.querySelectorAll('.modal.my-custom-modal').length).toBe(1);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should render with custom class name if provided with wrapClassName', () => {
|
|
249
|
+
render(
|
|
250
|
+
<Modal isOpen toggle={toggle} wrapClassName="my-custom-modal">
|
|
251
|
+
Yo!
|
|
252
|
+
</Modal>,
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
expect(document.getElementsByClassName('my-custom-modal').length).toBe(1);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should render with class "modal-content" and have custom class name if provided with contentClassName', () => {
|
|
259
|
+
render(
|
|
260
|
+
<Modal isOpen toggle={toggle} contentClassName="my-custom-modal">
|
|
261
|
+
Yo!
|
|
262
|
+
</Modal>,
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
expect(
|
|
266
|
+
document.querySelectorAll('.modal-content.my-custom-modal').length,
|
|
267
|
+
).toBe(1);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('should render with class "modal-backdrop" and have custom class name if provided with backdropClassName', () => {
|
|
271
|
+
render(
|
|
272
|
+
<Modal isOpen toggle={toggle} backdropClassName="my-custom-modal">
|
|
273
|
+
Yo!
|
|
274
|
+
</Modal>,
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
expect(
|
|
278
|
+
document.querySelectorAll('.modal-backdrop.my-custom-modal').length,
|
|
279
|
+
).toBe(1);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should render with the class "modal-${size}" when size is passed', () => {
|
|
283
|
+
render(
|
|
284
|
+
<Modal isOpen toggle={toggle} size="crazy">
|
|
285
|
+
Yo!
|
|
286
|
+
</Modal>,
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(1);
|
|
290
|
+
expect(document.getElementsByClassName('modal-crazy').length).toBe(1);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('should render modal when isOpen is true', () => {
|
|
294
|
+
render(
|
|
295
|
+
<Modal isOpen toggle={toggle}>
|
|
296
|
+
Yo!
|
|
297
|
+
</Modal>,
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
expect(document.getElementsByClassName('modal').length).toBe(1);
|
|
301
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('should render modal with default role of "dialog"', () => {
|
|
305
|
+
render(
|
|
306
|
+
<Modal isOpen toggle={toggle}>
|
|
307
|
+
Yo!
|
|
308
|
+
</Modal>,
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
expect(
|
|
312
|
+
document.getElementsByClassName('modal')[0].getAttribute('role'),
|
|
313
|
+
).toBe('dialog');
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('should render modal with provided role', () => {
|
|
317
|
+
render(
|
|
318
|
+
<Modal isOpen toggle={toggle} role="alert">
|
|
319
|
+
Yo!
|
|
320
|
+
</Modal>,
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
expect(
|
|
324
|
+
document.getElementsByClassName('modal')[0].getAttribute('role'),
|
|
325
|
+
).toBe('alert');
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('should render modal with aria-labelledby provided labelledBy', () => {
|
|
329
|
+
render(
|
|
330
|
+
<Modal isOpen toggle={toggle} labelledBy="myModalTitle">
|
|
331
|
+
Yo!
|
|
332
|
+
</Modal>,
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
expect(
|
|
336
|
+
document
|
|
337
|
+
.getElementsByClassName('modal')[0]
|
|
338
|
+
.getAttribute('aria-labelledby'),
|
|
339
|
+
).toBe('myModalTitle');
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('should not render modal when isOpen is false', () => {
|
|
343
|
+
render(
|
|
344
|
+
<Modal isOpen={false} toggle={toggle}>
|
|
345
|
+
Yo!
|
|
346
|
+
</Modal>,
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
expect(document.getElementsByClassName('modal').length).toBe(0);
|
|
350
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(0);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('should toggle modal', () => {
|
|
354
|
+
const { rerender } = render(
|
|
355
|
+
<Modal isOpen={false} toggle={toggle}>
|
|
356
|
+
Yo!
|
|
357
|
+
</Modal>,
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
expect(document.getElementsByClassName('modal').length).toBe(0);
|
|
361
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(0);
|
|
362
|
+
|
|
363
|
+
rerender(
|
|
364
|
+
<Modal isOpen toggle={toggle}>
|
|
365
|
+
Yo!
|
|
366
|
+
</Modal>,
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
expect(document.getElementsByClassName('modal').length).toBe(1);
|
|
370
|
+
expect(document.getElementsByClassName('modal-backdrop').length).toBe(1);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('should call onClosed & onOpened', () => {
|
|
374
|
+
const onOpened = jest.fn();
|
|
375
|
+
const onClosed = jest.fn();
|
|
376
|
+
const { rerender } = render(
|
|
377
|
+
<Modal
|
|
378
|
+
isOpen={false}
|
|
379
|
+
onOpened={onOpened}
|
|
380
|
+
onClosed={onClosed}
|
|
381
|
+
toggle={toggle}
|
|
382
|
+
>
|
|
383
|
+
Yo!
|
|
384
|
+
</Modal>,
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
expect(onOpened).not.toHaveBeenCalled();
|
|
388
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
389
|
+
rerender(
|
|
390
|
+
<Modal isOpen onOpened={onOpened} onClosed={onClosed} toggle={toggle}>
|
|
391
|
+
Yo!
|
|
392
|
+
</Modal>,
|
|
393
|
+
);
|
|
394
|
+
act(() => jest.advanceTimersByTime(300));
|
|
395
|
+
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
396
|
+
|
|
397
|
+
rerender(
|
|
398
|
+
<Modal
|
|
399
|
+
isOpen={false}
|
|
400
|
+
onOpened={onOpened}
|
|
401
|
+
onClosed={onClosed}
|
|
402
|
+
toggle={toggle}
|
|
403
|
+
>
|
|
404
|
+
Yo!
|
|
405
|
+
</Modal>,
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
act(() => jest.advanceTimersByTime(300));
|
|
409
|
+
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
410
|
+
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it('should call onClosed & onOpened when fade={false}', () => {
|
|
414
|
+
const onOpened = jest.fn();
|
|
415
|
+
const onClosed = jest.fn();
|
|
416
|
+
const { rerender } = render(
|
|
417
|
+
<Modal
|
|
418
|
+
isOpen={false}
|
|
419
|
+
onOpened={onOpened}
|
|
420
|
+
onClosed={onClosed}
|
|
421
|
+
toggle={toggle}
|
|
422
|
+
fade={false}
|
|
423
|
+
>
|
|
424
|
+
Yo!
|
|
425
|
+
</Modal>,
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
expect(onOpened).not.toHaveBeenCalled();
|
|
429
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
430
|
+
|
|
431
|
+
rerender(
|
|
432
|
+
<Modal
|
|
433
|
+
isOpen
|
|
434
|
+
onOpened={onOpened}
|
|
435
|
+
onClosed={onClosed}
|
|
436
|
+
toggle={toggle}
|
|
437
|
+
fade={false}
|
|
438
|
+
>
|
|
439
|
+
Yo!
|
|
440
|
+
</Modal>,
|
|
441
|
+
);
|
|
442
|
+
act(() => jest.advanceTimersByTime(1));
|
|
443
|
+
|
|
444
|
+
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
445
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
446
|
+
|
|
447
|
+
rerender(
|
|
448
|
+
<Modal
|
|
449
|
+
isOpen={false}
|
|
450
|
+
onOpened={onOpened}
|
|
451
|
+
onClosed={onClosed}
|
|
452
|
+
toggle={toggle}
|
|
453
|
+
fade={false}
|
|
454
|
+
>
|
|
455
|
+
Yo!
|
|
456
|
+
</Modal>,
|
|
457
|
+
);
|
|
458
|
+
act(() => jest.advanceTimersByTime(1));
|
|
459
|
+
|
|
460
|
+
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
461
|
+
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('should call toggle when escape key pressed and not for enter key', () => {
|
|
465
|
+
const toggle = jest.fn();
|
|
466
|
+
render(
|
|
467
|
+
<Modal isOpen toggle={toggle}>
|
|
468
|
+
Yo!
|
|
469
|
+
</Modal>,
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Enter', keyCode: 13 });
|
|
473
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
474
|
+
|
|
475
|
+
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
476
|
+
expect(toggle).toHaveBeenCalled();
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it('should not call toggle when escape key pressed when keyboard is false', () => {
|
|
480
|
+
const toggle = jest.fn();
|
|
481
|
+
render(
|
|
482
|
+
<Modal isOpen toggle={toggle} keyboard={false}>
|
|
483
|
+
Yo!
|
|
484
|
+
</Modal>,
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
488
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it('should call toggle when clicking backdrop', () => {
|
|
492
|
+
const toggle = jest.fn();
|
|
493
|
+
render(
|
|
494
|
+
<Modal isOpen toggle={toggle}>
|
|
495
|
+
<button id="clicker">Does Nothing</button>
|
|
496
|
+
</Modal>,
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
const buttonEl = screen.getByText(/does nothing/i);
|
|
500
|
+
fireEvent.mouseDown(buttonEl);
|
|
501
|
+
fireEvent.click(buttonEl);
|
|
502
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
503
|
+
|
|
504
|
+
const modalEl = document.body.getElementsByClassName('modal')[0];
|
|
505
|
+
fireEvent.mouseDown(modalEl);
|
|
506
|
+
fireEvent.click(modalEl);
|
|
507
|
+
expect(toggle).toHaveBeenCalled();
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it('should not call toggle when clicking backdrop and backdrop is "static"', () => {
|
|
511
|
+
const toggle = jest.fn();
|
|
512
|
+
render(
|
|
513
|
+
<Modal isOpen toggle={toggle} backdrop="static">
|
|
514
|
+
<button id="clicker">Does Nothing</button>
|
|
515
|
+
</Modal>,
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
fireEvent.click(document.getElementsByClassName('modal-backdrop')[0]);
|
|
519
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it('should not call toggle when escape key pressed and backdrop is "static" and keyboard=false', () => {
|
|
523
|
+
const toggle = jest.fn();
|
|
524
|
+
render(
|
|
525
|
+
<Modal isOpen toggle={toggle} backdrop="static" keyboard={false}>
|
|
526
|
+
Yo!
|
|
527
|
+
</Modal>,
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
531
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it('should call toggle when escape key pressed and backdrop is "static" and keyboard=true', () => {
|
|
535
|
+
const toggle = jest.fn();
|
|
536
|
+
render(
|
|
537
|
+
<Modal isOpen toggle={toggle} backdrop="static" keyboard>
|
|
538
|
+
<button id="clicker">Does Nothing</button>
|
|
539
|
+
</Modal>,
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
543
|
+
expect(toggle).toHaveBeenCalled();
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it('should animate when backdrop is "static" and escape key pressed and keyboard=false', () => {
|
|
547
|
+
render(
|
|
548
|
+
<Modal
|
|
549
|
+
isOpen
|
|
550
|
+
toggle={toggle}
|
|
551
|
+
backdrop="static"
|
|
552
|
+
keyboard={false}
|
|
553
|
+
data-testid="mandalorian"
|
|
554
|
+
>
|
|
555
|
+
<button id="clicker">Does Nothing</button>
|
|
556
|
+
</Modal>,
|
|
557
|
+
);
|
|
558
|
+
|
|
559
|
+
act(() => {
|
|
560
|
+
fireEvent.keyUp(document.getElementsByClassName('modal')[0], { key: 'Escape', keyCode: 27 });
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
expect(screen.getByTestId('mandalorian').parentElement).toHaveClass(
|
|
564
|
+
'modal-static',
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
act(() => jest.advanceTimersByTime(300));
|
|
568
|
+
|
|
569
|
+
expect(screen.getByTestId('mandalorian').parentElement).not.toHaveClass(
|
|
570
|
+
'modal-static',
|
|
571
|
+
);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it('should animate when backdrop is "static" and backdrop is clicked', () => {
|
|
575
|
+
render(
|
|
576
|
+
<Modal isOpen toggle={toggle} backdrop="static" data-testid="mandalorian">
|
|
577
|
+
<button id="clicker">Does Nothing</button>
|
|
578
|
+
</Modal>,
|
|
579
|
+
);
|
|
580
|
+
|
|
581
|
+
const backdropModalEl = document.getElementsByClassName('modal')[0];
|
|
582
|
+
act(() => {
|
|
583
|
+
fireEvent.mouseDown(backdropModalEl);
|
|
584
|
+
fireEvent.click(backdropModalEl);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
expect(screen.getByTestId('mandalorian').parentElement).toHaveClass(
|
|
588
|
+
'modal-static',
|
|
589
|
+
);
|
|
590
|
+
|
|
591
|
+
act(() => jest.advanceTimersByTime(300));
|
|
592
|
+
|
|
593
|
+
expect(screen.getByTestId('mandalorian').parentElement).not.toHaveClass(
|
|
594
|
+
'modal-static',
|
|
595
|
+
);
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it('should not animate when backdrop is "static" and modal is clicked', () => {
|
|
599
|
+
render(
|
|
600
|
+
<Modal isOpen toggle={toggle} backdrop="static" data-testid="mandalorian">
|
|
601
|
+
<button id="clicker">Does Nothing</button>
|
|
602
|
+
</Modal>,
|
|
603
|
+
);
|
|
604
|
+
|
|
605
|
+
const modalDialogEl = document.getElementsByClassName('modal-dialog')[0];
|
|
606
|
+
fireEvent.mouseDown(modalDialogEl);
|
|
607
|
+
fireEvent.click(modalDialogEl);
|
|
608
|
+
|
|
609
|
+
expect(screen.getByTestId('mandalorian').parentElement).not.toHaveClass(
|
|
610
|
+
'modal-static',
|
|
611
|
+
);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it('should destroy this._element', () => {
|
|
615
|
+
const { rerender } = render(
|
|
616
|
+
<Modal isOpen toggle={toggle} wrapClassName="weird-class">
|
|
617
|
+
<button id="clicker">Does Nothing</button>
|
|
618
|
+
</Modal>,
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
const element =
|
|
622
|
+
document.getElementsByClassName('weird-class')[0].parentElement;
|
|
623
|
+
expect(element).toBeInTheDocument();
|
|
624
|
+
|
|
625
|
+
rerender(
|
|
626
|
+
<Modal isOpen={false} toggle={toggle} wrapClassName="weird-class">
|
|
627
|
+
<button id="clicker">Does Nothing</button>
|
|
628
|
+
</Modal>,
|
|
629
|
+
);
|
|
630
|
+
act(() => jest.advanceTimersByTime(300));
|
|
631
|
+
expect(element).not.toBeInTheDocument();
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
it('should destroy this._element when unmountOnClose prop set to true', () => {
|
|
635
|
+
const { rerender } = render(
|
|
636
|
+
<Modal isOpen toggle={toggle} unmountOnClose wrapClassName="weird-class">
|
|
637
|
+
<button id="clicker">Does Nothing</button>
|
|
638
|
+
</Modal>,
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
const element =
|
|
642
|
+
document.getElementsByClassName('weird-class')[0].parentElement;
|
|
643
|
+
expect(element).toBeInTheDocument();
|
|
644
|
+
|
|
645
|
+
rerender(
|
|
646
|
+
<Modal
|
|
647
|
+
isOpen={false}
|
|
648
|
+
toggle={toggle}
|
|
649
|
+
unmountOnClose
|
|
650
|
+
wrapClassName="weird-class"
|
|
651
|
+
>
|
|
652
|
+
<button id="clicker">Does Nothing</button>
|
|
653
|
+
</Modal>,
|
|
654
|
+
);
|
|
655
|
+
act(() => jest.advanceTimersByTime(300));
|
|
656
|
+
expect(element).not.toBeInTheDocument();
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
it('should not destroy this._element when unmountOnClose prop set to false', () => {
|
|
660
|
+
const { rerender } = render(
|
|
661
|
+
<Modal
|
|
662
|
+
isOpen
|
|
663
|
+
toggle={toggle}
|
|
664
|
+
unmountOnClose={false}
|
|
665
|
+
wrapClassName="weird-class"
|
|
666
|
+
>
|
|
667
|
+
<button id="clicker">Does Nothing</button>
|
|
668
|
+
</Modal>,
|
|
669
|
+
);
|
|
670
|
+
|
|
671
|
+
const element =
|
|
672
|
+
document.getElementsByClassName('weird-class')[0].parentElement;
|
|
673
|
+
expect(element).toBeInTheDocument();
|
|
674
|
+
|
|
675
|
+
rerender(
|
|
676
|
+
<Modal
|
|
677
|
+
isOpen={false}
|
|
678
|
+
toggle={toggle}
|
|
679
|
+
unmountOnClose={false}
|
|
680
|
+
wrapClassName="weird-class"
|
|
681
|
+
>
|
|
682
|
+
<button id="clicker">Does Nothing</button>
|
|
683
|
+
</Modal>,
|
|
684
|
+
);
|
|
685
|
+
expect(element).toBeInTheDocument();
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
it('should destroy this._element on unmount', () => {
|
|
689
|
+
const { unmount } = render(
|
|
690
|
+
<Modal isOpen toggle={toggle} wrapClassName="weird-class">
|
|
691
|
+
<button id="clicker">Does Nothing</button>
|
|
692
|
+
</Modal>,
|
|
693
|
+
);
|
|
694
|
+
unmount();
|
|
695
|
+
jest.advanceTimersByTime(300);
|
|
696
|
+
expect(document.getElementsByClassName('modal').length).toBe(0);
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
it('should render nested modals', () => {
|
|
700
|
+
const { unmount } = render(
|
|
701
|
+
<Modal isOpen toggle={toggle}>
|
|
702
|
+
<ModalBody>
|
|
703
|
+
<Modal isOpen toggle={() => {}}>
|
|
704
|
+
Yo!
|
|
705
|
+
</Modal>
|
|
706
|
+
</ModalBody>
|
|
707
|
+
</Modal>,
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(2);
|
|
711
|
+
expect(document.body.className).toBe('modal-open');
|
|
712
|
+
|
|
713
|
+
unmount();
|
|
714
|
+
|
|
715
|
+
expect(document.getElementsByClassName('modal-dialog').length).toBe(0);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it('should remove exactly modal-open class from body', () => {
|
|
719
|
+
// set a body class which includes modal-open
|
|
720
|
+
document.body.className = 'my-modal-opened';
|
|
721
|
+
|
|
722
|
+
const { rerender } = render(
|
|
723
|
+
<Modal isOpen={false} toggle={toggle}>
|
|
724
|
+
Yo!
|
|
725
|
+
</Modal>,
|
|
726
|
+
);
|
|
727
|
+
|
|
728
|
+
expect(document.body.className).toBe('my-modal-opened');
|
|
729
|
+
|
|
730
|
+
rerender(
|
|
731
|
+
<Modal isOpen toggle={toggle}>
|
|
732
|
+
Yo!
|
|
733
|
+
</Modal>,
|
|
734
|
+
);
|
|
735
|
+
|
|
736
|
+
expect(document.body.className).toBe('my-modal-opened modal-open');
|
|
737
|
+
|
|
738
|
+
// using this to test if replace will leave a space when removing modal-open
|
|
739
|
+
document.body.className += ' modal-opened';
|
|
740
|
+
expect(document.body.className).toBe(
|
|
741
|
+
'my-modal-opened modal-open modal-opened',
|
|
742
|
+
);
|
|
743
|
+
|
|
744
|
+
rerender(
|
|
745
|
+
<Modal isOpen={false} toggle={toggle}>
|
|
746
|
+
Yo!
|
|
747
|
+
</Modal>,
|
|
748
|
+
);
|
|
749
|
+
|
|
750
|
+
act(() => jest.advanceTimersByTime(300));
|
|
751
|
+
expect(document.body.className).toBe('my-modal-opened modal-opened');
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
it('should call onEnter & onExit props if provided', () => {
|
|
755
|
+
const onEnter = jest.fn();
|
|
756
|
+
const onExit = jest.fn();
|
|
757
|
+
const { rerender, unmount } = render(
|
|
758
|
+
<Modal isOpen={false} onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
759
|
+
Yo!
|
|
760
|
+
</Modal>,
|
|
761
|
+
);
|
|
762
|
+
|
|
763
|
+
expect(onEnter).toHaveBeenCalled();
|
|
764
|
+
expect(onExit).not.toHaveBeenCalled();
|
|
765
|
+
|
|
766
|
+
onEnter.mockReset();
|
|
767
|
+
onExit.mockReset();
|
|
768
|
+
|
|
769
|
+
rerender(
|
|
770
|
+
<Modal isOpen onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
771
|
+
Yo!
|
|
772
|
+
</Modal>,
|
|
773
|
+
);
|
|
774
|
+
expect(onEnter).not.toHaveBeenCalled();
|
|
775
|
+
expect(onExit).not.toHaveBeenCalled();
|
|
776
|
+
|
|
777
|
+
onEnter.mockReset();
|
|
778
|
+
onExit.mockReset();
|
|
779
|
+
|
|
780
|
+
rerender(
|
|
781
|
+
<Modal isOpen={false} onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
782
|
+
Yo!
|
|
783
|
+
</Modal>,
|
|
784
|
+
);
|
|
785
|
+
unmount();
|
|
786
|
+
expect(onEnter).not.toHaveBeenCalled();
|
|
787
|
+
expect(onExit).toHaveBeenCalled();
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it('should update element z index when prop changes', () => {
|
|
791
|
+
const { rerender } = render(
|
|
792
|
+
<Modal isOpen zIndex={0} wrapClassName="sandman">
|
|
793
|
+
Yo!
|
|
794
|
+
</Modal>,
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
expect(
|
|
798
|
+
document.getElementsByClassName('sandman')[0].parentElement.style.zIndex,
|
|
799
|
+
).toBe('0');
|
|
800
|
+
|
|
801
|
+
rerender(
|
|
802
|
+
<Modal isOpen zIndex={1} wrapClassName="sandman">
|
|
803
|
+
Yo!
|
|
804
|
+
</Modal>,
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
expect(
|
|
808
|
+
document.getElementsByClassName('sandman')[0].parentElement.style.zIndex,
|
|
809
|
+
).toBe('1');
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
it('should allow focus on only focusable elements and tab through them', async () => {
|
|
813
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
814
|
+
render(
|
|
815
|
+
<Modal isOpen toggle={toggle}>
|
|
816
|
+
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
|
|
817
|
+
<ModalBody>
|
|
818
|
+
<a alt="test" href="/">
|
|
819
|
+
First Test
|
|
820
|
+
</a>
|
|
821
|
+
<map name="test">
|
|
822
|
+
<area alt="test" href="/" coords="200,5,200,30" />
|
|
823
|
+
</map>
|
|
824
|
+
<input type="text" aria-label="test text input" />
|
|
825
|
+
<input type="hidden" />
|
|
826
|
+
<input type="text" disabled value="Test" />
|
|
827
|
+
<select name="test" id="select_test">
|
|
828
|
+
<option>Second item</option>
|
|
829
|
+
</select>
|
|
830
|
+
<select name="test" id="select_test_disabled" disabled>
|
|
831
|
+
<option>Third item</option>
|
|
832
|
+
</select>
|
|
833
|
+
<textarea
|
|
834
|
+
name="textarea_test"
|
|
835
|
+
id="textarea_test"
|
|
836
|
+
cols="30"
|
|
837
|
+
rows="10"
|
|
838
|
+
aria-label="test text area"
|
|
839
|
+
/>
|
|
840
|
+
<textarea
|
|
841
|
+
name="textarea_test_disabled"
|
|
842
|
+
id="textarea_test_disabled"
|
|
843
|
+
cols="30"
|
|
844
|
+
rows="10"
|
|
845
|
+
disabled
|
|
846
|
+
/>
|
|
847
|
+
<object>Test</object>
|
|
848
|
+
<span tabIndex="0">test tab index</span>
|
|
849
|
+
</ModalBody>
|
|
850
|
+
<ModalFooter>
|
|
851
|
+
<Button disabled color="primary" onClick={toggle}>
|
|
852
|
+
Do Something
|
|
853
|
+
</Button>{' '}
|
|
854
|
+
<Button color="secondary" onClick={toggle}>
|
|
855
|
+
Cancel
|
|
856
|
+
</Button>
|
|
857
|
+
</ModalFooter>
|
|
858
|
+
</Modal>,
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
await localUser.tab();
|
|
862
|
+
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
863
|
+
await localUser.tab();
|
|
864
|
+
expect(screen.getByText(/first test/i)).toHaveFocus();
|
|
865
|
+
await localUser.tab();
|
|
866
|
+
expect(screen.getByLabelText(/test text input/i)).toHaveFocus();
|
|
867
|
+
await localUser.tab();
|
|
868
|
+
expect(screen.getByText(/second item/i).parentElement).toHaveFocus();
|
|
869
|
+
await localUser.tab();
|
|
870
|
+
expect(screen.getByLabelText(/test text area/i)).toHaveFocus();
|
|
871
|
+
await localUser.tab();
|
|
872
|
+
expect(screen.getByText(/test tab index/i)).toHaveFocus();
|
|
873
|
+
await localUser.tab();
|
|
874
|
+
expect(screen.getByText(/cancel/i)).toHaveFocus();
|
|
875
|
+
await localUser.tab();
|
|
876
|
+
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
it('should return the focus to the last focused element before the modal has opened', async () => {
|
|
880
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
881
|
+
const { rerender } = render(
|
|
882
|
+
<>
|
|
883
|
+
<button className="focus">Focused</button>
|
|
884
|
+
<Modal isOpen={false}>
|
|
885
|
+
<ModalBody>Whatever</ModalBody>
|
|
886
|
+
</Modal>
|
|
887
|
+
</>,
|
|
888
|
+
);
|
|
889
|
+
|
|
890
|
+
await localUser.tab();
|
|
891
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
892
|
+
|
|
893
|
+
rerender(
|
|
894
|
+
<>
|
|
895
|
+
<button className="focus">Focused</button>
|
|
896
|
+
<Modal isOpen>
|
|
897
|
+
<ModalBody>Whatever</ModalBody>
|
|
898
|
+
</Modal>
|
|
899
|
+
</>,
|
|
900
|
+
);
|
|
901
|
+
|
|
902
|
+
rerender(
|
|
903
|
+
<>
|
|
904
|
+
<button className="focus">Focused</button>
|
|
905
|
+
<Modal isOpen={false}>
|
|
906
|
+
<ModalBody>Whatever</ModalBody>
|
|
907
|
+
</Modal>
|
|
908
|
+
</>,
|
|
909
|
+
);
|
|
910
|
+
|
|
911
|
+
act(() => jest.runAllTimers());
|
|
912
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
it('should not return the focus to the last focused element before the modal has opened when "returnFocusAfterClose" is false', async () => {
|
|
916
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
917
|
+
const { rerender } = render(
|
|
918
|
+
<>
|
|
919
|
+
<button className="focus">Focused</button>
|
|
920
|
+
<Modal returnFocusAfterClose={false} isOpen={false}>
|
|
921
|
+
<ModalBody>Whatever</ModalBody>
|
|
922
|
+
</Modal>
|
|
923
|
+
</>,
|
|
924
|
+
);
|
|
925
|
+
|
|
926
|
+
await localUser.tab();
|
|
927
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
928
|
+
|
|
929
|
+
rerender(
|
|
930
|
+
<>
|
|
931
|
+
<button className="focus">Focused</button>
|
|
932
|
+
<Modal returnFocusAfterClose={false} isOpen>
|
|
933
|
+
<ModalBody>Whatever</ModalBody>
|
|
934
|
+
</Modal>
|
|
935
|
+
</>,
|
|
936
|
+
);
|
|
937
|
+
|
|
938
|
+
rerender(
|
|
939
|
+
<>
|
|
940
|
+
<button className="focus">Focused</button>
|
|
941
|
+
<Modal returnFocusAfterClose={false} isOpen={false}>
|
|
942
|
+
<ModalBody>Whatever</ModalBody>
|
|
943
|
+
</Modal>
|
|
944
|
+
</>,
|
|
945
|
+
);
|
|
946
|
+
|
|
947
|
+
act(() => jest.runAllTimers());
|
|
948
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
it('should return the focus to the last focused element before the modal has opened when "unmountOnClose" is false', async () => {
|
|
952
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
953
|
+
const { rerender } = render(
|
|
954
|
+
<>
|
|
955
|
+
<button className="focus">Focused</button>
|
|
956
|
+
<Modal unmountOnClose={false} isOpen={false}>
|
|
957
|
+
<ModalBody>Whatever</ModalBody>
|
|
958
|
+
</Modal>
|
|
959
|
+
</>,
|
|
960
|
+
);
|
|
961
|
+
|
|
962
|
+
await localUser.tab();
|
|
963
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
964
|
+
|
|
965
|
+
rerender(
|
|
966
|
+
<>
|
|
967
|
+
<button className="focus">Focused</button>
|
|
968
|
+
<Modal unmountOnClose={false} isOpen>
|
|
969
|
+
<ModalBody>Whatever</ModalBody>
|
|
970
|
+
</Modal>
|
|
971
|
+
</>,
|
|
972
|
+
);
|
|
973
|
+
|
|
974
|
+
rerender(
|
|
975
|
+
<>
|
|
976
|
+
<button className="focus">Focused</button>
|
|
977
|
+
<Modal unmountOnClose={false} isOpen={false}>
|
|
978
|
+
<ModalBody>Whatever</ModalBody>
|
|
979
|
+
</Modal>
|
|
980
|
+
</>,
|
|
981
|
+
);
|
|
982
|
+
|
|
983
|
+
act(() => jest.runAllTimers());
|
|
984
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
it('should not return the focus to the last focused element before the modal has opened when "returnFocusAfterClose" is false and "unmountOnClose" is false', async () => {
|
|
988
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
989
|
+
const { rerender } = render(
|
|
990
|
+
<>
|
|
991
|
+
<button className="focus">Focused</button>
|
|
992
|
+
<Modal
|
|
993
|
+
unmountOnClose={false}
|
|
994
|
+
returnFocusAfterClose={false}
|
|
995
|
+
isOpen={false}
|
|
996
|
+
>
|
|
997
|
+
<ModalBody>Whatever</ModalBody>
|
|
998
|
+
</Modal>
|
|
999
|
+
</>,
|
|
1000
|
+
);
|
|
1001
|
+
|
|
1002
|
+
await localUser.tab();
|
|
1003
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
1004
|
+
|
|
1005
|
+
rerender(
|
|
1006
|
+
<>
|
|
1007
|
+
<button className="focus">Focused</button>
|
|
1008
|
+
<Modal unmountOnClose={false} returnFocusAfterClose={false} isOpen>
|
|
1009
|
+
<ModalBody>Whatever</ModalBody>
|
|
1010
|
+
</Modal>
|
|
1011
|
+
</>,
|
|
1012
|
+
);
|
|
1013
|
+
|
|
1014
|
+
rerender(
|
|
1015
|
+
<>
|
|
1016
|
+
<button className="focus">Focused</button>
|
|
1017
|
+
<Modal
|
|
1018
|
+
unmountOnClose={false}
|
|
1019
|
+
returnFocusAfterClose={false}
|
|
1020
|
+
isOpen={false}
|
|
1021
|
+
>
|
|
1022
|
+
<ModalBody>Whatever</ModalBody>
|
|
1023
|
+
</Modal>
|
|
1024
|
+
</>,
|
|
1025
|
+
);
|
|
1026
|
+
|
|
1027
|
+
act(() => jest.runAllTimers());
|
|
1028
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
it('should attach/detach trapFocus for dialogs', () => {
|
|
1032
|
+
const addEventListener = jest.spyOn(document, 'addEventListener');
|
|
1033
|
+
const removeEventListener = jest.spyOn(document, 'removeEventListener');
|
|
1034
|
+
|
|
1035
|
+
const { unmount } = render(
|
|
1036
|
+
<Modal isOpen>
|
|
1037
|
+
<ModalBody>
|
|
1038
|
+
<Button className="focus">focusable element</Button>
|
|
1039
|
+
</ModalBody>
|
|
1040
|
+
</Modal>,
|
|
1041
|
+
);
|
|
1042
|
+
|
|
1043
|
+
expect(addEventListener).toHaveBeenCalledTimes(1);
|
|
1044
|
+
expect(addEventListener).toHaveBeenCalledWith(
|
|
1045
|
+
'focus',
|
|
1046
|
+
expect.any(Function),
|
|
1047
|
+
true,
|
|
1048
|
+
);
|
|
1049
|
+
|
|
1050
|
+
unmount();
|
|
1051
|
+
|
|
1052
|
+
expect(removeEventListener).toHaveBeenCalledTimes(1);
|
|
1053
|
+
expect(removeEventListener).toHaveBeenCalledWith(
|
|
1054
|
+
'focus',
|
|
1055
|
+
expect.any(Function),
|
|
1056
|
+
true,
|
|
1057
|
+
);
|
|
1058
|
+
|
|
1059
|
+
addEventListener.mockRestore();
|
|
1060
|
+
removeEventListener.mockRestore();
|
|
1061
|
+
});
|
|
1062
|
+
|
|
1063
|
+
it('should trap focus inside the open dialog', async () => {
|
|
1064
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
1065
|
+
const { rerender } = render(
|
|
1066
|
+
<>
|
|
1067
|
+
<Button className="first">Focused</Button>
|
|
1068
|
+
<Modal isOpen={false} trapFocus>
|
|
1069
|
+
<ModalBody>
|
|
1070
|
+
Something else to see
|
|
1071
|
+
<Button className="focus">focusable element</Button>
|
|
1072
|
+
</ModalBody>
|
|
1073
|
+
</Modal>
|
|
1074
|
+
</>,
|
|
1075
|
+
);
|
|
1076
|
+
|
|
1077
|
+
screen.getByText(/focused/i).focus();
|
|
1078
|
+
|
|
1079
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
1080
|
+
|
|
1081
|
+
rerender(
|
|
1082
|
+
<>
|
|
1083
|
+
<Button className="first">Focused</Button>
|
|
1084
|
+
<Modal isOpen trapFocus data-testid="modal">
|
|
1085
|
+
<ModalBody>
|
|
1086
|
+
Something else to see
|
|
1087
|
+
<Button className="focus">focusable element</Button>
|
|
1088
|
+
</ModalBody>
|
|
1089
|
+
</Modal>
|
|
1090
|
+
</>,
|
|
1091
|
+
);
|
|
1092
|
+
|
|
1093
|
+
act(() => jest.runAllTimers());
|
|
1094
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
1095
|
+
|
|
1096
|
+
expect(screen.getByTestId('modal').parentElement).toHaveFocus();
|
|
1097
|
+
// pressing tab shouldn't move focus outside the modal
|
|
1098
|
+
await localUser.tab();
|
|
1099
|
+
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
1100
|
+
await localUser.tab();
|
|
1101
|
+
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
it('tab should focus on inside modal children for nested modal', async () => {
|
|
1105
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
1106
|
+
render(
|
|
1107
|
+
<Modal isOpen toggle={toggle}>
|
|
1108
|
+
<ModalBody>
|
|
1109
|
+
<Button className="b0" onClick={toggle}>
|
|
1110
|
+
Cancel
|
|
1111
|
+
</Button>
|
|
1112
|
+
<Modal isOpen>
|
|
1113
|
+
<ModalBody>
|
|
1114
|
+
<Button className="b1">Click 1</Button>
|
|
1115
|
+
</ModalBody>
|
|
1116
|
+
</Modal>
|
|
1117
|
+
</ModalBody>
|
|
1118
|
+
</Modal>,
|
|
1119
|
+
);
|
|
1120
|
+
|
|
1121
|
+
await localUser.tab();
|
|
1122
|
+
expect(screen.getByText(/click 1/i)).toHaveFocus();
|
|
1123
|
+
// pressing tab doesn't take focus out of inside modal
|
|
1124
|
+
await localUser.tab();
|
|
1125
|
+
expect(screen.getByText(/click 1/i)).toHaveFocus();
|
|
1126
|
+
});
|
|
1127
|
+
|
|
1128
|
+
it('works with strict mode', () => {
|
|
1129
|
+
const spy = jest.spyOn(console, 'error');
|
|
1130
|
+
render(
|
|
1131
|
+
<React.StrictMode>
|
|
1132
|
+
<Modal isOpen>Hello</Modal>
|
|
1133
|
+
</React.StrictMode>,
|
|
1134
|
+
);
|
|
1135
|
+
expect(spy).not.toHaveBeenCalled();
|
|
1136
|
+
});
|
|
1137
|
+
});
|