@instructure/ui-toggle-details 10.19.2-snapshot-2 → 10.19.2-snapshot-4
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 +1 -1
- package/package.json +19 -19
- package/tsconfig.build.tsbuildinfo +1 -1
- package/es/ToggleDetails/__new-tests__/ToggleDetails.test.js +0 -175
- package/es/ToggleGroup/__new-tests__/ToggleGroup.test.js +0 -217
- package/lib/ToggleDetails/__new-tests__/ToggleDetails.test.js +0 -177
- package/lib/ToggleGroup/__new-tests__/ToggleGroup.test.js +0 -219
- package/src/ToggleDetails/__new-tests__/ToggleDetails.test.tsx +0 -209
- package/src/ToggleGroup/__new-tests__/ToggleGroup.test.tsx +0 -269
- package/types/ToggleDetails/__new-tests__/ToggleDetails.test.d.ts +0 -2
- package/types/ToggleDetails/__new-tests__/ToggleDetails.test.d.ts.map +0 -1
- package/types/ToggleGroup/__new-tests__/ToggleGroup.test.d.ts +0 -2
- package/types/ToggleGroup/__new-tests__/ToggleGroup.test.d.ts.map +0 -1
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
var _react = require("@testing-library/react");
|
|
5
|
-
var _runAxeCheck = require("@instructure/ui-axe-check/lib/runAxeCheck.js");
|
|
6
|
-
var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
|
|
7
|
-
var _vitest = require("vitest");
|
|
8
|
-
require("@testing-library/jest-dom");
|
|
9
|
-
var _index = require("../index");
|
|
10
|
-
var _jsxRuntime = require("@emotion/react/jsx-runtime");
|
|
11
|
-
var _ToggleGroup, _ToggleGroup2, _ToggleGroup3, _ToggleGroup4, _ToggleGroup5, _svg, _svg2, _ToggleGroup6;
|
|
12
|
-
/*
|
|
13
|
-
* The MIT License (MIT)
|
|
14
|
-
*
|
|
15
|
-
* Copyright (c) 2015 - present Instructure, Inc.
|
|
16
|
-
*
|
|
17
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
18
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
19
|
-
* in the Software without restriction, including without limitation the rights
|
|
20
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
21
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
22
|
-
* furnished to do so, subject to the following conditions:
|
|
23
|
-
*
|
|
24
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
25
|
-
* copies or substantial portions of the Software.
|
|
26
|
-
*
|
|
27
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
28
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
29
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
30
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
31
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
32
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
33
|
-
* SOFTWARE.
|
|
34
|
-
*/
|
|
35
|
-
describe('<ToggleGroup />', () => {
|
|
36
|
-
let consoleWarningMock;
|
|
37
|
-
let consoleErrorMock;
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
// Mocking console to prevent test output pollution
|
|
40
|
-
consoleWarningMock = _vitest.vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
41
|
-
consoleErrorMock = _vitest.vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
42
|
-
});
|
|
43
|
-
afterEach(() => {
|
|
44
|
-
consoleWarningMock.mockRestore();
|
|
45
|
-
consoleErrorMock.mockRestore();
|
|
46
|
-
});
|
|
47
|
-
it('should show its summary and hide its children by default', async () => {
|
|
48
|
-
(0, _react.render)(_ToggleGroup || (_ToggleGroup = (0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
49
|
-
"data-testId": "toggle-group",
|
|
50
|
-
transition: false,
|
|
51
|
-
summary: "This is the summary section",
|
|
52
|
-
toggleLabel: "This is the toggleLabel",
|
|
53
|
-
children: "This is the details section"
|
|
54
|
-
})));
|
|
55
|
-
const toggleGroup = _react.screen.getByTestId('toggle-group');
|
|
56
|
-
expect(toggleGroup).toHaveTextContent('This is the summary section');
|
|
57
|
-
expect(toggleGroup).not.toHaveTextContent('This is the details section');
|
|
58
|
-
});
|
|
59
|
-
it('should render with children showing with the defaultExpanded prop', async () => {
|
|
60
|
-
const _render = (0, _react.render)(_ToggleGroup2 || (_ToggleGroup2 = (0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
61
|
-
"data-testId": "toggle-group",
|
|
62
|
-
transition: false,
|
|
63
|
-
summary: "This is the summary section",
|
|
64
|
-
toggleLabel: "This is the toggleLabel",
|
|
65
|
-
defaultExpanded: true,
|
|
66
|
-
children: "This is the details section"
|
|
67
|
-
}))),
|
|
68
|
-
container = _render.container;
|
|
69
|
-
const toggleGroup = _react.screen.getByTestId('toggle-group');
|
|
70
|
-
const toggle = container.querySelector('button');
|
|
71
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'true');
|
|
72
|
-
expect(toggleGroup).toHaveTextContent('This is the details section');
|
|
73
|
-
});
|
|
74
|
-
it('should have an aria-controls attribute', async () => {
|
|
75
|
-
const _render2 = (0, _react.render)(_ToggleGroup3 || (_ToggleGroup3 = (0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
76
|
-
transition: false,
|
|
77
|
-
summary: "This is the summary section",
|
|
78
|
-
toggleLabel: "This is the toggleLabel",
|
|
79
|
-
defaultExpanded: true,
|
|
80
|
-
children: "This is the details section"
|
|
81
|
-
}))),
|
|
82
|
-
container = _render2.container;
|
|
83
|
-
const content = _react.screen.getByText('This is the details section');
|
|
84
|
-
const toggle = container.querySelector('button');
|
|
85
|
-
expect(toggle).toHaveAttribute('aria-controls', content.id);
|
|
86
|
-
});
|
|
87
|
-
it('should have an aria-expanded attribute', async () => {
|
|
88
|
-
const _render3 = (0, _react.render)(_ToggleGroup4 || (_ToggleGroup4 = (0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
89
|
-
transition: false,
|
|
90
|
-
summary: "This is the summary section",
|
|
91
|
-
toggleLabel: "This is the toggleLabel",
|
|
92
|
-
children: "This is the details section"
|
|
93
|
-
}))),
|
|
94
|
-
container = _render3.container;
|
|
95
|
-
const toggle = container.querySelector('button');
|
|
96
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'false');
|
|
97
|
-
});
|
|
98
|
-
it('should toggle on click events', async () => {
|
|
99
|
-
const _render4 = (0, _react.render)(_ToggleGroup5 || (_ToggleGroup5 = (0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
100
|
-
transition: false,
|
|
101
|
-
summary: "This is the summary section",
|
|
102
|
-
toggleLabel: "This is the toggleLabel",
|
|
103
|
-
children: "This is the details section"
|
|
104
|
-
}))),
|
|
105
|
-
container = _render4.container;
|
|
106
|
-
const toggle = container.querySelector('button');
|
|
107
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'false');
|
|
108
|
-
await _userEvent.default.click(toggle);
|
|
109
|
-
await (0, _react.waitFor)(() => {
|
|
110
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'true');
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
it('should call onToggle on click events', async () => {
|
|
114
|
-
const onToggle = _vitest.vi.fn();
|
|
115
|
-
const _render5 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
116
|
-
transition: false,
|
|
117
|
-
summary: "This is the summary section",
|
|
118
|
-
toggleLabel: "This is the toggleLabel",
|
|
119
|
-
expanded: false,
|
|
120
|
-
onToggle: onToggle,
|
|
121
|
-
children: "This is the details section"
|
|
122
|
-
})),
|
|
123
|
-
container = _render5.container;
|
|
124
|
-
const toggle = container.querySelector('button');
|
|
125
|
-
await _userEvent.default.click(toggle);
|
|
126
|
-
await (0, _react.waitFor)(() => {
|
|
127
|
-
const args = onToggle.mock.calls[0];
|
|
128
|
-
expect(onToggle).toHaveBeenCalledTimes(1);
|
|
129
|
-
expect(args[0].type).toBe('click');
|
|
130
|
-
expect(args[1]).toBe(true);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
it('should update the toggle screenreader label based on the expanded state', async () => {
|
|
134
|
-
const _render6 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
135
|
-
transition: false,
|
|
136
|
-
summary: "This is the summary section",
|
|
137
|
-
toggleLabel: expanded => expanded ? 'Hide content' : 'Show content',
|
|
138
|
-
children: "This is the details section"
|
|
139
|
-
})),
|
|
140
|
-
container = _render6.container;
|
|
141
|
-
const toggle = container.querySelector('button');
|
|
142
|
-
const scrContent = container.querySelector('[class$="-screenReaderContent"]');
|
|
143
|
-
expect(scrContent).toHaveTextContent('Show content');
|
|
144
|
-
await _userEvent.default.click(toggle);
|
|
145
|
-
await (0, _react.waitFor)(() => {
|
|
146
|
-
expect(scrContent).toHaveTextContent('Hide content');
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
it('should accept custom icons', async () => {
|
|
150
|
-
const Icon = _svg || (_svg = (0, _jsxRuntime.jsxs)("svg", {
|
|
151
|
-
height: "50",
|
|
152
|
-
width: "50",
|
|
153
|
-
children: [(0, _jsxRuntime.jsx)("title", {
|
|
154
|
-
children: "Icon collapsed"
|
|
155
|
-
}), (0, _jsxRuntime.jsx)("circle", {
|
|
156
|
-
cx: "25",
|
|
157
|
-
cy: "25",
|
|
158
|
-
r: "20"
|
|
159
|
-
})]
|
|
160
|
-
}));
|
|
161
|
-
const IconExpanded = _svg2 || (_svg2 = (0, _jsxRuntime.jsxs)("svg", {
|
|
162
|
-
height: "50",
|
|
163
|
-
width: "50",
|
|
164
|
-
children: [(0, _jsxRuntime.jsx)("title", {
|
|
165
|
-
children: "Icon expanded"
|
|
166
|
-
}), (0, _jsxRuntime.jsx)("circle", {
|
|
167
|
-
cx: "25",
|
|
168
|
-
cy: "25",
|
|
169
|
-
r: "20"
|
|
170
|
-
})]
|
|
171
|
-
}));
|
|
172
|
-
const _render7 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
173
|
-
transition: false,
|
|
174
|
-
summary: "This is the summary section",
|
|
175
|
-
toggleLabel: "This is the toggleLabel",
|
|
176
|
-
icon: () => Icon,
|
|
177
|
-
iconExpanded: () => IconExpanded,
|
|
178
|
-
children: "This is the details section"
|
|
179
|
-
})),
|
|
180
|
-
container = _render7.container;
|
|
181
|
-
const toggle = container.querySelector('button');
|
|
182
|
-
const svg = container.querySelector('svg');
|
|
183
|
-
expect(svg).toHaveTextContent('Icon collapsed');
|
|
184
|
-
await _userEvent.default.click(toggle);
|
|
185
|
-
await (0, _react.waitFor)(() => {
|
|
186
|
-
expect(svg).toHaveTextContent('Icon expanded');
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
it('should meet a11y standards', async () => {
|
|
190
|
-
const _render8 = (0, _react.render)(_ToggleGroup6 || (_ToggleGroup6 = (0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
191
|
-
transition: false,
|
|
192
|
-
summary: "This is the summary section",
|
|
193
|
-
toggleLabel: "This is the toggleLabel",
|
|
194
|
-
children: "This is the details section"
|
|
195
|
-
}))),
|
|
196
|
-
container = _render8.container;
|
|
197
|
-
const axeCheck = await (0, _runAxeCheck.runAxeCheck)(container);
|
|
198
|
-
expect(axeCheck).toBe(true);
|
|
199
|
-
});
|
|
200
|
-
it('focuses with the focus helper', async () => {
|
|
201
|
-
var _toggleRef;
|
|
202
|
-
let toggleRef;
|
|
203
|
-
const _render9 = (0, _react.render)((0, _jsxRuntime.jsx)(_index.ToggleGroup, {
|
|
204
|
-
"data-testId": "toggle-group",
|
|
205
|
-
transition: false,
|
|
206
|
-
summary: "This is the summary section",
|
|
207
|
-
toggleLabel: "This is the toggleLabel",
|
|
208
|
-
ref: el => {
|
|
209
|
-
toggleRef = el;
|
|
210
|
-
},
|
|
211
|
-
children: "This is the details section"
|
|
212
|
-
})),
|
|
213
|
-
container = _render9.container;
|
|
214
|
-
const toggle = container.querySelector('button');
|
|
215
|
-
expect(document.activeElement).not.toBe(toggle);
|
|
216
|
-
(_toggleRef = toggleRef) === null || _toggleRef === void 0 ? void 0 : _toggleRef.focus();
|
|
217
|
-
expect(document.activeElement).toBe(toggle);
|
|
218
|
-
});
|
|
219
|
-
});
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The MIT License (MIT)
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { render, screen, waitFor } from '@testing-library/react'
|
|
26
|
-
import { runAxeCheck } from '@instructure/ui-axe-check'
|
|
27
|
-
import userEvent from '@testing-library/user-event'
|
|
28
|
-
import { vi } from 'vitest'
|
|
29
|
-
import type { MockInstance } from 'vitest'
|
|
30
|
-
|
|
31
|
-
import '@testing-library/jest-dom'
|
|
32
|
-
import { ToggleDetails } from '../index'
|
|
33
|
-
|
|
34
|
-
describe('<ToggleDetails />', () => {
|
|
35
|
-
let consoleWarningMock: ReturnType<typeof vi.spyOn>
|
|
36
|
-
let consoleErrorMock: ReturnType<typeof vi.spyOn>
|
|
37
|
-
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
// Mocking console to prevent test output pollution
|
|
40
|
-
consoleWarningMock = vi
|
|
41
|
-
.spyOn(console, 'warn')
|
|
42
|
-
.mockImplementation(() => {}) as MockInstance
|
|
43
|
-
consoleErrorMock = vi
|
|
44
|
-
.spyOn(console, 'error')
|
|
45
|
-
.mockImplementation(() => {}) as MockInstance
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
afterEach(() => {
|
|
49
|
-
consoleWarningMock.mockRestore()
|
|
50
|
-
consoleErrorMock.mockRestore()
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('should hide its content', async () => {
|
|
54
|
-
render(<ToggleDetails summary="Click me">Content</ToggleDetails>)
|
|
55
|
-
const content = screen.queryByText('Content')
|
|
56
|
-
|
|
57
|
-
expect(content).not.toBeInTheDocument()
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
it('should place the icon after the summary when prop is set', async () => {
|
|
61
|
-
const { container } = render(
|
|
62
|
-
<ToggleDetails iconPosition="end" summary="Click me">
|
|
63
|
-
Content
|
|
64
|
-
</ToggleDetails>
|
|
65
|
-
)
|
|
66
|
-
const summary = container.querySelector(
|
|
67
|
-
'[class$="-toggleDetails__summaryText"]'
|
|
68
|
-
)
|
|
69
|
-
const icon = container.querySelector('[class$="-toggleDetails__icon"]')
|
|
70
|
-
|
|
71
|
-
expect(icon?.previousSibling).toBe(summary)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
it('should have an aria-controls attribute', async () => {
|
|
75
|
-
const { container } = render(
|
|
76
|
-
<ToggleDetails summary="Click me">Details</ToggleDetails>
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
|
|
80
|
-
const content = container.querySelector(
|
|
81
|
-
'[class$="-toggleDetails__details"]'
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
expect(toggle).toHaveAttribute('aria-controls', content?.id)
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it('should have an aria-expanded attribute', async () => {
|
|
88
|
-
const { container } = render(
|
|
89
|
-
<ToggleDetails summary="Click me">Details</ToggleDetails>
|
|
90
|
-
)
|
|
91
|
-
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
|
|
92
|
-
|
|
93
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'false')
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it('should not have aria attributes when it has no children', async () => {
|
|
97
|
-
render(
|
|
98
|
-
<ToggleDetails summary="Click me" data-testid="td__0"></ToggleDetails>
|
|
99
|
-
)
|
|
100
|
-
const toggle = screen.getByTestId('td__0')
|
|
101
|
-
|
|
102
|
-
expect(toggle).not.toHaveAttribute('aria-controls')
|
|
103
|
-
expect(toggle).not.toHaveAttribute('aria-expanded')
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
it('should toggle on click events', async () => {
|
|
107
|
-
const { container } = render(
|
|
108
|
-
<ToggleDetails summary="Click me">Details</ToggleDetails>
|
|
109
|
-
)
|
|
110
|
-
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
|
|
111
|
-
|
|
112
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'false')
|
|
113
|
-
|
|
114
|
-
await userEvent.click(screen.getByText('Click me'))
|
|
115
|
-
|
|
116
|
-
await waitFor(() => {
|
|
117
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'true')
|
|
118
|
-
})
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
it('should call onToggle on click events', async () => {
|
|
122
|
-
const onToggle = vi.fn()
|
|
123
|
-
|
|
124
|
-
render(
|
|
125
|
-
<ToggleDetails summary="Click me" expanded={false} onToggle={onToggle}>
|
|
126
|
-
Details
|
|
127
|
-
</ToggleDetails>
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
await userEvent.click(screen.getByText('Click me'))
|
|
131
|
-
|
|
132
|
-
await waitFor(() => {
|
|
133
|
-
const args = onToggle.mock.calls[0]
|
|
134
|
-
|
|
135
|
-
expect(onToggle).toHaveBeenCalledTimes(1)
|
|
136
|
-
expect(args[0].type).toBe('click')
|
|
137
|
-
expect(args[1]).toBe(true)
|
|
138
|
-
})
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
it('should call onToggle on click events when it has no children', async () => {
|
|
142
|
-
const onToggle = vi.fn()
|
|
143
|
-
|
|
144
|
-
render(
|
|
145
|
-
<ToggleDetails
|
|
146
|
-
summary="Click me"
|
|
147
|
-
expanded={false}
|
|
148
|
-
onToggle={onToggle}
|
|
149
|
-
data-testid="td__1"
|
|
150
|
-
></ToggleDetails>
|
|
151
|
-
)
|
|
152
|
-
const toggle = screen.getByTestId('td__1')
|
|
153
|
-
await userEvent.click(toggle)
|
|
154
|
-
|
|
155
|
-
await waitFor(() => {
|
|
156
|
-
const args = onToggle.mock.calls[0]
|
|
157
|
-
|
|
158
|
-
expect(onToggle).toHaveBeenCalledTimes(1)
|
|
159
|
-
expect(args[0].type).toBe('click')
|
|
160
|
-
expect(args[1]).toBe(true)
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
it('should be initialized by defaultExpanded prop', async () => {
|
|
165
|
-
const { container } = render(
|
|
166
|
-
<ToggleDetails summary="Click me" defaultExpanded>
|
|
167
|
-
Content
|
|
168
|
-
</ToggleDetails>
|
|
169
|
-
)
|
|
170
|
-
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
|
|
171
|
-
const content = container.querySelector(
|
|
172
|
-
'[class$="-toggleDetails__details"]'
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'true')
|
|
176
|
-
expect(content).toHaveTextContent('Content')
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
it('should meet a11y standards', async () => {
|
|
180
|
-
const { container } = render(
|
|
181
|
-
<ToggleDetails summary="Click me">Content</ToggleDetails>
|
|
182
|
-
)
|
|
183
|
-
const axeCheck = await runAxeCheck(container)
|
|
184
|
-
|
|
185
|
-
expect(axeCheck).toBe(true)
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
it('focuses with the focus helper', async () => {
|
|
189
|
-
let toggleRef: any
|
|
190
|
-
|
|
191
|
-
const { container } = render(
|
|
192
|
-
<ToggleDetails
|
|
193
|
-
summary="Click me"
|
|
194
|
-
ref={(el) => {
|
|
195
|
-
toggleRef = el
|
|
196
|
-
}}
|
|
197
|
-
>
|
|
198
|
-
Content
|
|
199
|
-
</ToggleDetails>
|
|
200
|
-
)
|
|
201
|
-
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
|
|
202
|
-
|
|
203
|
-
expect(document.activeElement).not.toBe(toggle)
|
|
204
|
-
|
|
205
|
-
toggleRef?.focus()
|
|
206
|
-
|
|
207
|
-
expect(document.activeElement).toBe(toggle)
|
|
208
|
-
})
|
|
209
|
-
})
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The MIT License (MIT)
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { render, screen, waitFor } from '@testing-library/react'
|
|
26
|
-
import { runAxeCheck } from '@instructure/ui-axe-check'
|
|
27
|
-
import userEvent from '@testing-library/user-event'
|
|
28
|
-
import { vi } from 'vitest'
|
|
29
|
-
import type { MockInstance } from 'vitest'
|
|
30
|
-
|
|
31
|
-
import '@testing-library/jest-dom'
|
|
32
|
-
import { ToggleGroup } from '../index'
|
|
33
|
-
|
|
34
|
-
describe('<ToggleGroup />', () => {
|
|
35
|
-
let consoleWarningMock: ReturnType<typeof vi.spyOn>
|
|
36
|
-
let consoleErrorMock: ReturnType<typeof vi.spyOn>
|
|
37
|
-
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
// Mocking console to prevent test output pollution
|
|
40
|
-
consoleWarningMock = vi
|
|
41
|
-
.spyOn(console, 'warn')
|
|
42
|
-
.mockImplementation(() => {}) as MockInstance
|
|
43
|
-
consoleErrorMock = vi
|
|
44
|
-
.spyOn(console, 'error')
|
|
45
|
-
.mockImplementation(() => {}) as MockInstance
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
afterEach(() => {
|
|
49
|
-
consoleWarningMock.mockRestore()
|
|
50
|
-
consoleErrorMock.mockRestore()
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('should show its summary and hide its children by default', async () => {
|
|
54
|
-
render(
|
|
55
|
-
<ToggleGroup
|
|
56
|
-
data-testId="toggle-group"
|
|
57
|
-
transition={false}
|
|
58
|
-
summary="This is the summary section"
|
|
59
|
-
toggleLabel="This is the toggleLabel"
|
|
60
|
-
>
|
|
61
|
-
This is the details section
|
|
62
|
-
</ToggleGroup>
|
|
63
|
-
)
|
|
64
|
-
const toggleGroup = screen.getByTestId('toggle-group')
|
|
65
|
-
|
|
66
|
-
expect(toggleGroup).toHaveTextContent('This is the summary section')
|
|
67
|
-
expect(toggleGroup).not.toHaveTextContent('This is the details section')
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('should render with children showing with the defaultExpanded prop', async () => {
|
|
71
|
-
const { container } = render(
|
|
72
|
-
<ToggleGroup
|
|
73
|
-
data-testId="toggle-group"
|
|
74
|
-
transition={false}
|
|
75
|
-
summary="This is the summary section"
|
|
76
|
-
toggleLabel="This is the toggleLabel"
|
|
77
|
-
defaultExpanded
|
|
78
|
-
>
|
|
79
|
-
This is the details section
|
|
80
|
-
</ToggleGroup>
|
|
81
|
-
)
|
|
82
|
-
const toggleGroup = screen.getByTestId('toggle-group')
|
|
83
|
-
const toggle = container.querySelector('button')
|
|
84
|
-
|
|
85
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'true')
|
|
86
|
-
expect(toggleGroup).toHaveTextContent('This is the details section')
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('should have an aria-controls attribute', async () => {
|
|
90
|
-
const { container } = render(
|
|
91
|
-
<ToggleGroup
|
|
92
|
-
transition={false}
|
|
93
|
-
summary="This is the summary section"
|
|
94
|
-
toggleLabel="This is the toggleLabel"
|
|
95
|
-
defaultExpanded
|
|
96
|
-
>
|
|
97
|
-
This is the details section
|
|
98
|
-
</ToggleGroup>
|
|
99
|
-
)
|
|
100
|
-
const content = screen.getByText('This is the details section')
|
|
101
|
-
const toggle = container.querySelector('button')
|
|
102
|
-
|
|
103
|
-
expect(toggle).toHaveAttribute('aria-controls', content.id)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
it('should have an aria-expanded attribute', async () => {
|
|
107
|
-
const { container } = render(
|
|
108
|
-
<ToggleGroup
|
|
109
|
-
transition={false}
|
|
110
|
-
summary="This is the summary section"
|
|
111
|
-
toggleLabel="This is the toggleLabel"
|
|
112
|
-
>
|
|
113
|
-
This is the details section
|
|
114
|
-
</ToggleGroup>
|
|
115
|
-
)
|
|
116
|
-
const toggle = container.querySelector('button')
|
|
117
|
-
|
|
118
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'false')
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
it('should toggle on click events', async () => {
|
|
122
|
-
const { container } = render(
|
|
123
|
-
<ToggleGroup
|
|
124
|
-
transition={false}
|
|
125
|
-
summary="This is the summary section"
|
|
126
|
-
toggleLabel="This is the toggleLabel"
|
|
127
|
-
>
|
|
128
|
-
This is the details section
|
|
129
|
-
</ToggleGroup>
|
|
130
|
-
)
|
|
131
|
-
const toggle = container.querySelector('button')!
|
|
132
|
-
|
|
133
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'false')
|
|
134
|
-
|
|
135
|
-
await userEvent.click(toggle)
|
|
136
|
-
|
|
137
|
-
await waitFor(() => {
|
|
138
|
-
expect(toggle).toHaveAttribute('aria-expanded', 'true')
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('should call onToggle on click events', async () => {
|
|
143
|
-
const onToggle = vi.fn()
|
|
144
|
-
|
|
145
|
-
const { container } = render(
|
|
146
|
-
<ToggleGroup
|
|
147
|
-
transition={false}
|
|
148
|
-
summary="This is the summary section"
|
|
149
|
-
toggleLabel="This is the toggleLabel"
|
|
150
|
-
expanded={false}
|
|
151
|
-
onToggle={onToggle}
|
|
152
|
-
>
|
|
153
|
-
This is the details section
|
|
154
|
-
</ToggleGroup>
|
|
155
|
-
)
|
|
156
|
-
const toggle = container.querySelector('button')!
|
|
157
|
-
|
|
158
|
-
await userEvent.click(toggle)
|
|
159
|
-
|
|
160
|
-
await waitFor(() => {
|
|
161
|
-
const args = onToggle.mock.calls[0]
|
|
162
|
-
|
|
163
|
-
expect(onToggle).toHaveBeenCalledTimes(1)
|
|
164
|
-
expect(args[0].type).toBe('click')
|
|
165
|
-
expect(args[1]).toBe(true)
|
|
166
|
-
})
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
it('should update the toggle screenreader label based on the expanded state', async () => {
|
|
170
|
-
const { container } = render(
|
|
171
|
-
<ToggleGroup
|
|
172
|
-
transition={false}
|
|
173
|
-
summary="This is the summary section"
|
|
174
|
-
toggleLabel={(expanded) => (expanded ? 'Hide content' : 'Show content')}
|
|
175
|
-
>
|
|
176
|
-
This is the details section
|
|
177
|
-
</ToggleGroup>
|
|
178
|
-
)
|
|
179
|
-
const toggle = container.querySelector('button')!
|
|
180
|
-
const scrContent = container.querySelector(
|
|
181
|
-
'[class$="-screenReaderContent"]'
|
|
182
|
-
)
|
|
183
|
-
|
|
184
|
-
expect(scrContent).toHaveTextContent('Show content')
|
|
185
|
-
|
|
186
|
-
await userEvent.click(toggle)
|
|
187
|
-
|
|
188
|
-
await waitFor(() => {
|
|
189
|
-
expect(scrContent).toHaveTextContent('Hide content')
|
|
190
|
-
})
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
it('should accept custom icons', async () => {
|
|
194
|
-
const Icon = (
|
|
195
|
-
<svg height="50" width="50">
|
|
196
|
-
<title>Icon collapsed</title>
|
|
197
|
-
<circle cx="25" cy="25" r="20" />
|
|
198
|
-
</svg>
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
const IconExpanded = (
|
|
202
|
-
<svg height="50" width="50">
|
|
203
|
-
<title>Icon expanded</title>
|
|
204
|
-
<circle cx="25" cy="25" r="20" />
|
|
205
|
-
</svg>
|
|
206
|
-
)
|
|
207
|
-
|
|
208
|
-
const { container } = render(
|
|
209
|
-
<ToggleGroup
|
|
210
|
-
transition={false}
|
|
211
|
-
summary="This is the summary section"
|
|
212
|
-
toggleLabel="This is the toggleLabel"
|
|
213
|
-
icon={() => Icon}
|
|
214
|
-
iconExpanded={() => IconExpanded}
|
|
215
|
-
>
|
|
216
|
-
This is the details section
|
|
217
|
-
</ToggleGroup>
|
|
218
|
-
)
|
|
219
|
-
const toggle = container.querySelector('button')!
|
|
220
|
-
const svg = container.querySelector('svg')!
|
|
221
|
-
|
|
222
|
-
expect(svg).toHaveTextContent('Icon collapsed')
|
|
223
|
-
|
|
224
|
-
await userEvent.click(toggle)
|
|
225
|
-
|
|
226
|
-
await waitFor(() => {
|
|
227
|
-
expect(svg).toHaveTextContent('Icon expanded')
|
|
228
|
-
})
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
it('should meet a11y standards', async () => {
|
|
232
|
-
const { container } = render(
|
|
233
|
-
<ToggleGroup
|
|
234
|
-
transition={false}
|
|
235
|
-
summary="This is the summary section"
|
|
236
|
-
toggleLabel="This is the toggleLabel"
|
|
237
|
-
>
|
|
238
|
-
This is the details section
|
|
239
|
-
</ToggleGroup>
|
|
240
|
-
)
|
|
241
|
-
const axeCheck = await runAxeCheck(container)
|
|
242
|
-
|
|
243
|
-
expect(axeCheck).toBe(true)
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
it('focuses with the focus helper', async () => {
|
|
247
|
-
let toggleRef: any
|
|
248
|
-
const { container } = render(
|
|
249
|
-
<ToggleGroup
|
|
250
|
-
data-testId="toggle-group"
|
|
251
|
-
transition={false}
|
|
252
|
-
summary="This is the summary section"
|
|
253
|
-
toggleLabel="This is the toggleLabel"
|
|
254
|
-
ref={(el) => {
|
|
255
|
-
toggleRef = el
|
|
256
|
-
}}
|
|
257
|
-
>
|
|
258
|
-
This is the details section
|
|
259
|
-
</ToggleGroup>
|
|
260
|
-
)
|
|
261
|
-
const toggle = container.querySelector('button')!
|
|
262
|
-
|
|
263
|
-
expect(document.activeElement).not.toBe(toggle)
|
|
264
|
-
|
|
265
|
-
toggleRef?.focus()
|
|
266
|
-
|
|
267
|
-
expect(document.activeElement).toBe(toggle)
|
|
268
|
-
})
|
|
269
|
-
})
|