57 lines
3.1 KiB
TypeScript
57 lines
3.1 KiB
TypeScript
import { act, createElement, type ComponentProps } from "react";
|
|
import { createRoot } from "react-dom/client";
|
|
import { expect, test, vi } from "vitest";
|
|
import { DiagnosticsButton } from "../src/client/components/living/Header";
|
|
import { Btn } from "../src/client/components/living/primitives";
|
|
import * as files from "../src/client/lib/download";
|
|
import { say } from "../src/client/ui/copy";
|
|
import { LONG_WAIT_MS } from "../src/client/ui/long-wait";
|
|
|
|
const controls = vi.hoisted(() => ({ button: vi.fn(), notice: vi.fn() }));
|
|
vi.mock("../src/client/components/living/primitives", () => ({
|
|
Btn: (props: unknown) => { controls.button(props); return null; },
|
|
Pill: (props: unknown) => { controls.notice(props); return null; },
|
|
}));
|
|
vi.mock("../src/client/components/living/Tooltip", () => ({
|
|
Tooltip: ({ children }: { children: unknown }) => children,
|
|
}));
|
|
|
|
test("keeps the page download result when the App reports success after the user switches exports", async () => {
|
|
const view = Object.assign(new EventTarget(), { HTMLIFrameElement: class {} });
|
|
const ownerDocument = Object.assign(new EventTarget(), { nodeType: 9, defaultView: view });
|
|
vi.stubGlobal("window", Object.assign(view, { document: ownerDocument }));
|
|
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
|
const root = createRoot(Object.assign(new EventTarget(), { nodeType: 1, tagName: "DIV", ownerDocument }) as unknown as HTMLElement);
|
|
let completeApp!: (response: Response) => void;
|
|
const fetch = vi.fn((_url: string, _request: RequestInit) => new Promise<Response>(resolve => { completeApp = resolve; }));
|
|
vi.stubGlobal("fetch", fetch);
|
|
const download = vi.spyOn(files, "downloadBlob").mockImplementation((_blob, _name, options) => options?.outcome?.("dispatched"));
|
|
const button = () => controls.button.mock.lastCall![0] as ComponentProps<typeof Btn>;
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
|
|
try {
|
|
await act(() => root.render(createElement<NonNullable<ComponentProps<typeof DiagnosticsButton>>>(DiagnosticsButton, { appPort: 39721 })));
|
|
await act(() => { button().onClick?.({} as never); });
|
|
expect(button().disabled).toBe(true);
|
|
expect(fetch).toHaveBeenCalledOnce();
|
|
const request = fetch.mock.calls[0][1];
|
|
await act(() => { vi.advanceTimersByTime(LONG_WAIT_MS); });
|
|
expect(button().cap).toBe("debug.pageOnly");
|
|
expect(request.signal?.aborted).toBe(false);
|
|
await act(() => { button().onClick?.({} as never); });
|
|
expect(request.signal?.aborted).toBe(true);
|
|
expect(download).toHaveBeenCalledOnce();
|
|
const report = JSON.parse(await download.mock.calls[0][0].text());
|
|
expect(Object.entries(report)[0]).toEqual(["app", "missing"]);
|
|
expect(controls.notice).toHaveBeenCalledWith(expect.objectContaining({ label: say("channel.attachment.downloadSent") }));
|
|
controls.notice.mockClear();
|
|
await act(() => completeApp(new Response(null, { status: 204 })));
|
|
expect(controls.notice).not.toHaveBeenCalled();
|
|
expect(download).toHaveBeenCalledOnce();
|
|
} finally {
|
|
await act(() => root.unmount());
|
|
download.mockRestore();
|
|
vi.useRealTimers();
|
|
vi.unstubAllGlobals();
|
|
}
|
|
});
|