|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { Collection, Feature } from 'ol'; |
| 3 | +import { SKResourceService } from './resources.service'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression test for #501 — the "Move" action on the Note details surfaces |
| 7 | + * must start the map Modify interaction on the *actual rendered note feature* |
| 8 | + * that the opening map click loaded into the draw collection. Modifying that |
| 9 | + * feature (not a reconstructed copy) is what visually moves the icon and yields |
| 10 | + * the coordinates the save persists. When no such feature is present (details |
| 11 | + * reached without a map click), Move is a no-op. |
| 12 | + * |
| 13 | + * It only touches `this.mapInteract`, so exercise it on a bare prototype |
| 14 | + * instance — no Angular DI (same approach as note-details-sizing.spec.ts). |
| 15 | + */ |
| 16 | +type FakeMapInteract = { |
| 17 | + draw: { features: Collection<Feature> }; |
| 18 | + startModifying: ReturnType<typeof vi.fn>; |
| 19 | +}; |
| 20 | + |
| 21 | +function noteFeature(id: string): Feature { |
| 22 | + const f = new Feature(); |
| 23 | + f.setId(id); |
| 24 | + return f; |
| 25 | +} |
| 26 | + |
| 27 | +function svc(mapInteract: FakeMapInteract) { |
| 28 | + const s = Object.create(SKResourceService.prototype) as SKResourceService; |
| 29 | + Object.assign(s as unknown as Record<string, unknown>, { mapInteract }); |
| 30 | + return s; |
| 31 | +} |
| 32 | + |
| 33 | +describe('startNoteModify (#501)', () => { |
| 34 | + it('modifies the note feature loaded by the map click', () => { |
| 35 | + const feature = noteFeature('note.note-1'); |
| 36 | + const mapInteract: FakeMapInteract = { |
| 37 | + draw: { features: new Collection([feature]) }, |
| 38 | + startModifying: vi.fn() |
| 39 | + }; |
| 40 | + |
| 41 | + svc(mapInteract).startNoteModify('note-1'); |
| 42 | + |
| 43 | + expect(mapInteract.startModifying).toHaveBeenCalledWith({ type: 'note' }); |
| 44 | + // the same rendered feature object is what gets modified |
| 45 | + expect(mapInteract.draw.features.item(0)).toBe(feature); |
| 46 | + }); |
| 47 | + |
| 48 | + it('restricts the interaction to the target note feature', () => { |
| 49 | + const target = noteFeature('note.note-1'); |
| 50 | + const mapInteract: FakeMapInteract = { |
| 51 | + draw: { features: new Collection([target, noteFeature('note.note-2')]) }, |
| 52 | + startModifying: vi.fn() |
| 53 | + }; |
| 54 | + |
| 55 | + svc(mapInteract).startNoteModify('note-1'); |
| 56 | + |
| 57 | + expect(mapInteract.draw.features.getLength()).toBe(1); |
| 58 | + expect(mapInteract.draw.features.item(0)).toBe(target); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does nothing when the target note feature is not loaded', () => { |
| 62 | + const mapInteract: FakeMapInteract = { |
| 63 | + draw: { features: new Collection([noteFeature('route.r-1')]) }, |
| 64 | + startModifying: vi.fn() |
| 65 | + }; |
| 66 | + |
| 67 | + svc(mapInteract).startNoteModify('note-1'); |
| 68 | + |
| 69 | + expect(mapInteract.startModifying).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('does nothing when no feature is loaded (no map click)', () => { |
| 73 | + const mapInteract: FakeMapInteract = { |
| 74 | + draw: { features: new Collection([]) }, |
| 75 | + startModifying: vi.fn() |
| 76 | + }; |
| 77 | + |
| 78 | + svc(mapInteract).startNoteModify('note-1'); |
| 79 | + |
| 80 | + expect(mapInteract.startModifying).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments