159 lines
7.3 KiB
TypeScript
159 lines
7.3 KiB
TypeScript
import { afterEach, expect, test, vi } from "vitest";
|
|
import { useState } from "react";
|
|
import { rememberRoom, selectedRoom } from "../src/client/channel/navigation";
|
|
import { useChannel } from "../src/client/channel/use-channel";
|
|
import { reloadFromRoomLayer, useRoomPane } from "../src/client/channel/use-room-pane";
|
|
import { useLayer, useLayerHistory } from "../src/client/ui/layer-history";
|
|
|
|
const { session, cleanups } = vi.hoisted(() => ({
|
|
session: { start: vi.fn(), close: vi.fn(), acceptInvitation: vi.fn(), getSnapshot: () => ({}), subscribe: vi.fn(), refreshReadMarkers: vi.fn() },
|
|
cleanups: [] as Array<() => void>,
|
|
}));
|
|
|
|
// Run the hook's real event handlers without introducing a DOM renderer dependency.
|
|
vi.mock("react", async original => ({
|
|
...await original<typeof import("react")>(),
|
|
useState: vi.fn(() => [session]),
|
|
useRef: (current: unknown) => ({ current }),
|
|
useContext: () => null,
|
|
useCallback: (callback: unknown) => callback,
|
|
useEffectEvent: (callback: unknown) => callback,
|
|
useSyncExternalStore: () => session.getSnapshot(),
|
|
useLayoutEffect: (effect: () => (() => void) | undefined) => {
|
|
const cleanup = effect();
|
|
if (cleanup) cleanups.push(cleanup);
|
|
},
|
|
useEffect: (effect: () => (() => void) | undefined) => {
|
|
const cleanup = effect();
|
|
if (cleanup) cleanups.push(cleanup);
|
|
},
|
|
}));
|
|
|
|
afterEach(() => {
|
|
for (const cleanup of cleanups.splice(0)) cleanup();
|
|
vi.unstubAllGlobals(); vi.clearAllMocks();
|
|
});
|
|
|
|
test("a diagnostic reload clears the overlay marker and keeps the selected room and other page state", () => {
|
|
const page = { piikChannelRoom: "room-1", piikRoomPane: "people", scrollTop: 48 };
|
|
const replaceState = vi.fn(), reload = vi.fn();
|
|
vi.stubGlobal("history", { state: { ...page, piikRoomLayer: true }, replaceState });
|
|
vi.stubGlobal("location", { reload });
|
|
reloadFromRoomLayer("/?debug=1");
|
|
expect(replaceState).toHaveBeenCalledWith(page, "", "/?debug=1");
|
|
expect(replaceState).toHaveBeenCalledBefore(reload);
|
|
});
|
|
|
|
test("Back closes the language menu, Header More and theater in turn above the same pane", async () => {
|
|
const page = { piikRoomPane: "people", piikChannelRoom: "garden" };
|
|
const state = { state: { ...page } };
|
|
const pushState = vi.fn(next => { state.state = next; });
|
|
vi.stubGlobal("history", Object.assign(state, { pushState, back: vi.fn() }));
|
|
const doc = new EventTarget(), view = Object.assign(new EventTarget(), { document: doc });
|
|
vi.stubGlobal("window", view);
|
|
vi.stubGlobal("document", Object.assign(doc, { defaultView: view }));
|
|
const display = vi.fn();
|
|
vi.mocked(useState).mockReturnValueOnce(["people", display]);
|
|
useRoomPane();
|
|
const theater = vi.fn(() => leaveTheater());
|
|
useLayer(true, theater, true);
|
|
const leaveTheater = cleanups.pop()!;
|
|
function menu() {
|
|
let open = true;
|
|
const hidePopover = vi.fn(() => { open = false; });
|
|
useLayerHistory({ current: { localName: "div", ownerDocument: doc, isConnected: true, matches: () => open, hidePopover } as unknown as HTMLElement }, true);
|
|
return hidePopover;
|
|
}
|
|
const header = menu(), language = menu(), hint = vi.fn();
|
|
useLayer(true, hint);
|
|
await Promise.resolve();
|
|
expect(pushState.mock.calls).toEqual([[{ ...page, piikRoomLayer: true }, ""]]);
|
|
function back() {
|
|
state.state = page;
|
|
view.dispatchEvent(new Event("popstate"));
|
|
expect(display).toHaveBeenLastCalledWith("people");
|
|
}
|
|
const dialog = Object.assign(new EventTarget(), {
|
|
localName: "dialog", ownerDocument: doc, isConnected: true, matches: () => true, close: vi.fn(),
|
|
});
|
|
dialog.addEventListener("cancel", event => event.preventDefault(), { once: true });
|
|
useLayerHistory({ current: dialog as unknown as HTMLElement }, true);
|
|
const removeDialog = cleanups.pop()!;
|
|
back();
|
|
expect(dialog.close).not.toHaveBeenCalled();
|
|
expect(language).not.toHaveBeenCalled();
|
|
expect(history.state).toEqual({ ...page, piikRoomLayer: true });
|
|
removeDialog();
|
|
back();
|
|
expect(language).toHaveBeenCalledOnce();
|
|
expect(header).not.toHaveBeenCalled();
|
|
expect(theater).not.toHaveBeenCalled();
|
|
back();
|
|
expect(header).toHaveBeenCalledOnce();
|
|
expect(theater).not.toHaveBeenCalled();
|
|
back();
|
|
expect(theater).toHaveBeenCalledOnce();
|
|
expect(history.state).toEqual(page);
|
|
expect(history.back).not.toHaveBeenCalled();
|
|
expect(hint).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("a persisted page return restarts the retired session and cleanup removes that listener", () => {
|
|
vi.stubGlobal("window", new EventTarget());
|
|
vi.stubGlobal("document", new EventTarget());
|
|
const shown = (persisted: boolean) => Object.assign(new Event("pageshow"), { persisted });
|
|
useChannel();
|
|
expect(session.start).toHaveBeenCalledOnce();
|
|
window.dispatchEvent(shown(false));
|
|
expect(session.start).toHaveBeenCalledOnce();
|
|
window.dispatchEvent(new Event("pagehide"));
|
|
expect(session.close).toHaveBeenCalledOnce();
|
|
window.dispatchEvent(shown(true));
|
|
expect(session.start).toHaveBeenCalledTimes(2);
|
|
for (const cleanup of cleanups.splice(0)) cleanup();
|
|
window.dispatchEvent(shown(true));
|
|
expect(session.start).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
test("hash invitation admission cannot replay an older room after navigation", async () => {
|
|
vi.stubGlobal("window", new EventTarget());
|
|
vi.stubGlobal("document", new EventTarget());
|
|
vi.stubGlobal("location", new URL("https://channel.test/#room=original"));
|
|
let admitFirst!: (room: string) => void;
|
|
const first = new Promise<string>(resolve => { admitFirst = resolve; });
|
|
session.acceptInvitation.mockReturnValueOnce(first).mockResolvedValueOnce("second");
|
|
useChannel();
|
|
location.hash = "room=first&invite=first-key";
|
|
window.dispatchEvent(new Event("hashchange"));
|
|
location.hash = "room=second";
|
|
window.dispatchEvent(new Event("hashchange"));
|
|
expect(session.acceptInvitation).toHaveBeenCalledTimes(2);
|
|
admitFirst("first"); await first;
|
|
expect(new URLSearchParams(location.hash.slice(1)).get("room")).toBe("second");
|
|
});
|
|
|
|
test("a room switch writes an address for the selected room and discards the previous room invite", () => {
|
|
const location = new URL("https://channel.test/?debug=1#room=first&invite=old-key&owner=owner-key&launch=launch-key&piik-client=1");
|
|
const page = { piikChannelRoom: "remembered", piikRoomPane: "chat" };
|
|
const entry = { state: page };
|
|
const replaceState = vi.fn((state, _title, url) => { entry.state = state; location.href = String(url); });
|
|
vi.stubGlobal("location", location);
|
|
vi.stubGlobal("history", Object.assign(entry, { replaceState }));
|
|
expect(selectedRoom()).toBe("first");
|
|
rememberRoom("first");
|
|
expect(new URLSearchParams(location.hash.slice(1)).get("invite")).toBe("old-key");
|
|
rememberRoom("second room");
|
|
expect(location.href).toBe("https://channel.test/?debug=1#room=second+room&owner=owner-key&launch=launch-key&piik-client=1");
|
|
expect(history.state).toEqual({ ...page, piikChannelRoom: "second room" });
|
|
expect(selectedRoom()).toBe("second room");
|
|
rememberRoom("");
|
|
expect(location.href).toBe("https://channel.test/?debug=1#owner=owner-key&launch=launch-key&piik-client=1");
|
|
expect(selectedRoom()).toBe("");
|
|
});
|
|
|
|
test("a roomless address can restore an older history entry's room", () => {
|
|
vi.stubGlobal("location", new URL("https://channel.test/"));
|
|
vi.stubGlobal("history", { state: { piikChannelRoom: "remembered" } });
|
|
expect(selectedRoom()).toBe("remembered");
|
|
});
|