|
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | +import { store } from "@/store"; |
| 3 | +import { handleHostStoreChange } from "./handleHostStoreChange"; |
| 4 | + |
| 5 | +describe("handleHostStoreChange", () => { |
| 6 | + let listeners: (() => void)[] = []; |
| 7 | + let hostState: Record<string, unknown> = {}; |
| 8 | + const hostStore = { |
| 9 | + get: (key: string) => hostState[key], |
| 10 | + set: (key: string, value: unknown) => { |
| 11 | + hostState = { ...hostState, [key]: value }; |
| 12 | + listeners.forEach((l) => void l()); |
| 13 | + }, |
| 14 | + subscribe: (_l: () => void) => { |
| 15 | + listeners.push(_l); |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + listeners = []; |
| 21 | + hostState = {}; |
| 22 | + }); |
| 23 | + |
| 24 | + it("should do nothing without host store", () => { |
| 25 | + store.setState({ configuration: {} }); |
| 26 | + const oldState = store.getState(); |
| 27 | + handleHostStoreChange(); |
| 28 | + const newState = store.getState(); |
| 29 | + expect(newState).toBe(oldState); |
| 30 | + expect(newState).toEqual(oldState); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should synchronize theme mode", () => { |
| 34 | + store.setState({ configuration: { hostStore } }); |
| 35 | + expect(store.getState().themeMode).toBeUndefined(); |
| 36 | + hostStore.set("themeMode", "light"); |
| 37 | + handleHostStoreChange(); |
| 38 | + expect(store.getState().themeMode).toEqual("light"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should generate callback requests", () => { |
| 42 | + const extensions = [{ name: "e0", version: "0", contributes: ["panels"] }]; |
| 43 | + store.setState({ |
| 44 | + configuration: { hostStore }, |
| 45 | + extensions, |
| 46 | + contributionsResult: { |
| 47 | + status: "ok", |
| 48 | + data: { |
| 49 | + extensions, |
| 50 | + contributions: { |
| 51 | + panels: [ |
| 52 | + { |
| 53 | + name: "p0", |
| 54 | + extension: "e0", |
| 55 | + layout: { |
| 56 | + function: { |
| 57 | + name: "layout", |
| 58 | + parameters: [], |
| 59 | + return: {}, |
| 60 | + }, |
| 61 | + inputs: [], |
| 62 | + outputs: [], |
| 63 | + }, |
| 64 | + callbacks: [ |
| 65 | + { |
| 66 | + function: { |
| 67 | + name: "callback", |
| 68 | + parameters: [], |
| 69 | + return: {}, |
| 70 | + }, |
| 71 | + inputs: [{ id: "@app", property: "variableName" }], |
| 72 | + outputs: [{ id: "select", property: "value" }], |
| 73 | + }, |
| 74 | + ], |
| 75 | + initialState: {}, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }); |
| 82 | + hostStore.set("variableName", "CHL"); |
| 83 | + handleHostStoreChange(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments