-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathCanvasAnnotations.test.jsx
More file actions
185 lines (150 loc) · 5.38 KB
/
CanvasAnnotations.test.jsx
File metadata and controls
185 lines (150 loc) · 5.38 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
import { render, screen } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { CanvasAnnotations } from '../../../src/components/CanvasAnnotations';
import { ScrollTo } from '../../../src/components/ScrollTo';
/** Utility function to wrap CanvasAnnotations */
function createWrapper(props) {
return render(
<CanvasAnnotations
classes={{}}
deselectAnnotation={() => {}}
hoverAnnotation={() => {}}
index={0}
label="A Canvas Label"
selectAnnotation={() => {}}
totalSize={1}
windowId="abc"
{...props}
/>,
);
}
describe('CanvasAnnotations', () => {
let wrapper;
const annotations = [
{
content: 'First Annotation',
id: 'abc123',
tags: ['abc123', 'def456'],
targetId: 'example.com/iiif/12345',
},
{
content: 'Last Annotation',
id: 'xyz321',
tags: [],
targetId: 'example.com/iiif/54321',
},
];
it('renders a heading for a single item', () => {
createWrapper({ annotations });
expect(screen.getByText('Item: [A Canvas Label]')).toBeInTheDocument();
});
it('renders a heading with the appropriate context based on index and totalSize', () => {
wrapper = createWrapper({ annotations, index: 1, totalSize: 2 });
expect(screen.getByText('Right: [A Canvas Label]')).toBeInTheDocument();
wrapper.unmount();
createWrapper({ annotations, index: 0, totalSize: 2 });
expect(screen.getByText('Left: [A Canvas Label]')).toBeInTheDocument();
});
it('renders a List w/ a ListItem for every annotation', () => {
createWrapper({ annotations });
expect(screen.getByRole('menu')).toBeInTheDocument();
expect(screen.getAllByRole('menuitem').length).toEqual(2);
});
test.skip('scrolls to the selected annotation', () => {
wrapper = createWrapper({ annotations, selectedAnnotationId: 'abc123' });
expect(wrapper.find(ScrollTo).length).toEqual(2);
expect(wrapper.find(ScrollTo).first().prop('scrollTo')).toEqual(true);
expect(wrapper.find(ScrollTo).last().prop('scrollTo')).toEqual(false);
});
it('renders a Chip for every tag', () => {
createWrapper({ annotations });
expect(screen.getByText('abc123', { container: 'span.MuiChip-label' })).toBeInTheDocument();
expect(screen.getByText('def456', { container: 'span.MuiChip-label' })).toBeInTheDocument();
});
it('renders nothing when there are no annotations', () => {
createWrapper();
expect(screen.queryByRole('menuitem')).not.toBeInTheDocument();
});
describe('interacting with annotations', () => {
it('triggers the selectAnnotation prop with the correct arguments when clicking an unselected annotation', async () => {
const selectAnnotation = vi.fn();
const user = userEvent.setup();
wrapper = createWrapper({
annotations,
selectAnnotation,
});
await user.click(screen.getByRole('menuitem', { name: /First Annotation/ }));
expect(selectAnnotation).toHaveBeenCalledWith('abc', 'abc123');
});
it('triggers the deselectAnnotation prop with the correct arguments when clicking a selected annotation', async () => {
const deselectAnnotation = vi.fn();
const user = userEvent.setup();
wrapper = createWrapper({
annotations,
deselectAnnotation,
selectedAnnotationId: 'abc123',
});
await user.click(screen.getByRole('menuitem', { name: /First Annotation/ }));
expect(deselectAnnotation).toHaveBeenCalledWith('abc', 'abc123');
});
it('highlights annotations on mouse enter', async () => {
const hoverAnnotation = vi.fn();
const user = userEvent.setup();
wrapper = createWrapper({
annotations: [
{
content: 'Annotation',
id: 'annoId',
tags: [],
targetId: 'example.com/iiif/12345',
},
],
hoverAnnotation,
});
await user.hover(screen.getByRole('menuitem', { name: /Annotation/ }));
expect(hoverAnnotation).toHaveBeenCalledWith('abc', ['annoId']);
});
it('highlights annotations on focus', async () => {
const hoverAnnotation = vi.fn();
const user = userEvent.setup();
wrapper = createWrapper({
annotations: [
{
content: 'Annotation',
id: 'annoId',
tags: [],
targetId: 'example.com/iiif/12345',
},
{
content: 'Annotation2',
id: 'annoId2',
tags: [],
targetId: 'example.com/iiif/12345',
},
],
hoverAnnotation,
});
await user.keyboard('{ArrowDown}');
expect(hoverAnnotation).toHaveBeenCalledWith('abc', ['annoId2']);
});
it('sets the highlighted annotation to null on mouse leave', async () => {
const hoverAnnotation = vi.fn();
const user = userEvent.setup();
wrapper = createWrapper({
annotations: [
{
content: 'Annotation',
id: 'annoId',
tags: [],
targetId: 'example.com/iiif/12345',
},
],
hoverAnnotation,
});
await user.hover(screen.getByRole('menuitem', { name: /Annotation/ }));
expect(hoverAnnotation).toHaveBeenCalledWith('abc', ['annoId']);
await user.hover(screen.getByRole('menu'));
expect(hoverAnnotation).toHaveBeenCalledWith('abc', []);
});
});
});