|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { setActivePinia, createPinia } from 'pinia'; |
| 3 | +import { effectScope, nextTick } from 'vue'; |
| 4 | +import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; |
| 5 | + |
| 6 | +import { useImageCacheStore } from '@/src/store/image-cache'; |
| 7 | +import { AnnotationToolType } from '@/src/store/tools/types'; |
| 8 | +import { useToolSelectionStore } from '@/src/store/tools/toolSelection'; |
| 9 | +import { useRulerStore } from '@/src/store/tools/rulers'; |
| 10 | +import { useKeyboardShortcuts } from '@/src/composables/useKeyboardShortcuts'; |
| 11 | + |
| 12 | +const IMAGE_ID = 'img-1'; |
| 13 | + |
| 14 | +/** Presses delete under a live listener, then tears it down. */ |
| 15 | +const pressDelete = async () => { |
| 16 | + const scope = effectScope(); |
| 17 | + scope.run(() => useKeyboardShortcuts()); |
| 18 | + |
| 19 | + // a real keystroke starts at the focused element and bubbles to window |
| 20 | + document.activeElement?.dispatchEvent( |
| 21 | + new KeyboardEvent('keydown', { |
| 22 | + key: 'Delete', |
| 23 | + bubbles: true, |
| 24 | + cancelable: true, |
| 25 | + }) |
| 26 | + ); |
| 27 | + await nextTick(); |
| 28 | + |
| 29 | + scope.stop(); |
| 30 | +}; |
| 31 | + |
| 32 | +const addSelectedRuler = () => { |
| 33 | + const id = useRulerStore().addTool({ |
| 34 | + imageID: IMAGE_ID, |
| 35 | + placing: false, |
| 36 | + firstPoint: [1, 1, 1], |
| 37 | + secondPoint: [2, 2, 2], |
| 38 | + }); |
| 39 | + useToolSelectionStore().addSelection(id, AnnotationToolType.Ruler); |
| 40 | + return id; |
| 41 | +}; |
| 42 | + |
| 43 | +describe('delete key removes selected annotations', () => { |
| 44 | + beforeEach(async () => { |
| 45 | + setActivePinia(createPinia()); |
| 46 | + useImageCacheStore().addVTKImageData(vtkImageData.newInstance(), 'CT', { |
| 47 | + id: IMAGE_ID, |
| 48 | + }); |
| 49 | + await nextTick(); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + document.body.innerHTML = ''; |
| 54 | + }); |
| 55 | + |
| 56 | + it('removes the selected annotation when delete is pressed', async () => { |
| 57 | + const ruler = addSelectedRuler(); |
| 58 | + |
| 59 | + await pressDelete(); |
| 60 | + |
| 61 | + expect(useRulerStore().toolByID).not.toHaveProperty(ruler); |
| 62 | + expect(useToolSelectionStore().selection).toEqual([]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('keeps unselected annotations when delete is pressed', async () => { |
| 66 | + const rulerStore = useRulerStore(); |
| 67 | + const kept = rulerStore.addTool({ |
| 68 | + imageID: IMAGE_ID, |
| 69 | + placing: false, |
| 70 | + firstPoint: [3, 3, 3], |
| 71 | + secondPoint: [4, 4, 4], |
| 72 | + }); |
| 73 | + const selected = addSelectedRuler(); |
| 74 | + |
| 75 | + await pressDelete(); |
| 76 | + |
| 77 | + expect(rulerStore.toolByID).not.toHaveProperty(selected); |
| 78 | + expect(rulerStore.toolByID).toHaveProperty(kept); |
| 79 | + }); |
| 80 | + |
| 81 | + it('ignores the delete key while typing in a text field', async () => { |
| 82 | + const ruler = addSelectedRuler(); |
| 83 | + |
| 84 | + const input = document.createElement('input'); |
| 85 | + document.body.appendChild(input); |
| 86 | + input.focus(); |
| 87 | + |
| 88 | + await pressDelete(); |
| 89 | + |
| 90 | + expect(useRulerStore().toolByID).toHaveProperty(ruler); |
| 91 | + }); |
| 92 | + |
| 93 | + // Checking a row in the annotations panel leaves focus on its checkbox |
| 94 | + it('removes the annotation while a checkbox holds focus', async () => { |
| 95 | + const ruler = addSelectedRuler(); |
| 96 | + |
| 97 | + const checkbox = document.createElement('input'); |
| 98 | + checkbox.type = 'checkbox'; |
| 99 | + document.body.appendChild(checkbox); |
| 100 | + checkbox.focus(); |
| 101 | + |
| 102 | + await pressDelete(); |
| 103 | + |
| 104 | + expect(useRulerStore().toolByID).not.toHaveProperty(ruler); |
| 105 | + }); |
| 106 | +}); |
0 commit comments