import { createGlobalPObjectId, type BlockColumnLinks } from "@platforma-sdk/model"; import { mount } from "@vue/test-utils"; import { describe, expect, test, vi } from "vitest"; import { computed, nextTick, shallowRef } from "vue"; // uikit reads the theme from localStorage on import, which this jsdom setup lacks. vi.hoisted(() => { const store = new Map(); vi.stubGlobal("localStorage", { getItem: (key: string) => store.get(key) ?? null, setItem: (key: string, value: string) => store.set(key, value), removeItem: (key: string) => store.delete(key), }); }); vi.mock("../../composition/useOpenBlock", () => ({ useOpenBlock: () => undefined })); const { PlBlockDependencies } = await import("./index"); const { columnLinksKey } = await import("../../columnLinks"); const PATH = createGlobalPObjectId("samples", "metadata.path"); const links: BlockColumnLinks = { blockId: "samples", dependencies: [], dependents: [ { producer: { blockId: "samples", name: "metadata.path" }, holderBlockId: "enrichment", ref: PATH, values: null, columnLabel: "Path", check: { type: "unavailable" }, }, ], labels: { enrichment: "Enrichment" }, }; describe("PlBlockDependencies", () => { test("shows nothing outside a block", () => { const wrapper = mount(PlBlockDependencies); expect(wrapper.find("[data-testid^='pl-block-dependencies-alert']").exists()).toBe(false); }); test("warns about blocks using an output the block does not export", () => { const wrapper = mount(PlBlockDependencies, { global: { provide: { [columnLinksKey]: computed(() => links) } }, }); const alert = wrapper.find("[data-testid='pl-block-dependencies-alert-usedBy']"); expect(alert.exists()).toBe(true); expect(alert.text()).toContain("Enrichment"); }); test("folds the warning to one line, and keeps it folded when the block opens again", async () => { const folded = { ...links, blockId: "folded" }; const wrapper = mountWith(computed(() => folded)); await wrapper.find("[data-testid='pl-block-dependencies-toggle']").trigger("click"); expect(wrapper.find("[data-testid='pl-block-dependencies-count']").text()).toBe("1 block"); expect(wrapper.find("[data-testid='pl-alert-message']").exists()).toBe(false); expect(wrapper.text()).not.toContain("Enrichment"); const reopened = mountWith(computed(() => folded)); expect(reopened.find("[data-testid='pl-block-dependencies-count']").exists()).toBe(true); expect(reopened.find("[data-testid='pl-block-dependencies-updated']").exists()).toBe(false); }); test("marks a folded warning updated once it says something else, until it is read", async () => { const current = shallowRef({ ...links, blockId: "updated" }); const wrapper = mountWith(current); await wrapper.find("[data-testid='pl-block-dependencies-toggle']").trigger("click"); current.value = withDependent(current.value, "dca", "DCA"); await nextTick(); expect(wrapper.find("[data-testid='pl-block-dependencies-updated']").exists()).toBe(true); expect(wrapper.find("[data-testid='pl-block-dependencies-count']").text()).toBe("2 blocks"); await wrapper.find("[data-testid='pl-block-dependencies-toggle']").trigger("click"); expect(wrapper.text()).toContain("DCA"); await wrapper.find("[data-testid='pl-block-dependencies-toggle']").trigger("click"); expect(wrapper.find("[data-testid='pl-block-dependencies-updated']").exists()).toBe(false); }); }); // Internals function mountWith(columnLinks: { readonly value: BlockColumnLinks }) { return mount(PlBlockDependencies, { global: { provide: { [columnLinksKey]: computed(() => columnLinks.value) } }, }); } function withDependent(of: BlockColumnLinks, blockId: string, label: string): BlockColumnLinks { const [link] = of.dependents; if (link === undefined) throw new Error("The links have no dependent to copy"); return { ...of, dependents: [...of.dependents, { ...link, holderBlockId: blockId }], labels: { ...of.labels, [blockId]: label }, }; }