import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { Endpoint } from "../utils/endpoint"; import { DataProviderKey } from "../utils/dataProviderKey"; import API, { APIConfiguration, type ApiGetResult, } from "@supersoniks/concorde/core/utils/api"; import { PublisherManager } from "@supersoniks/concorde/core/utils/PublisherProxy"; import { get } from "./api"; const staticConfig: APIConfiguration = { serviceURL: "https://api.example.test", token: null, userName: null, password: null, authToken: null, tokenProvider: null, }; async function flushGetMicrotasks() { await Promise.resolve(); await Promise.resolve(); } function mockPayload(result: T) { return { request: new Request("https://api.example.test/items/1"), response: new Response(JSON.stringify(result), { status: 200, headers: { "Content-Type": "application/json" }, }), result, }; } describe("get", () => { beforeEach(() => { vi.spyOn(API.prototype, "getDetailed").mockResolvedValue( mockPayload({ id: "loaded" }) as never, ); }); afterEach(() => { vi.restoreAllMocks(); document.body.replaceChildren(); }); it("assigne ApiGetResult avec configurationKey (publisher)", async () => { const pubId = "getSpecPub1"; PublisherManager.get(pubId).set(staticConfig); const confKey = new DataProviderKey(pubId); const itemEp = new Endpoint<{ id: string }>("items/1"); const tag = "test-api-get-config-key"; class C extends HTMLElement { @get(itemEp, confKey) payload: ApiGetResult<{ id: string }> | null = null; } if (!customElements.get(tag)) { customElements.define(tag, C); } const el = document.createElement(tag) as InstanceType; document.body.appendChild(el); await flushGetMicrotasks(); expect(el.payload?.result).toEqual({ id: "loaded" }); expect(el.payload?.response?.ok).toBe(true); expect(el.payload?.request.url).toContain("api.example.test"); expect(API.prototype.getDetailed).toHaveBeenCalledWith("items/1"); }); it("endpoint dynamique avec configurationKey", async () => { const pubId = "getSpecPub2"; PublisherManager.get(pubId).set(staticConfig); const confKey = new DataProviderKey(pubId); const rowEp = new Endpoint<{ id: string }>("rows/${rowId}"); class TestRow extends HTMLElement { rowId = "42"; @get(rowEp, confKey) payload: ApiGetResult<{ id: string }> | null = null; } const tag = "test-api-get-row"; if (!customElements.get(tag)) { customElements.define(tag, TestRow); } const el = document.createElement(tag) as InstanceType; document.body.appendChild(el); await flushGetMicrotasks(); expect(API.prototype.getDetailed).toHaveBeenCalledWith("rows/42"); expect(el.payload?.result).toEqual({ id: "loaded" }); }); it("mode scoped lit la configuration sur les ancĂȘtres (getApiConfiguration)", async () => { const dataEp = new Endpoint<{ id: string }>("scoped-endpoint"); class TestScoped extends HTMLElement { @get(dataEp) payload: ApiGetResult<{ id: string }> | null = null; } const tag = "test-api-get-scoped"; if (!customElements.get(tag)) { customElements.define(tag, TestScoped); } const wrap = document.createElement("div"); wrap.setAttribute("serviceURL", "https://from-ancestor.test"); const el = document.createElement(tag) as InstanceType; wrap.appendChild(el); document.body.appendChild(wrap); await flushGetMicrotasks(); expect(API.prototype.getDetailed).toHaveBeenCalledWith("scoped-endpoint"); expect(el.payload?.result).toEqual({ id: "loaded" }); }); it("relance le GET quand le publisher de config mute (onInternalMutation)", async () => { const pubId = "getSpecPubMut"; const pub = PublisherManager.get(pubId); pub.set(staticConfig); const confKey = new DataProviderKey(pubId); const itemEp = new Endpoint<{ id: string }>("items/1"); const tag = "test-api-get-mutation"; class C extends HTMLElement { @get(itemEp, confKey) payload: ApiGetResult<{ id: string }> | null = null; } if (!customElements.get(tag)) { customElements.define(tag, C); } const el = document.createElement(tag) as InstanceType; document.body.appendChild(el); await flushGetMicrotasks(); expect(API.prototype.getDetailed).toHaveBeenCalledTimes(1); pub.set({ ...staticConfig, serviceURL: "https://api.example.test/v2", }); await flushGetMicrotasks(); expect(API.prototype.getDetailed).toHaveBeenCalledTimes(2); expect(el.payload?.result).toEqual({ id: "loaded" }); }); });