-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathOpenSeadragonComponent.test.jsx
More file actions
107 lines (89 loc) · 3.22 KB
/
OpenSeadragonComponent.test.jsx
File metadata and controls
107 lines (89 loc) · 3.22 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
import { render } from '@tests/utils/test-utils';
import OpenSeadragon from 'openseadragon';
import OpenSeadragonComponent from '../../../src/components/OpenSeadragonComponent';
vi.mock('openseadragon');
describe('OpenSeadragonComponent', () => {
let addOnceHandler;
let fitBoundsWithConstraints;
beforeEach(() => {
addOnceHandler = vi.fn();
fitBoundsWithConstraints = vi.fn();
// Mock methods used in the component
OpenSeadragon.mockImplementation(function () {
return {
addHandler: vi.fn(),
addOnceHandler,
canvas: {},
destroy: vi.fn(),
innerTracker: {},
removeAllHandlers: vi.fn(),
viewport: {
centerSpringX: { target: { value: 0 } },
centerSpringY: { target: { value: 0 } },
fitBounds: vi.fn(),
fitBoundsWithConstraints,
zoomSpring: { target: { value: 1 } },
},
world: { addOnceHandler },
};
});
OpenSeadragon.Rect = vi.fn(function (x, y, width, height) {
return { height, width, x, y };
});
});
/**
* Invoke the most recently registered tile-loaded handler
*/
function invokeTileLoadedHandler() {
// Extract and invoke the most recently registered 'tile-loaded' handler
// to simulate OSD firing the event when tiles finish loading
// OSD provides addOnceHandler to register events on viewer
const { lastCall } = addOnceHandler.mock; // Vitest's lastCall
const [_eventName, tileLoadedHandler] = lastCall || [];
if (tileLoadedHandler) tileLoadedHandler();
}
/**
* Render component and complete initial tile loading
* @param {Array} bounds - Initial bounds
* @returns {object} Render result
*/
function renderAndInitialize(bounds = [0, 0, 5000, 3000]) {
const result = render(<OpenSeadragonComponent windowId="test" viewerConfig={{ bounds }} />);
// Component registers a 'tile-loaded' handler during mount to set initial viewport
invokeTileLoadedHandler();
// Clear mocks after initialization
fitBoundsWithConstraints.mockClear();
addOnceHandler.mockClear();
return result;
}
it('resets zoom and center when bounds change', () => {
const { rerender } = renderAndInitialize();
// Change bounds to different dimensions
rerender(
<OpenSeadragonComponent windowId="test" viewerConfig={{ bounds: [0, 0, 3000, 2000] }} />,
);
// Component registered a 'tile-loaded' handler when bounds change
invokeTileLoadedHandler();
// Should call fitBoundsWithConstraints with the new bounds to reset zoom and center
expect(fitBoundsWithConstraints).toHaveBeenCalledWith(
expect.objectContaining({
height: 2000,
width: 3000,
x: 0,
y: 0,
}),
true,
);
});
it('does not reset zoom when bounds remain the same', () => {
const { rerender } = renderAndInitialize();
// Rerender with same bounds
rerender(
<OpenSeadragonComponent windowId="test" viewerConfig={{ bounds: [0, 0, 5000, 3000] }} />,
);
// Should not register a new tile-loaded handler
expect(addOnceHandler).not.toHaveBeenCalled();
// Should not call fitBoundsWithConstraints
expect(fitBoundsWithConstraints).not.toHaveBeenCalled();
});
});