@collana-solutions/reactstrap 10.0.0 → 10.0.1
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 +26 -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/DropdownContext.js +9 -9
- package/esm/TabContext.js +5 -5
- package/esm/__tests__/Tabs.spec.js +4 -4
- 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/DropdownContext.js +9 -9
- 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,840 +1,840 @@
|
|
|
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 { Offcanvas, OffcanvasBody, OffcanvasHeader, Button } from '..';
|
|
7
|
-
import { testForCustomClass } from '../testUtils';
|
|
8
|
-
|
|
9
|
-
describe('Offcanvas', () => {
|
|
10
|
-
let toggle;
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
toggle = () => {};
|
|
13
|
-
jest.useFakeTimers();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
jest.clearAllTimers();
|
|
18
|
-
document.body.removeAttribute('style');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('should render offcanvas portal into DOM', () => {
|
|
22
|
-
render(
|
|
23
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
24
|
-
Yo!
|
|
25
|
-
</Offcanvas>,
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
jest.advanceTimersByTime(300);
|
|
29
|
-
|
|
30
|
-
expect(screen.getByText(/yo/i)).toBeInTheDocument();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should render with the class "offcanvas"', () => {
|
|
34
|
-
render(
|
|
35
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
36
|
-
Yo!
|
|
37
|
-
</Offcanvas>,
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
expect(screen.getByText(/yo/i)).toHaveClass('offcanvas');
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should render with the backdrop with the class "offcanvas-backdrop" by default', () => {
|
|
44
|
-
render(
|
|
45
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
46
|
-
Yo!
|
|
47
|
-
</Offcanvas>,
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
expect(document.getElementsByClassName('offcanvas-backdrop')).toHaveLength(
|
|
51
|
-
1,
|
|
52
|
-
);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('should not render with the backdrop with the class "offcanvas-backdrop" when backdrop is "false"', () => {
|
|
56
|
-
render(
|
|
57
|
-
<Offcanvas isOpen toggle={toggle} backdrop={false}>
|
|
58
|
-
Yo!
|
|
59
|
-
</Offcanvas>,
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
expect(document.getElementsByClassName('offcanvas').length).toBe(1);
|
|
63
|
-
expect(document.getElementsByClassName('offcanvas-backdrop').length).toBe(
|
|
64
|
-
0,
|
|
65
|
-
);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should have custom class name if provided', () => {
|
|
69
|
-
testForCustomClass(Offcanvas, { isOpen: true, toggle });
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should render with additional props if provided', () => {
|
|
73
|
-
render(
|
|
74
|
-
<Offcanvas isOpen toggle={toggle} style={{ maxWidth: '95%' }}>
|
|
75
|
-
Yo!
|
|
76
|
-
</Offcanvas>,
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
expect(document.getElementsByClassName('offcanvas')[0].style.maxWidth).toBe(
|
|
80
|
-
'95%',
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('should render without fade transition if provided with fade={false}', () => {
|
|
85
|
-
render(
|
|
86
|
-
<Offcanvas
|
|
87
|
-
isOpen
|
|
88
|
-
toggle={toggle}
|
|
89
|
-
fade={false}
|
|
90
|
-
className="fadeless-offcanvas"
|
|
91
|
-
>
|
|
92
|
-
Howdy!
|
|
93
|
-
</Offcanvas>,
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
expect(
|
|
97
|
-
document.getElementsByClassName('fadeless-offcanvas')[0],
|
|
98
|
-
).not.toHaveClass('fade');
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should render when expected when passed offcanvasTransition and backdropTransition props', () => {
|
|
102
|
-
render(
|
|
103
|
-
<Offcanvas
|
|
104
|
-
isOpen
|
|
105
|
-
toggle={toggle}
|
|
106
|
-
offcanvasTransition={{ timeout: 2 }}
|
|
107
|
-
backdropTransition={{ timeout: 10 }}
|
|
108
|
-
className="custom-timeout-offcanvas"
|
|
109
|
-
>
|
|
110
|
-
Hello, world!
|
|
111
|
-
</Offcanvas>,
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
expect(
|
|
115
|
-
document.getElementsByClassName('custom-timeout-offcanvas')[0],
|
|
116
|
-
).toHaveClass('fade');
|
|
117
|
-
expect(
|
|
118
|
-
document.getElementsByClassName('custom-timeout-offcanvas')[0],
|
|
119
|
-
).not.toHaveClass('show');
|
|
120
|
-
expect(
|
|
121
|
-
document.getElementsByClassName('offcanvas-backdrop')[0],
|
|
122
|
-
).not.toHaveClass('show');
|
|
123
|
-
|
|
124
|
-
act(() => jest.advanceTimersByTime(20));
|
|
125
|
-
expect(
|
|
126
|
-
document.getElementsByClassName('custom-timeout-offcanvas')[0],
|
|
127
|
-
).toHaveClass('show');
|
|
128
|
-
expect(
|
|
129
|
-
document.getElementsByClassName('offcanvas-backdrop')[0],
|
|
130
|
-
).toHaveClass('show');
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('should render with class "offcanvas-backdrop" and have custom class name if provided with backdropClassName', () => {
|
|
134
|
-
render(
|
|
135
|
-
<Offcanvas isOpen toggle={toggle} backdropClassName="my-custom-offcanvas">
|
|
136
|
-
Yo!
|
|
137
|
-
</Offcanvas>,
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
expect(
|
|
141
|
-
document.getElementsByClassName(
|
|
142
|
-
'offcanvas-backdrop my-custom-offcanvas',
|
|
143
|
-
)[0],
|
|
144
|
-
).toBeInTheDocument();
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('should render with the class "offcanvas-${direction}" when direction is passed', () => {
|
|
148
|
-
render(
|
|
149
|
-
<Offcanvas isOpen toggle={toggle} direction="top">
|
|
150
|
-
Yo!
|
|
151
|
-
</Offcanvas>,
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
expect(screen.getByText(/yo/i)).toHaveClass('offcanvas-top');
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should render offcanvas when isOpen is true', () => {
|
|
158
|
-
render(
|
|
159
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
160
|
-
Yo!
|
|
161
|
-
</Offcanvas>,
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
expect(screen.getByText(/yo/i)).toHaveClass('offcanvas');
|
|
165
|
-
expect(document.getElementsByClassName('offcanvas-backdrop').length).toBe(
|
|
166
|
-
1,
|
|
167
|
-
);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('should render offcanvas with default role of "dialog"', () => {
|
|
171
|
-
render(
|
|
172
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
173
|
-
Yo!
|
|
174
|
-
</Offcanvas>,
|
|
175
|
-
);
|
|
176
|
-
expect(screen.getByText(/yo/i).getAttribute('role')).toBe('dialog');
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('should render offcanvas with provided role', () => {
|
|
180
|
-
render(
|
|
181
|
-
<Offcanvas isOpen toggle={toggle} role="alert">
|
|
182
|
-
Yo!
|
|
183
|
-
</Offcanvas>,
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
expect(screen.getByText(/yo/i).getAttribute('role')).toBe('alert');
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('should render offcanvas with aria-labelledby provided labelledBy', () => {
|
|
190
|
-
render(
|
|
191
|
-
<Offcanvas isOpen toggle={toggle} labelledBy="myOffcanvasTitle">
|
|
192
|
-
Yo!
|
|
193
|
-
</Offcanvas>,
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
expect(screen.getByText(/yo/i).getAttribute('aria-labelledby')).toBe(
|
|
197
|
-
'myOffcanvasTitle',
|
|
198
|
-
);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('should not render offcanvas when isOpen is false', () => {
|
|
202
|
-
render(
|
|
203
|
-
<Offcanvas isOpen={false} toggle={toggle}>
|
|
204
|
-
Yo!
|
|
205
|
-
</Offcanvas>,
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
expect(screen.queryByText(/yo/i)).not.toBeInTheDocument();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('should toggle offcanvas', () => {
|
|
212
|
-
const { rerender } = render(
|
|
213
|
-
<Offcanvas isOpen={false} toggle={toggle}>
|
|
214
|
-
Yo!
|
|
215
|
-
</Offcanvas>,
|
|
216
|
-
);
|
|
217
|
-
|
|
218
|
-
expect(screen.queryByText(/yo/i)).not.toBeInTheDocument();
|
|
219
|
-
|
|
220
|
-
rerender(
|
|
221
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
222
|
-
Yo!
|
|
223
|
-
</Offcanvas>,
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
expect(screen.queryByText(/yo/i)).toBeInTheDocument();
|
|
227
|
-
|
|
228
|
-
rerender(
|
|
229
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
230
|
-
Yo!
|
|
231
|
-
</Offcanvas>,
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
expect(screen.queryByText(/yo/i)).toBeInTheDocument();
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it('should call onClosed & onOpened', () => {
|
|
238
|
-
const onOpened = jest.fn();
|
|
239
|
-
const onClosed = jest.fn();
|
|
240
|
-
const { rerender } = render(
|
|
241
|
-
<Offcanvas
|
|
242
|
-
isOpen={false}
|
|
243
|
-
onOpened={onOpened}
|
|
244
|
-
onClosed={onClosed}
|
|
245
|
-
toggle={toggle}
|
|
246
|
-
>
|
|
247
|
-
Yo!
|
|
248
|
-
</Offcanvas>,
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
expect(onOpened).not.toHaveBeenCalled();
|
|
252
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
253
|
-
|
|
254
|
-
rerender(
|
|
255
|
-
<Offcanvas isOpen onOpened={onOpened} onClosed={onClosed} toggle={toggle}>
|
|
256
|
-
Yo!
|
|
257
|
-
</Offcanvas>,
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
act(() => jest.advanceTimersByTime(300));
|
|
261
|
-
|
|
262
|
-
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
263
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
264
|
-
|
|
265
|
-
onOpened.mockClear();
|
|
266
|
-
onClosed.mockClear();
|
|
267
|
-
|
|
268
|
-
rerender(
|
|
269
|
-
<Offcanvas
|
|
270
|
-
isOpen={false}
|
|
271
|
-
onOpened={onOpened}
|
|
272
|
-
onClosed={onClosed}
|
|
273
|
-
toggle={toggle}
|
|
274
|
-
>
|
|
275
|
-
Yo!
|
|
276
|
-
</Offcanvas>,
|
|
277
|
-
);
|
|
278
|
-
|
|
279
|
-
act(() => jest.advanceTimersByTime(300));
|
|
280
|
-
|
|
281
|
-
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
282
|
-
expect(onOpened).not.toHaveBeenCalled();
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it('should call onClosed & onOpened when fade={false}', () => {
|
|
286
|
-
const onOpened = jest.fn();
|
|
287
|
-
const onClosed = jest.fn();
|
|
288
|
-
const { rerender } = render(
|
|
289
|
-
<Offcanvas
|
|
290
|
-
isOpen={false}
|
|
291
|
-
onOpened={onOpened}
|
|
292
|
-
onClosed={onClosed}
|
|
293
|
-
toggle={toggle}
|
|
294
|
-
fade={false}
|
|
295
|
-
>
|
|
296
|
-
Yo!
|
|
297
|
-
</Offcanvas>,
|
|
298
|
-
);
|
|
299
|
-
|
|
300
|
-
expect(onOpened).not.toHaveBeenCalled();
|
|
301
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
302
|
-
|
|
303
|
-
rerender(
|
|
304
|
-
<Offcanvas
|
|
305
|
-
isOpen
|
|
306
|
-
onOpened={onOpened}
|
|
307
|
-
onClosed={onClosed}
|
|
308
|
-
toggle={toggle}
|
|
309
|
-
fade={false}
|
|
310
|
-
>
|
|
311
|
-
Yo!
|
|
312
|
-
</Offcanvas>,
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
act(() => jest.advanceTimersByTime(300));
|
|
316
|
-
|
|
317
|
-
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
318
|
-
expect(onClosed).not.toHaveBeenCalled();
|
|
319
|
-
|
|
320
|
-
onOpened.mockClear();
|
|
321
|
-
onClosed.mockClear();
|
|
322
|
-
|
|
323
|
-
rerender(
|
|
324
|
-
<Offcanvas
|
|
325
|
-
isOpen={false}
|
|
326
|
-
onOpened={onOpened}
|
|
327
|
-
onClosed={onClosed}
|
|
328
|
-
toggle={toggle}
|
|
329
|
-
fade={false}
|
|
330
|
-
>
|
|
331
|
-
Yo!
|
|
332
|
-
</Offcanvas>,
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
act(() => jest.advanceTimersByTime(300));
|
|
336
|
-
|
|
337
|
-
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
338
|
-
expect(onOpened).not.toHaveBeenCalled();
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
it('should call toggle when escape key pressed', () => {
|
|
342
|
-
const toggle = jest.fn();
|
|
343
|
-
render(
|
|
344
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
345
|
-
Yo!
|
|
346
|
-
</Offcanvas>,
|
|
347
|
-
);
|
|
348
|
-
|
|
349
|
-
fireEvent.keyUp(screen.getByRole('dialog'), { key: 'Escape' });
|
|
350
|
-
expect(toggle).toHaveBeenCalled();
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
it('should not call toggle when escape key pressed when keyboard is false', () => {
|
|
354
|
-
const toggle = jest.fn();
|
|
355
|
-
render(
|
|
356
|
-
<Offcanvas isOpen toggle={toggle} keyboard={false}>
|
|
357
|
-
Yo!
|
|
358
|
-
</Offcanvas>,
|
|
359
|
-
);
|
|
360
|
-
|
|
361
|
-
fireEvent.keyUp(screen.getByRole('dialog'), { key: 'Escape' });
|
|
362
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
it('should call toggle when clicking backdrop', () => {
|
|
366
|
-
const toggle = jest.fn();
|
|
367
|
-
render(
|
|
368
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
369
|
-
<button id="clicker">Does Nothing</button>
|
|
370
|
-
</Offcanvas>,
|
|
371
|
-
);
|
|
372
|
-
|
|
373
|
-
fireEvent.click(screen.getByText(/does nothing/i));
|
|
374
|
-
|
|
375
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
376
|
-
|
|
377
|
-
const backdrop = document.getElementsByClassName('offcanvas-backdrop')[0];
|
|
378
|
-
fireEvent.mouseDown(backdrop);
|
|
379
|
-
fireEvent.click(backdrop);
|
|
380
|
-
|
|
381
|
-
expect(toggle).toHaveBeenCalled();
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
it('should call toggle when clicking backdrop when fade is false', () => {
|
|
385
|
-
const toggle = jest.fn();
|
|
386
|
-
render(
|
|
387
|
-
<Offcanvas isOpen toggle={toggle} fade={false}>
|
|
388
|
-
<button id="clicker">Does Nothing</button>
|
|
389
|
-
</Offcanvas>,
|
|
390
|
-
);
|
|
391
|
-
|
|
392
|
-
fireEvent.click(screen.getByText(/does nothing/i));
|
|
393
|
-
|
|
394
|
-
expect(toggle).not.toHaveBeenCalled();
|
|
395
|
-
|
|
396
|
-
const backdrop = document.getElementsByClassName('offcanvas-backdrop')[0];
|
|
397
|
-
fireEvent.mouseDown(backdrop);
|
|
398
|
-
fireEvent.click(backdrop);
|
|
399
|
-
|
|
400
|
-
expect(toggle).toHaveBeenCalled();
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
it('should destroy this._element', () => {
|
|
404
|
-
const { rerender } = render(
|
|
405
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
406
|
-
thor and dr.johns
|
|
407
|
-
</Offcanvas>,
|
|
408
|
-
);
|
|
409
|
-
|
|
410
|
-
const element = screen.getByText(/thor and dr.johns/i);
|
|
411
|
-
expect(element).toBeInTheDocument();
|
|
412
|
-
|
|
413
|
-
rerender(
|
|
414
|
-
<Offcanvas isOpen={false} toggle={toggle}>
|
|
415
|
-
thor and dr.johns
|
|
416
|
-
</Offcanvas>,
|
|
417
|
-
);
|
|
418
|
-
act(() => jest.advanceTimersByTime(300));
|
|
419
|
-
expect(element).not.toBeInTheDocument();
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
it('should destroy this._element when unmountOnClose prop set to true', () => {
|
|
423
|
-
const { rerender } = render(
|
|
424
|
-
<Offcanvas isOpen toggle={toggle} unmountOnClose>
|
|
425
|
-
thor and dr.johns
|
|
426
|
-
</Offcanvas>,
|
|
427
|
-
);
|
|
428
|
-
|
|
429
|
-
const element = screen.getByText(/thor and dr.johns/i);
|
|
430
|
-
expect(element).toBeInTheDocument();
|
|
431
|
-
|
|
432
|
-
rerender(
|
|
433
|
-
<Offcanvas isOpen={false} toggle={toggle} unmountOnClose>
|
|
434
|
-
thor and dr.johns
|
|
435
|
-
</Offcanvas>,
|
|
436
|
-
);
|
|
437
|
-
act(() => jest.advanceTimersByTime(300));
|
|
438
|
-
expect(element).not.toBeInTheDocument();
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
it('should not destroy this._element when unmountOnClose prop set to false', () => {
|
|
442
|
-
const { rerender } = render(
|
|
443
|
-
<Offcanvas isOpen toggle={toggle} unmountOnClose={false}>
|
|
444
|
-
thor and dr.johns
|
|
445
|
-
</Offcanvas>,
|
|
446
|
-
);
|
|
447
|
-
|
|
448
|
-
const element = screen.getByText(/thor and dr.johns/i);
|
|
449
|
-
expect(element).toBeInTheDocument();
|
|
450
|
-
|
|
451
|
-
rerender(
|
|
452
|
-
<Offcanvas isOpen={false} toggle={toggle} unmountOnClose={false}>
|
|
453
|
-
thor and dr.johns
|
|
454
|
-
</Offcanvas>,
|
|
455
|
-
);
|
|
456
|
-
jest.advanceTimersByTime(300);
|
|
457
|
-
expect(element).toBeInTheDocument();
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
it('should destroy this._element on unmount', () => {
|
|
461
|
-
const { unmount } = render(
|
|
462
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
463
|
-
thor and dr.johns
|
|
464
|
-
</Offcanvas>,
|
|
465
|
-
);
|
|
466
|
-
|
|
467
|
-
const element = screen.getByText(/thor and dr.johns/i);
|
|
468
|
-
expect(element).toBeInTheDocument();
|
|
469
|
-
|
|
470
|
-
unmount();
|
|
471
|
-
act(() => jest.advanceTimersByTime(300));
|
|
472
|
-
expect(element).not.toBeInTheDocument();
|
|
473
|
-
});
|
|
474
|
-
|
|
475
|
-
it('should remove exactly visibility styles from body', () => {
|
|
476
|
-
// set a body class which includes offcanvas-open
|
|
477
|
-
document.body.style.background = 'blue';
|
|
478
|
-
|
|
479
|
-
const { rerender } = render(
|
|
480
|
-
<Offcanvas isOpen={false} toggle={toggle}>
|
|
481
|
-
Yo!
|
|
482
|
-
</Offcanvas>,
|
|
483
|
-
);
|
|
484
|
-
|
|
485
|
-
// assert that the offcanvas is closed and the body class is what was set initially
|
|
486
|
-
act(() => jest.advanceTimersByTime(300));
|
|
487
|
-
expect(document.body.style.background).toBe('blue');
|
|
488
|
-
expect(document.body.style.overflow).toBe('');
|
|
489
|
-
|
|
490
|
-
rerender(
|
|
491
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
492
|
-
Yo!
|
|
493
|
-
</Offcanvas>,
|
|
494
|
-
);
|
|
495
|
-
|
|
496
|
-
// assert that the offcanvas is open and the body class is what was set initially + offcanvas-open
|
|
497
|
-
act(() => jest.advanceTimersByTime(300));
|
|
498
|
-
expect(document.body.style.background).toBe('blue');
|
|
499
|
-
expect(document.body.style.overflow).toBe('hidden');
|
|
500
|
-
|
|
501
|
-
// append another body class which includes offcanvas-open
|
|
502
|
-
// using this to test if replace will leave a space when removing offcanvas-open
|
|
503
|
-
document.body.style.color = 'red';
|
|
504
|
-
expect(document.body.style.background).toBe('blue');
|
|
505
|
-
expect(document.body.style.color).toBe('red');
|
|
506
|
-
expect(document.body.style.overflow).toBe('hidden');
|
|
507
|
-
|
|
508
|
-
rerender(
|
|
509
|
-
<Offcanvas isOpen={false} toggle={toggle}>
|
|
510
|
-
Yo!
|
|
511
|
-
</Offcanvas>,
|
|
512
|
-
);
|
|
513
|
-
|
|
514
|
-
// assert that the offcanvas is closed and the body class is what was set initially
|
|
515
|
-
act(() => jest.advanceTimersByTime(301));
|
|
516
|
-
expect(document.body.style.background).toBe('blue');
|
|
517
|
-
expect(document.body.style.color).toBe('red');
|
|
518
|
-
expect(document.body.style.overflow).toBe('');
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
it('should call onEnter & onExit props if provided', () => {
|
|
522
|
-
const onEnter = jest.fn();
|
|
523
|
-
const onExit = jest.fn();
|
|
524
|
-
const { rerender, unmount } = render(
|
|
525
|
-
<Offcanvas
|
|
526
|
-
isOpen={false}
|
|
527
|
-
onEnter={onEnter}
|
|
528
|
-
onExit={onExit}
|
|
529
|
-
toggle={toggle}
|
|
530
|
-
>
|
|
531
|
-
Yo!
|
|
532
|
-
</Offcanvas>,
|
|
533
|
-
);
|
|
534
|
-
|
|
535
|
-
expect(onEnter).toHaveBeenCalled();
|
|
536
|
-
expect(onExit).not.toHaveBeenCalled();
|
|
537
|
-
|
|
538
|
-
onEnter.mockReset();
|
|
539
|
-
onExit.mockReset();
|
|
540
|
-
|
|
541
|
-
rerender(
|
|
542
|
-
<Offcanvas isOpen onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
543
|
-
Yo!
|
|
544
|
-
</Offcanvas>,
|
|
545
|
-
);
|
|
546
|
-
jest.advanceTimersByTime(300);
|
|
547
|
-
|
|
548
|
-
expect(onEnter).not.toHaveBeenCalled();
|
|
549
|
-
expect(onExit).not.toHaveBeenCalled();
|
|
550
|
-
|
|
551
|
-
onEnter.mockReset();
|
|
552
|
-
onExit.mockReset();
|
|
553
|
-
|
|
554
|
-
unmount();
|
|
555
|
-
expect(onEnter).not.toHaveBeenCalled();
|
|
556
|
-
expect(onExit).toHaveBeenCalled();
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
it('should update element z index when prop changes', () => {
|
|
560
|
-
const { rerender } = render(
|
|
561
|
-
<Offcanvas isOpen zIndex={0}>
|
|
562
|
-
Yo!
|
|
563
|
-
</Offcanvas>,
|
|
564
|
-
);
|
|
565
|
-
expect(screen.getByText(/yo/i).parentElement).toHaveStyle('z-index: 0');
|
|
566
|
-
rerender(
|
|
567
|
-
<Offcanvas isOpen zIndex={1}>
|
|
568
|
-
Yo!
|
|
569
|
-
</Offcanvas>,
|
|
570
|
-
);
|
|
571
|
-
expect(screen.getByText(/yo/i).parentElement).toHaveStyle('z-index: 1');
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
it('should allow focus on only focusable elements', async () => {
|
|
575
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
576
|
-
render(
|
|
577
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
578
|
-
<OffcanvasHeader toggle={toggle}>Offcanvas title</OffcanvasHeader>
|
|
579
|
-
<OffcanvasBody>
|
|
580
|
-
<a alt="test" href="/">
|
|
581
|
-
First Test
|
|
582
|
-
</a>
|
|
583
|
-
<map name="test">
|
|
584
|
-
<area alt="test" href="/" coords="200,5,200,30" />
|
|
585
|
-
</map>
|
|
586
|
-
<input type="text" aria-label="test text input" />
|
|
587
|
-
<input type="hidden" />
|
|
588
|
-
<input type="text" disabled value="Test" />
|
|
589
|
-
<select name="test" id="select_test">
|
|
590
|
-
<option>Second item</option>
|
|
591
|
-
</select>
|
|
592
|
-
<select name="test" id="select_test_disabled" disabled>
|
|
593
|
-
<option>Third item</option>
|
|
594
|
-
</select>
|
|
595
|
-
<textarea
|
|
596
|
-
name="textarea_test"
|
|
597
|
-
id="textarea_test"
|
|
598
|
-
cols="30"
|
|
599
|
-
rows="10"
|
|
600
|
-
aria-label="test text area"
|
|
601
|
-
/>
|
|
602
|
-
<textarea
|
|
603
|
-
name="textarea_test_disabled"
|
|
604
|
-
id="textarea_test_disabled"
|
|
605
|
-
cols="30"
|
|
606
|
-
rows="10"
|
|
607
|
-
disabled
|
|
608
|
-
/>
|
|
609
|
-
<object>Test</object>
|
|
610
|
-
<span tabIndex="0">test tab index</span>
|
|
611
|
-
</OffcanvasBody>
|
|
612
|
-
</Offcanvas>,
|
|
613
|
-
);
|
|
614
|
-
|
|
615
|
-
await localUser.tab();
|
|
616
|
-
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
617
|
-
await localUser.tab();
|
|
618
|
-
expect(screen.getByText(/first test/i)).toHaveFocus();
|
|
619
|
-
await localUser.tab();
|
|
620
|
-
expect(screen.getByLabelText(/test text input/i)).toHaveFocus();
|
|
621
|
-
await localUser.tab();
|
|
622
|
-
expect(screen.getByText(/second item/i).parentElement).toHaveFocus();
|
|
623
|
-
await localUser.tab();
|
|
624
|
-
expect(screen.getByLabelText(/test text area/i)).toHaveFocus();
|
|
625
|
-
await localUser.tab();
|
|
626
|
-
expect(screen.getByText(/test tab index/i)).toHaveFocus();
|
|
627
|
-
await localUser.tab();
|
|
628
|
-
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
629
|
-
});
|
|
630
|
-
|
|
631
|
-
it('should return the focus to the last focused element before the offcanvas has opened', async () => {
|
|
632
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
633
|
-
const { rerender } = render(
|
|
634
|
-
<>
|
|
635
|
-
<button className="focus">Focused</button>
|
|
636
|
-
<Offcanvas isOpen={false}>
|
|
637
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
638
|
-
</Offcanvas>
|
|
639
|
-
</>,
|
|
640
|
-
);
|
|
641
|
-
|
|
642
|
-
await localUser.tab();
|
|
643
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
644
|
-
|
|
645
|
-
rerender(
|
|
646
|
-
<>
|
|
647
|
-
<button className="focus">Focused</button>
|
|
648
|
-
<Offcanvas isOpen>
|
|
649
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
650
|
-
</Offcanvas>
|
|
651
|
-
</>,
|
|
652
|
-
);
|
|
653
|
-
|
|
654
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
655
|
-
|
|
656
|
-
rerender(
|
|
657
|
-
<>
|
|
658
|
-
<button className="focus">Focused</button>
|
|
659
|
-
<Offcanvas isOpen={false}>
|
|
660
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
661
|
-
</Offcanvas>
|
|
662
|
-
</>,
|
|
663
|
-
);
|
|
664
|
-
|
|
665
|
-
act(() => jest.runAllTimers());
|
|
666
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
it('should not return the focus to the last focused element before the offcanvas has opened when "returnFocusAfterClose" is false', async () => {
|
|
670
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
671
|
-
const { rerender } = render(
|
|
672
|
-
<>
|
|
673
|
-
<button className="focus">Focused</button>
|
|
674
|
-
<Offcanvas returnFocusAfterClose={false} isOpen={false}>
|
|
675
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
676
|
-
</Offcanvas>
|
|
677
|
-
</>,
|
|
678
|
-
);
|
|
679
|
-
|
|
680
|
-
await localUser.tab();
|
|
681
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
682
|
-
|
|
683
|
-
rerender(
|
|
684
|
-
<>
|
|
685
|
-
<button className="focus">Focused</button>
|
|
686
|
-
<Offcanvas returnFocusAfterClose={false} isOpen>
|
|
687
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
688
|
-
</Offcanvas>
|
|
689
|
-
</>,
|
|
690
|
-
);
|
|
691
|
-
|
|
692
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
693
|
-
|
|
694
|
-
rerender(
|
|
695
|
-
<>
|
|
696
|
-
<button className="focus">Focused</button>
|
|
697
|
-
<Offcanvas returnFocusAfterClose={false} isOpen={false}>
|
|
698
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
699
|
-
</Offcanvas>
|
|
700
|
-
</>,
|
|
701
|
-
);
|
|
702
|
-
|
|
703
|
-
act(() => jest.runAllTimers());
|
|
704
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
it('should return the focus to the last focused element before the offcanvas has opened when "unmountOnClose" is false', async () => {
|
|
708
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
709
|
-
const { rerender } = render(
|
|
710
|
-
<>
|
|
711
|
-
<button className="focus">Focused</button>
|
|
712
|
-
<Offcanvas unmountOnClose={false} isOpen={false}>
|
|
713
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
714
|
-
</Offcanvas>
|
|
715
|
-
</>,
|
|
716
|
-
);
|
|
717
|
-
|
|
718
|
-
await localUser.tab();
|
|
719
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
720
|
-
|
|
721
|
-
rerender(
|
|
722
|
-
<>
|
|
723
|
-
<button className="focus">Focused</button>
|
|
724
|
-
<Offcanvas unmountOnClose={false} isOpen>
|
|
725
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
726
|
-
</Offcanvas>
|
|
727
|
-
</>,
|
|
728
|
-
);
|
|
729
|
-
|
|
730
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
731
|
-
|
|
732
|
-
rerender(
|
|
733
|
-
<>
|
|
734
|
-
<button className="focus">Focused</button>
|
|
735
|
-
<Offcanvas unmountOnClose={false} isOpen={false}>
|
|
736
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
737
|
-
</Offcanvas>
|
|
738
|
-
</>,
|
|
739
|
-
);
|
|
740
|
-
|
|
741
|
-
act(() => jest.runAllTimers());
|
|
742
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
743
|
-
});
|
|
744
|
-
|
|
745
|
-
it('should not return the focus to the last focused element before the offcanvas has opened when "returnFocusAfterClose" is false and "unmountOnClose" is false', async () => {
|
|
746
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
747
|
-
const { rerender } = render(
|
|
748
|
-
<>
|
|
749
|
-
<button className="focus">Focused</button>
|
|
750
|
-
<Offcanvas
|
|
751
|
-
returnFocusAfterClose={false}
|
|
752
|
-
unmountOnClose={false}
|
|
753
|
-
isOpen={false}
|
|
754
|
-
>
|
|
755
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
756
|
-
</Offcanvas>
|
|
757
|
-
</>,
|
|
758
|
-
);
|
|
759
|
-
|
|
760
|
-
await localUser.tab();
|
|
761
|
-
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
762
|
-
|
|
763
|
-
rerender(
|
|
764
|
-
<>
|
|
765
|
-
<button className="focus">Focused</button>
|
|
766
|
-
<Offcanvas returnFocusAfterClose={false} unmountOnClose={false} isOpen>
|
|
767
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
768
|
-
</Offcanvas>
|
|
769
|
-
</>,
|
|
770
|
-
);
|
|
771
|
-
|
|
772
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
773
|
-
|
|
774
|
-
rerender(
|
|
775
|
-
<>
|
|
776
|
-
<button className="focus">Focused</button>
|
|
777
|
-
<Offcanvas
|
|
778
|
-
returnFocusAfterClose={false}
|
|
779
|
-
unmountOnClose={false}
|
|
780
|
-
isOpen={false}
|
|
781
|
-
>
|
|
782
|
-
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
783
|
-
</Offcanvas>
|
|
784
|
-
</>,
|
|
785
|
-
);
|
|
786
|
-
|
|
787
|
-
act(() => jest.runAllTimers());
|
|
788
|
-
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
789
|
-
});
|
|
790
|
-
|
|
791
|
-
it('should attach/detach trapFocus for dialogs', () => {
|
|
792
|
-
const addEventListener = jest.spyOn(document, 'addEventListener');
|
|
793
|
-
const removeEventListener = jest.spyOn(document, 'removeEventListener');
|
|
794
|
-
|
|
795
|
-
const { unmount } = render(
|
|
796
|
-
<Offcanvas isOpen toggle={toggle}>
|
|
797
|
-
Yo!
|
|
798
|
-
</Offcanvas>,
|
|
799
|
-
);
|
|
800
|
-
|
|
801
|
-
expect(addEventListener).toHaveBeenCalledTimes(1);
|
|
802
|
-
expect(addEventListener).toHaveBeenCalledWith(
|
|
803
|
-
'focus',
|
|
804
|
-
expect.any(Function),
|
|
805
|
-
true,
|
|
806
|
-
);
|
|
807
|
-
|
|
808
|
-
unmount();
|
|
809
|
-
|
|
810
|
-
expect(removeEventListener).toHaveBeenCalledTimes(1);
|
|
811
|
-
expect(removeEventListener).toHaveBeenCalledWith(
|
|
812
|
-
'focus',
|
|
813
|
-
expect.any(Function),
|
|
814
|
-
true,
|
|
815
|
-
);
|
|
816
|
-
|
|
817
|
-
addEventListener.mockRestore();
|
|
818
|
-
removeEventListener.mockRestore();
|
|
819
|
-
});
|
|
820
|
-
|
|
821
|
-
it('should trap focus inside the open dialog', async () => {
|
|
822
|
-
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
823
|
-
render(
|
|
824
|
-
<>
|
|
825
|
-
<Button className="first">Focused</Button>
|
|
826
|
-
<Offcanvas isOpen trapFocus>
|
|
827
|
-
<OffcanvasBody>
|
|
828
|
-
Something else to see
|
|
829
|
-
<Button className="focus">focusable element</Button>
|
|
830
|
-
</OffcanvasBody>
|
|
831
|
-
</Offcanvas>
|
|
832
|
-
</>,
|
|
833
|
-
);
|
|
834
|
-
await localUser.tab();
|
|
835
|
-
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
836
|
-
|
|
837
|
-
await localUser.tab();
|
|
838
|
-
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
839
|
-
});
|
|
840
|
-
});
|
|
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 { Offcanvas, OffcanvasBody, OffcanvasHeader, Button } from '..';
|
|
7
|
+
import { testForCustomClass } from '../testUtils';
|
|
8
|
+
|
|
9
|
+
describe('Offcanvas', () => {
|
|
10
|
+
let toggle;
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
toggle = () => {};
|
|
13
|
+
jest.useFakeTimers();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
jest.clearAllTimers();
|
|
18
|
+
document.body.removeAttribute('style');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should render offcanvas portal into DOM', () => {
|
|
22
|
+
render(
|
|
23
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
24
|
+
Yo!
|
|
25
|
+
</Offcanvas>,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
jest.advanceTimersByTime(300);
|
|
29
|
+
|
|
30
|
+
expect(screen.getByText(/yo/i)).toBeInTheDocument();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should render with the class "offcanvas"', () => {
|
|
34
|
+
render(
|
|
35
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
36
|
+
Yo!
|
|
37
|
+
</Offcanvas>,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(screen.getByText(/yo/i)).toHaveClass('offcanvas');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should render with the backdrop with the class "offcanvas-backdrop" by default', () => {
|
|
44
|
+
render(
|
|
45
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
46
|
+
Yo!
|
|
47
|
+
</Offcanvas>,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(document.getElementsByClassName('offcanvas-backdrop')).toHaveLength(
|
|
51
|
+
1,
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should not render with the backdrop with the class "offcanvas-backdrop" when backdrop is "false"', () => {
|
|
56
|
+
render(
|
|
57
|
+
<Offcanvas isOpen toggle={toggle} backdrop={false}>
|
|
58
|
+
Yo!
|
|
59
|
+
</Offcanvas>,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
expect(document.getElementsByClassName('offcanvas').length).toBe(1);
|
|
63
|
+
expect(document.getElementsByClassName('offcanvas-backdrop').length).toBe(
|
|
64
|
+
0,
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should have custom class name if provided', () => {
|
|
69
|
+
testForCustomClass(Offcanvas, { isOpen: true, toggle });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should render with additional props if provided', () => {
|
|
73
|
+
render(
|
|
74
|
+
<Offcanvas isOpen toggle={toggle} style={{ maxWidth: '95%' }}>
|
|
75
|
+
Yo!
|
|
76
|
+
</Offcanvas>,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(document.getElementsByClassName('offcanvas')[0].style.maxWidth).toBe(
|
|
80
|
+
'95%',
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should render without fade transition if provided with fade={false}', () => {
|
|
85
|
+
render(
|
|
86
|
+
<Offcanvas
|
|
87
|
+
isOpen
|
|
88
|
+
toggle={toggle}
|
|
89
|
+
fade={false}
|
|
90
|
+
className="fadeless-offcanvas"
|
|
91
|
+
>
|
|
92
|
+
Howdy!
|
|
93
|
+
</Offcanvas>,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(
|
|
97
|
+
document.getElementsByClassName('fadeless-offcanvas')[0],
|
|
98
|
+
).not.toHaveClass('fade');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should render when expected when passed offcanvasTransition and backdropTransition props', () => {
|
|
102
|
+
render(
|
|
103
|
+
<Offcanvas
|
|
104
|
+
isOpen
|
|
105
|
+
toggle={toggle}
|
|
106
|
+
offcanvasTransition={{ timeout: 2 }}
|
|
107
|
+
backdropTransition={{ timeout: 10 }}
|
|
108
|
+
className="custom-timeout-offcanvas"
|
|
109
|
+
>
|
|
110
|
+
Hello, world!
|
|
111
|
+
</Offcanvas>,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(
|
|
115
|
+
document.getElementsByClassName('custom-timeout-offcanvas')[0],
|
|
116
|
+
).toHaveClass('fade');
|
|
117
|
+
expect(
|
|
118
|
+
document.getElementsByClassName('custom-timeout-offcanvas')[0],
|
|
119
|
+
).not.toHaveClass('show');
|
|
120
|
+
expect(
|
|
121
|
+
document.getElementsByClassName('offcanvas-backdrop')[0],
|
|
122
|
+
).not.toHaveClass('show');
|
|
123
|
+
|
|
124
|
+
act(() => jest.advanceTimersByTime(20));
|
|
125
|
+
expect(
|
|
126
|
+
document.getElementsByClassName('custom-timeout-offcanvas')[0],
|
|
127
|
+
).toHaveClass('show');
|
|
128
|
+
expect(
|
|
129
|
+
document.getElementsByClassName('offcanvas-backdrop')[0],
|
|
130
|
+
).toHaveClass('show');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should render with class "offcanvas-backdrop" and have custom class name if provided with backdropClassName', () => {
|
|
134
|
+
render(
|
|
135
|
+
<Offcanvas isOpen toggle={toggle} backdropClassName="my-custom-offcanvas">
|
|
136
|
+
Yo!
|
|
137
|
+
</Offcanvas>,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
expect(
|
|
141
|
+
document.getElementsByClassName(
|
|
142
|
+
'offcanvas-backdrop my-custom-offcanvas',
|
|
143
|
+
)[0],
|
|
144
|
+
).toBeInTheDocument();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should render with the class "offcanvas-${direction}" when direction is passed', () => {
|
|
148
|
+
render(
|
|
149
|
+
<Offcanvas isOpen toggle={toggle} direction="top">
|
|
150
|
+
Yo!
|
|
151
|
+
</Offcanvas>,
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
expect(screen.getByText(/yo/i)).toHaveClass('offcanvas-top');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should render offcanvas when isOpen is true', () => {
|
|
158
|
+
render(
|
|
159
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
160
|
+
Yo!
|
|
161
|
+
</Offcanvas>,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
expect(screen.getByText(/yo/i)).toHaveClass('offcanvas');
|
|
165
|
+
expect(document.getElementsByClassName('offcanvas-backdrop').length).toBe(
|
|
166
|
+
1,
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should render offcanvas with default role of "dialog"', () => {
|
|
171
|
+
render(
|
|
172
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
173
|
+
Yo!
|
|
174
|
+
</Offcanvas>,
|
|
175
|
+
);
|
|
176
|
+
expect(screen.getByText(/yo/i).getAttribute('role')).toBe('dialog');
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('should render offcanvas with provided role', () => {
|
|
180
|
+
render(
|
|
181
|
+
<Offcanvas isOpen toggle={toggle} role="alert">
|
|
182
|
+
Yo!
|
|
183
|
+
</Offcanvas>,
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
expect(screen.getByText(/yo/i).getAttribute('role')).toBe('alert');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('should render offcanvas with aria-labelledby provided labelledBy', () => {
|
|
190
|
+
render(
|
|
191
|
+
<Offcanvas isOpen toggle={toggle} labelledBy="myOffcanvasTitle">
|
|
192
|
+
Yo!
|
|
193
|
+
</Offcanvas>,
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
expect(screen.getByText(/yo/i).getAttribute('aria-labelledby')).toBe(
|
|
197
|
+
'myOffcanvasTitle',
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('should not render offcanvas when isOpen is false', () => {
|
|
202
|
+
render(
|
|
203
|
+
<Offcanvas isOpen={false} toggle={toggle}>
|
|
204
|
+
Yo!
|
|
205
|
+
</Offcanvas>,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
expect(screen.queryByText(/yo/i)).not.toBeInTheDocument();
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should toggle offcanvas', () => {
|
|
212
|
+
const { rerender } = render(
|
|
213
|
+
<Offcanvas isOpen={false} toggle={toggle}>
|
|
214
|
+
Yo!
|
|
215
|
+
</Offcanvas>,
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
expect(screen.queryByText(/yo/i)).not.toBeInTheDocument();
|
|
219
|
+
|
|
220
|
+
rerender(
|
|
221
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
222
|
+
Yo!
|
|
223
|
+
</Offcanvas>,
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
expect(screen.queryByText(/yo/i)).toBeInTheDocument();
|
|
227
|
+
|
|
228
|
+
rerender(
|
|
229
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
230
|
+
Yo!
|
|
231
|
+
</Offcanvas>,
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
expect(screen.queryByText(/yo/i)).toBeInTheDocument();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should call onClosed & onOpened', () => {
|
|
238
|
+
const onOpened = jest.fn();
|
|
239
|
+
const onClosed = jest.fn();
|
|
240
|
+
const { rerender } = render(
|
|
241
|
+
<Offcanvas
|
|
242
|
+
isOpen={false}
|
|
243
|
+
onOpened={onOpened}
|
|
244
|
+
onClosed={onClosed}
|
|
245
|
+
toggle={toggle}
|
|
246
|
+
>
|
|
247
|
+
Yo!
|
|
248
|
+
</Offcanvas>,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
expect(onOpened).not.toHaveBeenCalled();
|
|
252
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
253
|
+
|
|
254
|
+
rerender(
|
|
255
|
+
<Offcanvas isOpen onOpened={onOpened} onClosed={onClosed} toggle={toggle}>
|
|
256
|
+
Yo!
|
|
257
|
+
</Offcanvas>,
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
act(() => jest.advanceTimersByTime(300));
|
|
261
|
+
|
|
262
|
+
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
263
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
264
|
+
|
|
265
|
+
onOpened.mockClear();
|
|
266
|
+
onClosed.mockClear();
|
|
267
|
+
|
|
268
|
+
rerender(
|
|
269
|
+
<Offcanvas
|
|
270
|
+
isOpen={false}
|
|
271
|
+
onOpened={onOpened}
|
|
272
|
+
onClosed={onClosed}
|
|
273
|
+
toggle={toggle}
|
|
274
|
+
>
|
|
275
|
+
Yo!
|
|
276
|
+
</Offcanvas>,
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
act(() => jest.advanceTimersByTime(300));
|
|
280
|
+
|
|
281
|
+
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
282
|
+
expect(onOpened).not.toHaveBeenCalled();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('should call onClosed & onOpened when fade={false}', () => {
|
|
286
|
+
const onOpened = jest.fn();
|
|
287
|
+
const onClosed = jest.fn();
|
|
288
|
+
const { rerender } = render(
|
|
289
|
+
<Offcanvas
|
|
290
|
+
isOpen={false}
|
|
291
|
+
onOpened={onOpened}
|
|
292
|
+
onClosed={onClosed}
|
|
293
|
+
toggle={toggle}
|
|
294
|
+
fade={false}
|
|
295
|
+
>
|
|
296
|
+
Yo!
|
|
297
|
+
</Offcanvas>,
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
expect(onOpened).not.toHaveBeenCalled();
|
|
301
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
302
|
+
|
|
303
|
+
rerender(
|
|
304
|
+
<Offcanvas
|
|
305
|
+
isOpen
|
|
306
|
+
onOpened={onOpened}
|
|
307
|
+
onClosed={onClosed}
|
|
308
|
+
toggle={toggle}
|
|
309
|
+
fade={false}
|
|
310
|
+
>
|
|
311
|
+
Yo!
|
|
312
|
+
</Offcanvas>,
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
act(() => jest.advanceTimersByTime(300));
|
|
316
|
+
|
|
317
|
+
expect(onOpened).toHaveBeenCalledTimes(1);
|
|
318
|
+
expect(onClosed).not.toHaveBeenCalled();
|
|
319
|
+
|
|
320
|
+
onOpened.mockClear();
|
|
321
|
+
onClosed.mockClear();
|
|
322
|
+
|
|
323
|
+
rerender(
|
|
324
|
+
<Offcanvas
|
|
325
|
+
isOpen={false}
|
|
326
|
+
onOpened={onOpened}
|
|
327
|
+
onClosed={onClosed}
|
|
328
|
+
toggle={toggle}
|
|
329
|
+
fade={false}
|
|
330
|
+
>
|
|
331
|
+
Yo!
|
|
332
|
+
</Offcanvas>,
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
act(() => jest.advanceTimersByTime(300));
|
|
336
|
+
|
|
337
|
+
expect(onClosed).toHaveBeenCalledTimes(1);
|
|
338
|
+
expect(onOpened).not.toHaveBeenCalled();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should call toggle when escape key pressed', () => {
|
|
342
|
+
const toggle = jest.fn();
|
|
343
|
+
render(
|
|
344
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
345
|
+
Yo!
|
|
346
|
+
</Offcanvas>,
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
fireEvent.keyUp(screen.getByRole('dialog'), { key: 'Escape' });
|
|
350
|
+
expect(toggle).toHaveBeenCalled();
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('should not call toggle when escape key pressed when keyboard is false', () => {
|
|
354
|
+
const toggle = jest.fn();
|
|
355
|
+
render(
|
|
356
|
+
<Offcanvas isOpen toggle={toggle} keyboard={false}>
|
|
357
|
+
Yo!
|
|
358
|
+
</Offcanvas>,
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
fireEvent.keyUp(screen.getByRole('dialog'), { key: 'Escape' });
|
|
362
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('should call toggle when clicking backdrop', () => {
|
|
366
|
+
const toggle = jest.fn();
|
|
367
|
+
render(
|
|
368
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
369
|
+
<button id="clicker">Does Nothing</button>
|
|
370
|
+
</Offcanvas>,
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
fireEvent.click(screen.getByText(/does nothing/i));
|
|
374
|
+
|
|
375
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
376
|
+
|
|
377
|
+
const backdrop = document.getElementsByClassName('offcanvas-backdrop')[0];
|
|
378
|
+
fireEvent.mouseDown(backdrop);
|
|
379
|
+
fireEvent.click(backdrop);
|
|
380
|
+
|
|
381
|
+
expect(toggle).toHaveBeenCalled();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('should call toggle when clicking backdrop when fade is false', () => {
|
|
385
|
+
const toggle = jest.fn();
|
|
386
|
+
render(
|
|
387
|
+
<Offcanvas isOpen toggle={toggle} fade={false}>
|
|
388
|
+
<button id="clicker">Does Nothing</button>
|
|
389
|
+
</Offcanvas>,
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
fireEvent.click(screen.getByText(/does nothing/i));
|
|
393
|
+
|
|
394
|
+
expect(toggle).not.toHaveBeenCalled();
|
|
395
|
+
|
|
396
|
+
const backdrop = document.getElementsByClassName('offcanvas-backdrop')[0];
|
|
397
|
+
fireEvent.mouseDown(backdrop);
|
|
398
|
+
fireEvent.click(backdrop);
|
|
399
|
+
|
|
400
|
+
expect(toggle).toHaveBeenCalled();
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it('should destroy this._element', () => {
|
|
404
|
+
const { rerender } = render(
|
|
405
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
406
|
+
thor and dr.johns
|
|
407
|
+
</Offcanvas>,
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
const element = screen.getByText(/thor and dr.johns/i);
|
|
411
|
+
expect(element).toBeInTheDocument();
|
|
412
|
+
|
|
413
|
+
rerender(
|
|
414
|
+
<Offcanvas isOpen={false} toggle={toggle}>
|
|
415
|
+
thor and dr.johns
|
|
416
|
+
</Offcanvas>,
|
|
417
|
+
);
|
|
418
|
+
act(() => jest.advanceTimersByTime(300));
|
|
419
|
+
expect(element).not.toBeInTheDocument();
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('should destroy this._element when unmountOnClose prop set to true', () => {
|
|
423
|
+
const { rerender } = render(
|
|
424
|
+
<Offcanvas isOpen toggle={toggle} unmountOnClose>
|
|
425
|
+
thor and dr.johns
|
|
426
|
+
</Offcanvas>,
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const element = screen.getByText(/thor and dr.johns/i);
|
|
430
|
+
expect(element).toBeInTheDocument();
|
|
431
|
+
|
|
432
|
+
rerender(
|
|
433
|
+
<Offcanvas isOpen={false} toggle={toggle} unmountOnClose>
|
|
434
|
+
thor and dr.johns
|
|
435
|
+
</Offcanvas>,
|
|
436
|
+
);
|
|
437
|
+
act(() => jest.advanceTimersByTime(300));
|
|
438
|
+
expect(element).not.toBeInTheDocument();
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('should not destroy this._element when unmountOnClose prop set to false', () => {
|
|
442
|
+
const { rerender } = render(
|
|
443
|
+
<Offcanvas isOpen toggle={toggle} unmountOnClose={false}>
|
|
444
|
+
thor and dr.johns
|
|
445
|
+
</Offcanvas>,
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
const element = screen.getByText(/thor and dr.johns/i);
|
|
449
|
+
expect(element).toBeInTheDocument();
|
|
450
|
+
|
|
451
|
+
rerender(
|
|
452
|
+
<Offcanvas isOpen={false} toggle={toggle} unmountOnClose={false}>
|
|
453
|
+
thor and dr.johns
|
|
454
|
+
</Offcanvas>,
|
|
455
|
+
);
|
|
456
|
+
jest.advanceTimersByTime(300);
|
|
457
|
+
expect(element).toBeInTheDocument();
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it('should destroy this._element on unmount', () => {
|
|
461
|
+
const { unmount } = render(
|
|
462
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
463
|
+
thor and dr.johns
|
|
464
|
+
</Offcanvas>,
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
const element = screen.getByText(/thor and dr.johns/i);
|
|
468
|
+
expect(element).toBeInTheDocument();
|
|
469
|
+
|
|
470
|
+
unmount();
|
|
471
|
+
act(() => jest.advanceTimersByTime(300));
|
|
472
|
+
expect(element).not.toBeInTheDocument();
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it('should remove exactly visibility styles from body', () => {
|
|
476
|
+
// set a body class which includes offcanvas-open
|
|
477
|
+
document.body.style.background = 'blue';
|
|
478
|
+
|
|
479
|
+
const { rerender } = render(
|
|
480
|
+
<Offcanvas isOpen={false} toggle={toggle}>
|
|
481
|
+
Yo!
|
|
482
|
+
</Offcanvas>,
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
// assert that the offcanvas is closed and the body class is what was set initially
|
|
486
|
+
act(() => jest.advanceTimersByTime(300));
|
|
487
|
+
expect(document.body.style.background).toBe('blue');
|
|
488
|
+
expect(document.body.style.overflow).toBe('');
|
|
489
|
+
|
|
490
|
+
rerender(
|
|
491
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
492
|
+
Yo!
|
|
493
|
+
</Offcanvas>,
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
// assert that the offcanvas is open and the body class is what was set initially + offcanvas-open
|
|
497
|
+
act(() => jest.advanceTimersByTime(300));
|
|
498
|
+
expect(document.body.style.background).toBe('blue');
|
|
499
|
+
expect(document.body.style.overflow).toBe('hidden');
|
|
500
|
+
|
|
501
|
+
// append another body class which includes offcanvas-open
|
|
502
|
+
// using this to test if replace will leave a space when removing offcanvas-open
|
|
503
|
+
document.body.style.color = 'red';
|
|
504
|
+
expect(document.body.style.background).toBe('blue');
|
|
505
|
+
expect(document.body.style.color).toBe('red');
|
|
506
|
+
expect(document.body.style.overflow).toBe('hidden');
|
|
507
|
+
|
|
508
|
+
rerender(
|
|
509
|
+
<Offcanvas isOpen={false} toggle={toggle}>
|
|
510
|
+
Yo!
|
|
511
|
+
</Offcanvas>,
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
// assert that the offcanvas is closed and the body class is what was set initially
|
|
515
|
+
act(() => jest.advanceTimersByTime(301));
|
|
516
|
+
expect(document.body.style.background).toBe('blue');
|
|
517
|
+
expect(document.body.style.color).toBe('red');
|
|
518
|
+
expect(document.body.style.overflow).toBe('');
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it('should call onEnter & onExit props if provided', () => {
|
|
522
|
+
const onEnter = jest.fn();
|
|
523
|
+
const onExit = jest.fn();
|
|
524
|
+
const { rerender, unmount } = render(
|
|
525
|
+
<Offcanvas
|
|
526
|
+
isOpen={false}
|
|
527
|
+
onEnter={onEnter}
|
|
528
|
+
onExit={onExit}
|
|
529
|
+
toggle={toggle}
|
|
530
|
+
>
|
|
531
|
+
Yo!
|
|
532
|
+
</Offcanvas>,
|
|
533
|
+
);
|
|
534
|
+
|
|
535
|
+
expect(onEnter).toHaveBeenCalled();
|
|
536
|
+
expect(onExit).not.toHaveBeenCalled();
|
|
537
|
+
|
|
538
|
+
onEnter.mockReset();
|
|
539
|
+
onExit.mockReset();
|
|
540
|
+
|
|
541
|
+
rerender(
|
|
542
|
+
<Offcanvas isOpen onEnter={onEnter} onExit={onExit} toggle={toggle}>
|
|
543
|
+
Yo!
|
|
544
|
+
</Offcanvas>,
|
|
545
|
+
);
|
|
546
|
+
jest.advanceTimersByTime(300);
|
|
547
|
+
|
|
548
|
+
expect(onEnter).not.toHaveBeenCalled();
|
|
549
|
+
expect(onExit).not.toHaveBeenCalled();
|
|
550
|
+
|
|
551
|
+
onEnter.mockReset();
|
|
552
|
+
onExit.mockReset();
|
|
553
|
+
|
|
554
|
+
unmount();
|
|
555
|
+
expect(onEnter).not.toHaveBeenCalled();
|
|
556
|
+
expect(onExit).toHaveBeenCalled();
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it('should update element z index when prop changes', () => {
|
|
560
|
+
const { rerender } = render(
|
|
561
|
+
<Offcanvas isOpen zIndex={0}>
|
|
562
|
+
Yo!
|
|
563
|
+
</Offcanvas>,
|
|
564
|
+
);
|
|
565
|
+
expect(screen.getByText(/yo/i).parentElement).toHaveStyle('z-index: 0');
|
|
566
|
+
rerender(
|
|
567
|
+
<Offcanvas isOpen zIndex={1}>
|
|
568
|
+
Yo!
|
|
569
|
+
</Offcanvas>,
|
|
570
|
+
);
|
|
571
|
+
expect(screen.getByText(/yo/i).parentElement).toHaveStyle('z-index: 1');
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it('should allow focus on only focusable elements', async () => {
|
|
575
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
576
|
+
render(
|
|
577
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
578
|
+
<OffcanvasHeader toggle={toggle}>Offcanvas title</OffcanvasHeader>
|
|
579
|
+
<OffcanvasBody>
|
|
580
|
+
<a alt="test" href="/">
|
|
581
|
+
First Test
|
|
582
|
+
</a>
|
|
583
|
+
<map name="test">
|
|
584
|
+
<area alt="test" href="/" coords="200,5,200,30" />
|
|
585
|
+
</map>
|
|
586
|
+
<input type="text" aria-label="test text input" />
|
|
587
|
+
<input type="hidden" />
|
|
588
|
+
<input type="text" disabled value="Test" />
|
|
589
|
+
<select name="test" id="select_test">
|
|
590
|
+
<option>Second item</option>
|
|
591
|
+
</select>
|
|
592
|
+
<select name="test" id="select_test_disabled" disabled>
|
|
593
|
+
<option>Third item</option>
|
|
594
|
+
</select>
|
|
595
|
+
<textarea
|
|
596
|
+
name="textarea_test"
|
|
597
|
+
id="textarea_test"
|
|
598
|
+
cols="30"
|
|
599
|
+
rows="10"
|
|
600
|
+
aria-label="test text area"
|
|
601
|
+
/>
|
|
602
|
+
<textarea
|
|
603
|
+
name="textarea_test_disabled"
|
|
604
|
+
id="textarea_test_disabled"
|
|
605
|
+
cols="30"
|
|
606
|
+
rows="10"
|
|
607
|
+
disabled
|
|
608
|
+
/>
|
|
609
|
+
<object>Test</object>
|
|
610
|
+
<span tabIndex="0">test tab index</span>
|
|
611
|
+
</OffcanvasBody>
|
|
612
|
+
</Offcanvas>,
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
await localUser.tab();
|
|
616
|
+
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
617
|
+
await localUser.tab();
|
|
618
|
+
expect(screen.getByText(/first test/i)).toHaveFocus();
|
|
619
|
+
await localUser.tab();
|
|
620
|
+
expect(screen.getByLabelText(/test text input/i)).toHaveFocus();
|
|
621
|
+
await localUser.tab();
|
|
622
|
+
expect(screen.getByText(/second item/i).parentElement).toHaveFocus();
|
|
623
|
+
await localUser.tab();
|
|
624
|
+
expect(screen.getByLabelText(/test text area/i)).toHaveFocus();
|
|
625
|
+
await localUser.tab();
|
|
626
|
+
expect(screen.getByText(/test tab index/i)).toHaveFocus();
|
|
627
|
+
await localUser.tab();
|
|
628
|
+
expect(screen.getByLabelText(/close/i)).toHaveFocus();
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it('should return the focus to the last focused element before the offcanvas has opened', async () => {
|
|
632
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
633
|
+
const { rerender } = render(
|
|
634
|
+
<>
|
|
635
|
+
<button className="focus">Focused</button>
|
|
636
|
+
<Offcanvas isOpen={false}>
|
|
637
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
638
|
+
</Offcanvas>
|
|
639
|
+
</>,
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
await localUser.tab();
|
|
643
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
644
|
+
|
|
645
|
+
rerender(
|
|
646
|
+
<>
|
|
647
|
+
<button className="focus">Focused</button>
|
|
648
|
+
<Offcanvas isOpen>
|
|
649
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
650
|
+
</Offcanvas>
|
|
651
|
+
</>,
|
|
652
|
+
);
|
|
653
|
+
|
|
654
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
655
|
+
|
|
656
|
+
rerender(
|
|
657
|
+
<>
|
|
658
|
+
<button className="focus">Focused</button>
|
|
659
|
+
<Offcanvas isOpen={false}>
|
|
660
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
661
|
+
</Offcanvas>
|
|
662
|
+
</>,
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
act(() => jest.runAllTimers());
|
|
666
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it('should not return the focus to the last focused element before the offcanvas has opened when "returnFocusAfterClose" is false', async () => {
|
|
670
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
671
|
+
const { rerender } = render(
|
|
672
|
+
<>
|
|
673
|
+
<button className="focus">Focused</button>
|
|
674
|
+
<Offcanvas returnFocusAfterClose={false} isOpen={false}>
|
|
675
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
676
|
+
</Offcanvas>
|
|
677
|
+
</>,
|
|
678
|
+
);
|
|
679
|
+
|
|
680
|
+
await localUser.tab();
|
|
681
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
682
|
+
|
|
683
|
+
rerender(
|
|
684
|
+
<>
|
|
685
|
+
<button className="focus">Focused</button>
|
|
686
|
+
<Offcanvas returnFocusAfterClose={false} isOpen>
|
|
687
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
688
|
+
</Offcanvas>
|
|
689
|
+
</>,
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
693
|
+
|
|
694
|
+
rerender(
|
|
695
|
+
<>
|
|
696
|
+
<button className="focus">Focused</button>
|
|
697
|
+
<Offcanvas returnFocusAfterClose={false} isOpen={false}>
|
|
698
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
699
|
+
</Offcanvas>
|
|
700
|
+
</>,
|
|
701
|
+
);
|
|
702
|
+
|
|
703
|
+
act(() => jest.runAllTimers());
|
|
704
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
it('should return the focus to the last focused element before the offcanvas has opened when "unmountOnClose" is false', async () => {
|
|
708
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
709
|
+
const { rerender } = render(
|
|
710
|
+
<>
|
|
711
|
+
<button className="focus">Focused</button>
|
|
712
|
+
<Offcanvas unmountOnClose={false} isOpen={false}>
|
|
713
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
714
|
+
</Offcanvas>
|
|
715
|
+
</>,
|
|
716
|
+
);
|
|
717
|
+
|
|
718
|
+
await localUser.tab();
|
|
719
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
720
|
+
|
|
721
|
+
rerender(
|
|
722
|
+
<>
|
|
723
|
+
<button className="focus">Focused</button>
|
|
724
|
+
<Offcanvas unmountOnClose={false} isOpen>
|
|
725
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
726
|
+
</Offcanvas>
|
|
727
|
+
</>,
|
|
728
|
+
);
|
|
729
|
+
|
|
730
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
731
|
+
|
|
732
|
+
rerender(
|
|
733
|
+
<>
|
|
734
|
+
<button className="focus">Focused</button>
|
|
735
|
+
<Offcanvas unmountOnClose={false} isOpen={false}>
|
|
736
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
737
|
+
</Offcanvas>
|
|
738
|
+
</>,
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
act(() => jest.runAllTimers());
|
|
742
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
it('should not return the focus to the last focused element before the offcanvas has opened when "returnFocusAfterClose" is false and "unmountOnClose" is false', async () => {
|
|
746
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
747
|
+
const { rerender } = render(
|
|
748
|
+
<>
|
|
749
|
+
<button className="focus">Focused</button>
|
|
750
|
+
<Offcanvas
|
|
751
|
+
returnFocusAfterClose={false}
|
|
752
|
+
unmountOnClose={false}
|
|
753
|
+
isOpen={false}
|
|
754
|
+
>
|
|
755
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
756
|
+
</Offcanvas>
|
|
757
|
+
</>,
|
|
758
|
+
);
|
|
759
|
+
|
|
760
|
+
await localUser.tab();
|
|
761
|
+
expect(screen.getByText(/focused/i)).toHaveFocus();
|
|
762
|
+
|
|
763
|
+
rerender(
|
|
764
|
+
<>
|
|
765
|
+
<button className="focus">Focused</button>
|
|
766
|
+
<Offcanvas returnFocusAfterClose={false} unmountOnClose={false} isOpen>
|
|
767
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
768
|
+
</Offcanvas>
|
|
769
|
+
</>,
|
|
770
|
+
);
|
|
771
|
+
|
|
772
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
773
|
+
|
|
774
|
+
rerender(
|
|
775
|
+
<>
|
|
776
|
+
<button className="focus">Focused</button>
|
|
777
|
+
<Offcanvas
|
|
778
|
+
returnFocusAfterClose={false}
|
|
779
|
+
unmountOnClose={false}
|
|
780
|
+
isOpen={false}
|
|
781
|
+
>
|
|
782
|
+
<OffcanvasBody>Whatever</OffcanvasBody>
|
|
783
|
+
</Offcanvas>
|
|
784
|
+
</>,
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
act(() => jest.runAllTimers());
|
|
788
|
+
expect(screen.getByText(/focused/i)).not.toHaveFocus();
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
it('should attach/detach trapFocus for dialogs', () => {
|
|
792
|
+
const addEventListener = jest.spyOn(document, 'addEventListener');
|
|
793
|
+
const removeEventListener = jest.spyOn(document, 'removeEventListener');
|
|
794
|
+
|
|
795
|
+
const { unmount } = render(
|
|
796
|
+
<Offcanvas isOpen toggle={toggle}>
|
|
797
|
+
Yo!
|
|
798
|
+
</Offcanvas>,
|
|
799
|
+
);
|
|
800
|
+
|
|
801
|
+
expect(addEventListener).toHaveBeenCalledTimes(1);
|
|
802
|
+
expect(addEventListener).toHaveBeenCalledWith(
|
|
803
|
+
'focus',
|
|
804
|
+
expect.any(Function),
|
|
805
|
+
true,
|
|
806
|
+
);
|
|
807
|
+
|
|
808
|
+
unmount();
|
|
809
|
+
|
|
810
|
+
expect(removeEventListener).toHaveBeenCalledTimes(1);
|
|
811
|
+
expect(removeEventListener).toHaveBeenCalledWith(
|
|
812
|
+
'focus',
|
|
813
|
+
expect.any(Function),
|
|
814
|
+
true,
|
|
815
|
+
);
|
|
816
|
+
|
|
817
|
+
addEventListener.mockRestore();
|
|
818
|
+
removeEventListener.mockRestore();
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
it('should trap focus inside the open dialog', async () => {
|
|
822
|
+
const localUser = user.setup({ delay: null, advanceTimers: jest.advanceTimersByTime.bind(jest) });
|
|
823
|
+
render(
|
|
824
|
+
<>
|
|
825
|
+
<Button className="first">Focused</Button>
|
|
826
|
+
<Offcanvas isOpen trapFocus>
|
|
827
|
+
<OffcanvasBody>
|
|
828
|
+
Something else to see
|
|
829
|
+
<Button className="focus">focusable element</Button>
|
|
830
|
+
</OffcanvasBody>
|
|
831
|
+
</Offcanvas>
|
|
832
|
+
</>,
|
|
833
|
+
);
|
|
834
|
+
await localUser.tab();
|
|
835
|
+
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
836
|
+
|
|
837
|
+
await localUser.tab();
|
|
838
|
+
expect(screen.getByText(/focusable element/i)).toHaveFocus();
|
|
839
|
+
});
|
|
840
|
+
});
|