-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathSidebarIndexTableOfContents.test.jsx
More file actions
250 lines (212 loc) · 7.15 KB
/
SidebarIndexTableOfContents.test.jsx
File metadata and controls
250 lines (212 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { render, screen, waitFor } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { Utils } from 'manifesto.js';
import { act } from '@testing-library/react';
import { SidebarIndexTableOfContents } from '../../../src/components/SidebarIndexTableOfContents';
import ConnectedSidebarIndexTableOfContents from '../../../src/containers/SidebarIndexTableOfContents';
import manifestVersion2 from '../../fixtures/version-2/structures.json';
import manifestVersion3 from '../../fixtures/version-3/structures.json';
/**
* Create wrapper for SidebarIndexTableOfContents component
* @param {*} props
*/
function createWrapper(props) {
const manifest = Utils.parseManifest(props.manifest ? props.manifest : manifestVersion2);
return render(
<SidebarIndexTableOfContents
id="something"
classes={{}}
treeStructure={props.treeStructure ? props.treeStructure : manifest.getDefaultTree()}
visibleNodeIds={props.visibleNodeIds ? props.visibleNodeIds : []}
expandedNodeIds={props.expandedNodeIds ? props.expandedNodeIds : []}
containerRef={props.containerRef}
nodeIdToScrollTo={props.nodeIdToScrollTo}
{...props}
/>,
);
}
/**
* Create an interactive wrapper for SidebarIndexTableOfContents component hooked up
* to the redux store (because the controlled treeview really needs to be connected to
* write a reasonable test for it)
*/
function createInteractiveWrapper({ manifest = manifestVersion3, ...props }) {
return render(
<ConnectedSidebarIndexTableOfContents
id="something"
windowId="a"
{...props}
/>,
{
preloadedState: {
companionWindows: {
something: {
id: 'something',
},
},
manifests: {
'http://example.com/manifest.json': {
json: manifest,
},
},
windows: {
a: { manifestId: 'http://example.com/manifest.json' },
},
},
},
);
}
describe('SidebarIndexTableOfContents', () => {
let setCanvas;
beforeEach(() => {
setCanvas = vi.fn();
});
it('does not render a TreeView if the tree structure is missing', () => {
const { container } = createWrapper({
treeStructure: undefined,
});
expect(container).toBeEmptyDOMElement();
});
it('renders a tree item for every visible node', () => {
const { unmount } = createWrapper({});
expect(screen.getAllByRole('treeitem')).toHaveLength(5);
unmount();
createWrapper({
treeStructure: {
nodes: [
{
id: '0',
nodes: [
{
id: '0-0',
nodes: [],
},
{
id: '0-1',
nodes: [],
},
],
},
],
},
});
expect(screen.getByRole('treeitem')).toBeInTheDocument();
});
it('accepts missing nodes property for tree structure and tree nodes', () => {
const { unmount } = createWrapper({
treeStructure: { nodes: undefined },
});
expect(screen.queryByRole('treeitem')).not.toBeInTheDocument();
unmount();
createWrapper({
treeStructure: {
nodes: [{ id: '0' }],
},
});
expect(screen.getByRole('treeitem')).toBeInTheDocument();
});
it('toggles branch nodes on click', async () => {
const user = userEvent.setup();
const { store } = createInteractiveWrapper({});
expect(screen.getByRole('treeitem')).toBeInTheDocument();
const root = screen.getByRole('treeitem');
await user.click(root.querySelector('.MuiTreeItem-iconContainer')); // eslint-disable-line testing-library/no-node-access
expect(screen.getAllByRole('treeitem')).toHaveLength(5);
await user.click(root.querySelector('.MuiTreeItem-iconContainer')); // eslint-disable-line testing-library/no-node-access
await waitFor(() => {
expect(screen.getByRole('treeitem')).toBeInTheDocument();
});
expect(store.getState().windows.a.canvasId).toBeUndefined();
});
it('toggles branch nodes with arrow keys', async () => {
const user = userEvent.setup();
const { store } = createInteractiveWrapper({});
const root = screen.getByRole('treeitem');
act(() => {
root.focus();
});
await user.keyboard('{ArrowRight}');
expect(screen.getAllByRole('treeitem')).toHaveLength(5);
await user.keyboard('{ArrowLeft}');
await waitFor(() => {
expect(screen.getByRole('treeitem')).toBeInTheDocument();
});
expect(store.getState().windows.a.canvasId).toBeUndefined();
});
it('toggles branch nodes with Space or Enter key', async () => {
const user = userEvent.setup();
const { store } = createInteractiveWrapper({});
const root = screen.getByRole('treeitem');
act(() => {
root.focus();
});
await user.keyboard('{Enter}');
expect(screen.getAllByRole('treeitem')).toHaveLength(5);
await user.keyboard('{ArrowLeft}');
await waitFor(() => {
expect(screen.getByRole('treeitem')).toBeInTheDocument();
});
await user.keyboard(' ');
expect(screen.getAllByRole('treeitem')).toHaveLength(5);
expect(store.getState().windows.a.canvasId).toBeUndefined();
});
it('calls setCanvas only on click for leaf nodes', async () => {
const user = userEvent.setup();
const { store } = createInteractiveWrapper({});
const root = screen.getByRole('treeitem');
act(() => {
root.focus();
});
await user.keyboard('{Enter}');
const leafNode = screen.getAllByRole('treeitem')[1];
act(() => {
leafNode.focus();
});
await user.keyboard('{Enter}');
expect(store.getState().windows.a.canvasId).toEqual('http://foo.test/1/canvas/c2');
});
it('sets the canvas to a start canvas if present (IIIF v2)', async () => {
const user = userEvent.setup();
createWrapper({
expandItems: () => { },
manifest: manifestVersion2,
setCanvas,
windowId: 'a',
});
const leafNode = screen.getAllByRole('treeitem')[3];
act(() => {
leafNode.focus();
});
await user.keyboard('{Enter}');
expect(setCanvas).toHaveBeenLastCalledWith('a', 'http://foo.test/1/canvas/c11');
});
it('sets the canvas to a start canvas if present (IIIF v3)', async () => {
const user = userEvent.setup();
const { store } = createInteractiveWrapper({
manifest: manifestVersion3,
});
const root = screen.getByRole('treeitem');
act(() => {
root.focus();
});
await user.keyboard('{Enter}');
const leafNode1 = screen.getAllByRole('treeitem')[2];
act(() => {
leafNode1.focus();
});
await user.keyboard('{Enter}');
expect(store.getState().windows.a.canvasId).toEqual('http://foo.test/1/canvas/c7');
const leafNode2 = screen.getAllByRole('treeitem')[3];
act(() => {
leafNode2.focus();
});
await user.keyboard('{Enter}');
expect(store.getState().windows.a.canvasId).toEqual('http://foo.test/1/canvas/c9');
const leafNode3 = screen.getAllByRole('treeitem')[4];
act(() => {
leafNode3.focus();
});
await user.keyboard('{Enter}');
expect(store.getState().windows.a.canvasId).toEqual('http://foo.test/1/canvas/c10');
});
});