|
| 1 | +import { ServiceProvider } from "@posthog/di/react"; |
| 2 | +import { Theme } from "@radix-ui/themes"; |
| 3 | +import { render, screen } from "@testing-library/react"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | +import { Container } from "inversify"; |
| 6 | +import { useEffect } from "react"; |
| 7 | +import { describe, expect, it, vi } from "vitest"; |
| 8 | +import { BrowserPanel } from "./BrowserPanel"; |
| 9 | +import { |
| 10 | + BROWSER_VIEW_COMPONENT, |
| 11 | + type BrowserViewHandle, |
| 12 | + type BrowserViewProps, |
| 13 | +} from "./identifiers"; |
| 14 | + |
| 15 | +const loadURL = vi.fn<(url: string) => Promise<void>>(); |
| 16 | + |
| 17 | +const browserViewHandle: BrowserViewHandle = { |
| 18 | + loadURL, |
| 19 | + reload: vi.fn(), |
| 20 | + stop: vi.fn(), |
| 21 | + goBack: vi.fn(), |
| 22 | + goForward: vi.fn(), |
| 23 | +}; |
| 24 | + |
| 25 | +function FakeBrowserView({ onReady }: BrowserViewProps) { |
| 26 | + useEffect(() => { |
| 27 | + onReady(browserViewHandle); |
| 28 | + return () => onReady(null); |
| 29 | + }, [onReady]); |
| 30 | + return <div data-testid="browser-view" />; |
| 31 | +} |
| 32 | + |
| 33 | +describe("BrowserPanel", () => { |
| 34 | + it("navigates when Enter is pressed in the address input", async () => { |
| 35 | + loadURL.mockResolvedValue(); |
| 36 | + const container = new Container(); |
| 37 | + container.bind(BROWSER_VIEW_COMPONENT).toConstantValue(FakeBrowserView); |
| 38 | + const user = userEvent.setup(); |
| 39 | + |
| 40 | + render( |
| 41 | + <ServiceProvider container={container}> |
| 42 | + <Theme> |
| 43 | + <BrowserPanel url="about:blank" /> |
| 44 | + </Theme> |
| 45 | + </ServiceProvider>, |
| 46 | + ); |
| 47 | + |
| 48 | + await user.type( |
| 49 | + screen.getByRole("textbox", { name: "Address" }), |
| 50 | + "posthog.com{Enter}", |
| 51 | + ); |
| 52 | + |
| 53 | + expect(loadURL).toHaveBeenCalledWith("https://posthog.com"); |
| 54 | + }); |
| 55 | +}); |
0 commit comments