|
| 1 | +import { Theme } from "@radix-ui/themes"; |
| 2 | +import { act, render, screen } from "@testing-library/react"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { AppLoadingScreen } from "./AppLoadingScreen"; |
| 5 | + |
| 6 | +vi.mock("@posthog/ui/shell/openExternal", () => ({ |
| 7 | + openExternalUrl: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +import { openExternalUrl } from "@posthog/ui/shell/openExternal"; |
| 11 | + |
| 12 | +const STALL_TIMEOUT_MS = 30_000; |
| 13 | + |
| 14 | +function renderScreen() { |
| 15 | + return render( |
| 16 | + <Theme> |
| 17 | + <AppLoadingScreen /> |
| 18 | + </Theme>, |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +describe("AppLoadingScreen", () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.useFakeTimers(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.useRealTimers(); |
| 29 | + vi.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("renders the loading logo immediately", () => { |
| 33 | + renderScreen(); |
| 34 | + expect(screen.getByTestId("app-loading-logo")).toBeInTheDocument(); |
| 35 | + expect( |
| 36 | + screen.queryByText("PostHog is taking longer than expected to start"), |
| 37 | + ).not.toBeInTheDocument(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("keeps the logo until just before the stall timeout", () => { |
| 41 | + renderScreen(); |
| 42 | + act(() => { |
| 43 | + vi.advanceTimersByTime(STALL_TIMEOUT_MS - 1); |
| 44 | + }); |
| 45 | + expect(screen.getByTestId("app-loading-logo")).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("swaps to the stalled screen after the timeout", () => { |
| 49 | + renderScreen(); |
| 50 | + act(() => { |
| 51 | + vi.advanceTimersByTime(STALL_TIMEOUT_MS); |
| 52 | + }); |
| 53 | + expect( |
| 54 | + screen.getByText("PostHog is taking longer than expected to start"), |
| 55 | + ).toBeInTheDocument(); |
| 56 | + expect(screen.getByRole("button", { name: "Retry" })).toBeInTheDocument(); |
| 57 | + expect( |
| 58 | + screen.getByRole("button", { name: "Get support" }), |
| 59 | + ).toBeInTheDocument(); |
| 60 | + expect(screen.queryByTestId("app-loading-logo")).not.toBeInTheDocument(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("clears the stall timer on unmount", () => { |
| 64 | + const { unmount } = renderScreen(); |
| 65 | + unmount(); |
| 66 | + expect(() => { |
| 67 | + vi.advanceTimersByTime(STALL_TIMEOUT_MS); |
| 68 | + }).not.toThrow(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("opens the support link from the stalled screen", () => { |
| 72 | + renderScreen(); |
| 73 | + act(() => { |
| 74 | + vi.advanceTimersByTime(STALL_TIMEOUT_MS); |
| 75 | + }); |
| 76 | + screen.getByRole("button", { name: "Get support" }).click(); |
| 77 | + expect(openExternalUrl).toHaveBeenCalledWith( |
| 78 | + expect.stringContaining("discord"), |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
0 commit comments