|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { effectScope, nextTick, ref, type EffectScope } from 'vue'; |
| 3 | +import { createPinia, setActivePinia } from 'pinia'; |
| 4 | +import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera'; |
| 5 | +import { usePersistCameraConfig } from '@/src/composables/usePersistCameraConfig'; |
| 6 | +import useViewCameraStore from '@/src/store/view-configs/camera'; |
| 7 | + |
| 8 | +describe('usePersistCameraConfig', () => { |
| 9 | + let scope: EffectScope; |
| 10 | + beforeEach(() => { |
| 11 | + setActivePinia(createPinia()); |
| 12 | + scope = effectScope(true); |
| 13 | + }); |
| 14 | + afterEach(() => scope.stop()); |
| 15 | + |
| 16 | + // Camera changes reach the saved config on the next task. |
| 17 | + const settle = async () => { |
| 18 | + await nextTick(); |
| 19 | + await new Promise((resolve) => setTimeout(resolve)); |
| 20 | + }; |
| 21 | + |
| 22 | + const setup = () => { |
| 23 | + const camera = vtkCamera.newInstance(); |
| 24 | + const imageId = ref('a'); |
| 25 | + scope.run(() => usePersistCameraConfig('view', imageId, camera)); |
| 26 | + // Stands in for the view fitting its camera to a newly shown image. |
| 27 | + const show = async (id: string, fittedScale: number) => { |
| 28 | + imageId.value = id; |
| 29 | + const cameras = useViewCameraStore(); |
| 30 | + if (!cameras.isCameraInitialized('view', id)) { |
| 31 | + camera.setParallelScale(fittedScale); |
| 32 | + cameras.markCameraAsInitialized('view', id); |
| 33 | + } |
| 34 | + await settle(); |
| 35 | + }; |
| 36 | + return { camera, imageId, show }; |
| 37 | + }; |
| 38 | + |
| 39 | + it('returns an image to its own zoom when another fits at the same scale', async () => { |
| 40 | + const { camera, show } = setup(); |
| 41 | + await show('a', 16); |
| 42 | + await show('b', 16); |
| 43 | + |
| 44 | + await show('a', 16); |
| 45 | + camera.setParallelScale(4); |
| 46 | + await settle(); |
| 47 | + await show('b', 16); |
| 48 | + |
| 49 | + expect(camera.getParallelScale()).toBe(16); |
| 50 | + }); |
| 51 | + |
| 52 | + it('does not save the previous camera for an image that is not shown yet', async () => { |
| 53 | + const { camera, imageId, show } = setup(); |
| 54 | + await show('a', 4); |
| 55 | + |
| 56 | + imageId.value = 'b'; |
| 57 | + await settle(); |
| 58 | + await show('a', 4); |
| 59 | + await show('b', 16); |
| 60 | + |
| 61 | + expect(camera.getParallelScale()).toBe(16); |
| 62 | + }); |
| 63 | +}); |
0 commit comments