// @vitest-environment jsdom // // The app says what its entry URL tells the account signed in about a switch of account, once, and // only once that account is known. Without the hook in the shell, a refused switch or a tab that // reloaded as another account comes back without a word. import { afterEach, describe, expect, it, vi } from "vitest"; import { renderHook } from "@testing-library/react"; vi.mock("react-i18next", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, useTranslation: () => ({ t: (key: string, values?: Record) => values ? `${key} ${JSON.stringify(values)}` : key, i18n: { language: "en" }, }), }; }); const notifyMock = vi.fn(); vi.mock("./notifications", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, notify: (...a: unknown[]) => notifyMock(...a) }; }); const notice = vi.fn(); vi.mock("./entryUrl", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, accountSwitchNotice: () => notice() }; }); const { useAccountSwitchNotice } = await import("./useAccountSwitchNotice"); afterEach(() => { notifyMock.mockClear(); notice.mockReset(); }); describe("the shell's word on a switch of account", () => { it("waits for the account, then says it once", () => { notice.mockReturnValue({ level: "info", key: "header.account.switchRequested", values: { email: "b@example.com" }, }); const { rerender } = renderHook(({ signedIn }) => useAccountSwitchNotice(signedIn), { initialProps: { signedIn: false }, }); expect(notifyMock).not.toHaveBeenCalled(); rerender({ signedIn: true }); rerender({ signedIn: false }); rerender({ signedIn: true }); expect(notifyMock).toHaveBeenCalledTimes(1); expect(notifyMock).toHaveBeenCalledWith({ level: "info", message: 'header.account.switchRequested {"email":"b@example.com"}', }); }); it("says nothing when the entry URL has nothing to say", () => { notice.mockReturnValue(null); renderHook(() => useAccountSwitchNotice(true)); expect(notifyMock).not.toHaveBeenCalled(); }); });