-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathGalleryViewThumbnail.test.js
More file actions
129 lines (116 loc) · 5.52 KB
/
GalleryViewThumbnail.test.js
File metadata and controls
129 lines (116 loc) · 5.52 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
import { render, fireEvent, screen } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { Utils } from 'manifesto.js';
import { mockAllIsIntersecting } from 'react-intersection-observer/test-utils';
import manifestJson from '../../fixtures/version-2/019.json';
import { GalleryViewThumbnail } from '../../../src/components/GalleryViewThumbnail';
/** create wrapper */
function createWrapper(props) {
return render(
<GalleryViewThumbnail
canvas={Utils.parseManifest(manifestJson).getSequences()[0].getCanvases()[0]}
focusOnCanvas={() => {}}
setCanvas={() => {}}
{...props}
/>,
);
}
describe('GalleryView', () => {
beforeEach(() => {
window.HTMLElement.prototype.scrollIntoView = vi.fn();
});
it('renders the thumbnail', () => {
createWrapper({ config: { height: 55 } });
expect(screen.getByRole('presentation')).toBeInTheDocument();
expect(screen.getByRole('presentation')).toHaveStyle('height: 55px');
});
it('sets the selected canvas on click', async () => {
const setCanvas = vi.fn();
createWrapper({ setCanvas });
const user = userEvent.setup();
await user.click(screen.getByRole('button'));
expect(setCanvas).toHaveBeenCalledWith('http://iiif.io/api/presentation/2.0/example/fixtures/canvas/24/c1.json');
});
it('sets the window mode if the selected canvas is clicked', async () => {
const focusOnCanvas = vi.fn();
createWrapper({ focusOnCanvas, selected: true });
const user = userEvent.setup();
await user.click(screen.getByRole('button'));
expect(focusOnCanvas).toHaveBeenCalled();
});
it('sets the window mode if the user hits enter while on a canvas', () => {
const focusOnCanvas = vi.fn();
createWrapper({ focusOnCanvas, selected: true });
const button = screen.getByRole('button');
button.focus();
fireEvent.keyUp(button, { code: 'Enter', key: 'Enter' });
expect(focusOnCanvas).toHaveBeenCalled();
});
it('sets the window mode if the user hits space while on a canvas', () => {
const focusOnCanvas = vi.fn();
createWrapper({ focusOnCanvas, selected: true });
const button = screen.getByRole('button');
button.focus();
fireEvent.keyUp(button, { code: ' ', key: ' ' });
expect(focusOnCanvas).toHaveBeenCalled();
});
it('sets the canvas if the user hits a key (non-space or non-enter) while on a canvas', () => {
const setCanvas = vi.fn();
createWrapper({ selected: true, setCanvas });
const button = screen.getByRole('button');
button.focus();
fireEvent.keyUp(button, { code: 'd', key: 'd' });
expect(setCanvas).toHaveBeenCalledWith('http://iiif.io/api/presentation/2.0/example/fixtures/canvas/24/c1.json');
});
describe('on-demand annotation fetching', () => {
const canvas = {
getHeight: () => 50,
getLabel: () => ({ getValue: () => 'label' }),
getServices: vi.fn(),
getThumbnail: vi.fn(),
getType: vi.fn(),
getWidth: () => 50,
isCanvas: vi.fn(),
isCollection: vi.fn(),
isManifest: vi.fn(),
};
let requestCanvasAnnotations;
beforeEach(() => { requestCanvasAnnotations = vi.fn(); });
it('triggers requestCanvasAnnotations when there is an intersection and no annotions ', () => {
createWrapper({ annotationsCount: 0, canvas, requestCanvasAnnotations });
mockAllIsIntersecting(true);
expect(requestCanvasAnnotations).toHaveBeenCalledTimes(1);
});
it('does nothing if there is an intersection and existing annotations', () => {
createWrapper({ annotationsCount: 1, canvas, requestCanvasAnnotations });
mockAllIsIntersecting(true);
expect(requestCanvasAnnotations).not.toHaveBeenCalled();
});
it('does nothing if there is no intersection', () => {
createWrapper({ annotationsCount: 0, canvas, requestCanvasAnnotations });
expect(requestCanvasAnnotations).not.toHaveBeenCalled();
});
});
describe('annotation count chip', () => {
it('hides the chip if there are no annotations', () => {
const { container } = createWrapper({ annotationsCount: 0 });
expect(container.querySelector('.MuiChip-root')).not.toBeInTheDocument(); // eslint-disable-line testing-library/no-node-access, testing-library/no-container
});
it('shows the number of search annotations on a canvas', () => {
const { container } = createWrapper({ annotationsCount: 50 });
expect(container.querySelector('.MuiChip-root')).toBeInTheDocument(); // eslint-disable-line testing-library/no-node-access, testing-library/no-container
expect(container.querySelector('.MuiChip-root')).toHaveTextContent('50'); // eslint-disable-line testing-library/no-node-access, testing-library/no-container
});
});
describe('search annotation count chip', () => {
it('hides the chip if there are no annotations', () => {
const { container } = createWrapper({ searchAnnotationsCount: 0 });
expect(container.querySelector('.MuiChip-root')).not.toBeInTheDocument(); // eslint-disable-line testing-library/no-node-access, testing-library/no-container
});
it('shows the number of search annotations on a canvas', () => {
const { container } = createWrapper({ searchAnnotationsCount: 50 });
expect(container.querySelector('.MuiChip-root')).toBeInTheDocument(); // eslint-disable-line testing-library/no-node-access, testing-library/no-container
expect(container.querySelector('.MuiChip-root')).toHaveTextContent('50'); // eslint-disable-line testing-library/no-node-access, testing-library/no-container
});
});
});