SPB Git

Repository Public

react.test.ts 5.3 KB 121 lines typescript

1// @vitest-environment jsdom2/**3 * =============================================================================4 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION5 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING6 * =============================================================================7 *8 * Project          : MacLustr Identity9 * File             : sdk/typescript/tests/unit/react.test.ts10 * Module           : sdk11 * Repository       : https://git.spboucher.ai/maclustr-identity12 * Project Website  : https://identity.spboucher.ai13 * Created          : 2026-12-0114 * Last Modified    : 2026-12-0115 *16 * Author           : Simon-Pierre Boucher17 * Position         : Professor of Finance18 * Institution      : Université du Québec en Outaouais (UQO)19 * Organization     : SP Boucher20 * Website          : https://spboucher.ai21 * Git              : https://git.spboucher.ai22 *23 * Preferred Contact: admin@spboucher.ai24 * Technical Contact: dev@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { createElement } from "react";44import { createRoot, type Root } from "react-dom/client";45import { act } from "react";46import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";47import { VerifyButton, useVerification, type UseVerification } from "../../src/react";4849(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;5051const ORIGIN = "https://identity.spboucher.ai";52const URL_OK = `${ORIGIN}/verify/vs_42#vsc_x`;53let root: Root;54let host: HTMLElement;5556beforeEach(() => {57  host = document.createElement("div");58  document.body.appendChild(host);59  root = createRoot(host);60});61afterEach(() => {62  act(() => root.unmount());63  document.body.innerHTML = "";64});6566const flush = () => act(async () => void (await Promise.resolve()));6768describe("<VerifyButton>", () => {69  it("creates the session through the app backend, opens the modal and reports completion", async () => {70    const createSession = vi.fn(async () => ({ url: URL_OK }));71    const onComplete = vi.fn();72    act(() => root.render(createElement(VerifyButton, { createSession, onComplete, className: "cta" }, "Verify me")));73    const button = host.querySelector("button")!;74    expect(button.textContent).toBe("Verify me");75    expect(button.className).toBe("cta");76    await act(async () => button.click());77    await flush();78    expect(createSession).toHaveBeenCalledTimes(1);79    const frame = document.querySelector("iframe")!;80    expect(frame.getAttribute("src")).toBe(URL_OK);81    expect(frame.getAttribute("allow")).toBe("camera");82    expect(button.getAttribute("data-state")).toBe("open");83    expect(button.getAttribute("aria-disabled")).toBe("true");84    expect(button.disabled).toBe(false); // stays focusable so focus can return to it85    await act(async () => button.click());86    expect(createSession).toHaveBeenCalledTimes(1); // extra clicks are ignored while open87    act(() => void window.dispatchEvent(new MessageEvent("message", { origin: "https://evil.example", data: { source: "maclustr-identity", type: "completed", status: "verified" } })));88    expect(onComplete).not.toHaveBeenCalled();89    act(() => void window.dispatchEvent(new MessageEvent("message", { origin: ORIGIN, data: { source: "maclustr-identity", type: "completed", session_id: "vs_42", status: "verified" } })));90    expect(onComplete).toHaveBeenCalledWith({ sessionId: "vs_42", status: "verified" });91    expect(document.querySelector("iframe")).toBeNull();92    expect(button.getAttribute("data-state")).toBe("completed");93    expect(button.hasAttribute("aria-disabled")).toBe(false);94  });95});9697describe("useVerification", () => {98  it("exposes an error state when the backend cannot create a session, and closes the dialog on unmount", async () => {99    let api!: UseVerification;100    const onError = vi.fn();101    let fail = true;102    const Probe = () => {103      api = useVerification({ createSession: async () => (fail ? Promise.reject(new Error("identity service unreachable")) : URL_OK), onError });104      return null;105    };106    act(() => root.render(createElement(Probe)));107    expect(api.state).toBe("idle");108    await act(async () => api.open());109    expect(api.state).toBe("error");110    expect(api.error).toMatchObject({ code: "session_creation_failed", message: "identity service unreachable" });111    expect(onError).toHaveBeenCalledTimes(1);112    expect(document.querySelector("iframe")).toBeNull();113    fail = false;114    await act(async () => api.open());115    expect(api.state).toBe("open");116    expect(document.querySelector("iframe")).not.toBeNull();117    act(() => root.render(createElement("div")));118    expect(document.querySelector("iframe")).toBeNull();119  });120});121
Files main
Menu

Theme