Skip to content

Commit 917dba5

Browse files
committed
Add a test for OpenSeadragonComponent
1 parent 6f67d33 commit 917dba5

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { render } from '@tests/utils/test-utils';
2+
import OpenSeadragon from 'openseadragon';
3+
import OpenSeadragonComponent from '../../../src/components/OpenSeadragonComponent';
4+
5+
vi.mock('openseadragon');
6+
7+
describe('OpenSeadragonComponent', () => {
8+
let addOnceHandler;
9+
let fitBoundsWithConstraints;
10+
11+
beforeEach(() => {
12+
addOnceHandler = vi.fn();
13+
fitBoundsWithConstraints = vi.fn();
14+
15+
// Mock methods used in the component
16+
OpenSeadragon.mockImplementation(() => ({
17+
addHandler: vi.fn(),
18+
addOnceHandler,
19+
canvas: {},
20+
destroy: vi.fn(),
21+
innerTracker: {},
22+
removeAllHandlers: vi.fn(),
23+
viewport: {
24+
fitBounds: vi.fn(),
25+
fitBoundsWithConstraints,
26+
centerSpringX: { target: { value: 0 } },
27+
centerSpringY: { target: { value: 0 } },
28+
zoomSpring: { target: { value: 1 } },
29+
},
30+
world: { addOnceHandler },
31+
}));
32+
33+
OpenSeadragon.Rect = vi.fn((x, y, width, height) => ({ x, y, width, height }));
34+
});
35+
36+
function invokeTileLoadedHandler() {
37+
// Extract and invoke the most recently registered 'tile-loaded' handler
38+
// to simulate OSD firing the event when tiles finish loading
39+
// OSD provides addOnceHandler to register events on viewer
40+
const lastCall = addOnceHandler.mock.lastCall; // Vitest's lastCall
41+
const [_eventName, tileLoadedHandler] = lastCall || [];
42+
if (tileLoadedHandler) tileLoadedHandler();
43+
}
44+
45+
function renderAndInitialize(bounds = [0, 0, 5000, 3000]) {
46+
const result = render(
47+
<OpenSeadragonComponent windowId="test" viewerConfig={{ bounds }} />,
48+
);
49+
50+
// Component registers a 'tile-loaded' handler during mount to set initial viewport
51+
invokeTileLoadedHandler();
52+
53+
// Clear mocks after initialization
54+
fitBoundsWithConstraints.mockClear();
55+
addOnceHandler.mockClear();
56+
57+
return result;
58+
}
59+
60+
it('resets zoom and center when bounds change', () => {
61+
const { rerender } = renderAndInitialize();
62+
63+
// Change bounds to different dimensions
64+
rerender(
65+
<OpenSeadragonComponent windowId="test" viewerConfig={{ bounds: [0, 0, 3000, 2000] }} />,
66+
);
67+
68+
// Component registered a 'tile-loaded' handler when bounds change
69+
invokeTileLoadedHandler();
70+
71+
// Should call fitBoundsWithConstraints with the new bounds to reset zoom and center
72+
expect(fitBoundsWithConstraints).toHaveBeenCalledWith(
73+
expect.objectContaining({
74+
x: 0,
75+
y: 0,
76+
width: 3000,
77+
height: 2000,
78+
}),
79+
true,
80+
);
81+
});
82+
83+
it('does not reset zoom when bounds remain the same', () => {
84+
const { rerender } = renderAndInitialize();
85+
86+
// Rerender with same bounds
87+
rerender(
88+
<OpenSeadragonComponent windowId="test" viewerConfig={{ bounds: [0, 0, 5000, 3000] }} />,
89+
);
90+
91+
// Should not register a new tile-loaded handler
92+
expect(addOnceHandler).not.toHaveBeenCalled();
93+
94+
// Should not call fitBoundsWithConstraints
95+
expect(fitBoundsWithConstraints).not.toHaveBeenCalled();
96+
});
97+
});

0 commit comments

Comments
 (0)