|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest" |
| 2 | +import { createAssessmentSelectionStore } from "@/store/assessmentSelectionStore" |
| 3 | +import { ApollonEditor } from "@/apollon-editor" |
| 4 | + |
| 5 | +describe("assessmentSelectionStore host-driven highlights", () => { |
| 6 | + let store: ReturnType<typeof createAssessmentSelectionStore> |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + store = createAssessmentSelectionStore() |
| 10 | + }) |
| 11 | + |
| 12 | + it("starts with an empty highlight map", () => { |
| 13 | + expect(store.getState().highlightedElements).toEqual({}) |
| 14 | + }) |
| 15 | + |
| 16 | + it("setElementHighlights replaces the whole map", () => { |
| 17 | + store.getState().setElementHighlights({ |
| 18 | + "node-1": "rgba(23,162,184,0.3)", |
| 19 | + "edge-2": "rgba(219,53,69,0.6)", |
| 20 | + }) |
| 21 | + expect(store.getState().highlightedElements).toEqual({ |
| 22 | + "node-1": "rgba(23,162,184,0.3)", |
| 23 | + "edge-2": "rgba(219,53,69,0.6)", |
| 24 | + }) |
| 25 | + |
| 26 | + // A subsequent call replaces rather than merges. |
| 27 | + store.getState().setElementHighlights({ "node-3": "#ff0000" }) |
| 28 | + expect(store.getState().highlightedElements).toEqual({ |
| 29 | + "node-3": "#ff0000", |
| 30 | + }) |
| 31 | + |
| 32 | + // An empty map clears all highlights. |
| 33 | + store.getState().setElementHighlights({}) |
| 34 | + expect(store.getState().highlightedElements).toEqual({}) |
| 35 | + }) |
| 36 | + |
| 37 | + it("host highlights are independent of selection state", () => { |
| 38 | + store.getState().setElementHighlights({ "node-1": "#ff0000" }) |
| 39 | + store.getState().selectMultipleElements(["node-1", "node-2"]) |
| 40 | + store.getState().setHighlightedElement("node-2") |
| 41 | + |
| 42 | + // Toggling assessment-selection mode off clears the interactive selection |
| 43 | + // and hover, but must NOT wipe the host-driven highlight overlay. |
| 44 | + store.getState().setAssessmentSelectionMode(false) |
| 45 | + |
| 46 | + const state = store.getState() |
| 47 | + expect(state.selectedElementIds).toEqual([]) |
| 48 | + expect(state.highlightedElementId).toBeNull() |
| 49 | + expect(state.highlightedElements).toEqual({ "node-1": "#ff0000" }) |
| 50 | + }) |
| 51 | + |
| 52 | + it("reset clears host highlights along with the rest of the state", () => { |
| 53 | + store.getState().setElementHighlights({ "node-1": "#ff0000" }) |
| 54 | + store.getState().reset() |
| 55 | + expect(store.getState().highlightedElements).toEqual({}) |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +describe("ApollonEditor host-driven highlight API", () => { |
| 60 | + let container: HTMLElement |
| 61 | + let editor: ApollonEditor |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + container = document.createElement("div") |
| 65 | + document.body.appendChild(container) |
| 66 | + editor = new ApollonEditor(container) |
| 67 | + }) |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + editor.destroy() |
| 71 | + container.remove() |
| 72 | + }) |
| 73 | + |
| 74 | + it("normalizes a Map input to a Record", () => { |
| 75 | + const map = new Map([ |
| 76 | + ["node-1", "#ff0000"], |
| 77 | + ["edge-2", "#00ff00"], |
| 78 | + ]) |
| 79 | + editor.setElementHighlights(map) |
| 80 | + expect(editor.getElementHighlights()).toEqual({ |
| 81 | + "node-1": "#ff0000", |
| 82 | + "edge-2": "#00ff00", |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it("isolates stored highlights from the caller's input and returned snapshot", () => { |
| 87 | + // Map and Record inputs share one normalization path, so a single input |
| 88 | + // type exercises the defensive copy on the way in... |
| 89 | + const input = new Map([["node-1", "#ff0000"]]) |
| 90 | + editor.setElementHighlights(input) |
| 91 | + input.set("node-1", "tampered") |
| 92 | + expect(editor.getElementHighlights()).toEqual({ "node-1": "#ff0000" }) |
| 93 | + |
| 94 | + // ...and the getter must hand back an independent copy on the way out. |
| 95 | + const snapshot = editor.getElementHighlights() |
| 96 | + snapshot["node-1"] = "tampered" |
| 97 | + expect(editor.getElementHighlights()).toEqual({ "node-1": "#ff0000" }) |
| 98 | + }) |
| 99 | + |
| 100 | + it("clears on null but treats undefined as a no-op", () => { |
| 101 | + editor.setElementHighlights({ "node-1": "#ff0000" }) |
| 102 | + editor.setElementHighlights(null) |
| 103 | + expect(editor.getElementHighlights()).toEqual({}) |
| 104 | + |
| 105 | + editor.setElementHighlights({ "node-1": "#ff0000" }) |
| 106 | + editor.setElementHighlights(undefined) |
| 107 | + expect(editor.getElementHighlights()).toEqual({ "node-1": "#ff0000" }) |
| 108 | + }) |
| 109 | +}) |
0 commit comments