import { describe, expect, it } from "vitest"; import { fireEvent, render, screen, waitFor, within, } from "@solidjs/testing-library"; import { MemoryRouter, Route, createMemoryHistory } from "@solidjs/router"; import { AppShell } from "../../components/AppShell"; import { SessionProvider } from "../../lib/session"; import type { Session } from "../../lib/types"; import { anonSession, jsonResponse, signedInSession, stubApi, } from "../support"; function renderShell(session: Session = signedInSession()) { stubApi([{ path: "/api/session", handler: () => jsonResponse(session) }]); const history = createMemoryHistory(); return { history, ...render(() => ( ( {props.children} )} >

Feed content

} />

New steep page

} />
)), }; } async function waitForAccountMenu() { await screen.findByRole("link", { name: "Notifications" }); } function footerLinks() { const footer = screen.getByRole("navigation", { name: "Footer" }); return Array.from(footer.querySelectorAll("a")).map((link) => [ link.textContent, link.getAttribute("href"), link.getAttribute("target"), ]); } describe("AppShell", () => { it("links the info pages and source from the footer", () => { renderShell(); expect(footerLinks()).toEqual([ ["About", "/about", null], ["Terms of Service", "/terms", null], ["Source Code", "https://tangled.org/oolong.social/oolong", "_blank"], ["AT Protocol", "/atproto", null], ]); }); it("offers the create and account dropdowns to a signed-in visitor", async () => { renderShell(signedInSession({ is_moderator: true })); await waitForAccountMenu(); const create = screen.getByLabelText("Create new"); expect(create.tagName).toBe("SUMMARY"); fireEvent.click(create); const createMenu = within(create.closest("details")!); expect(createMenu.getByRole("link", { name: "New steep" })).toHaveAttribute( "href", "/brews/new", ); expect(screen.getByRole("link", { name: "Tea" })).toHaveAttribute( "href", "/teas/new", ); expect(screen.getByRole("link", { name: "Vendor" })).toHaveAttribute( "href", "/vendors/new", ); expect(screen.getByRole("link", { name: "Vessel" })).toHaveAttribute( "href", "/vessels/new", ); expect(screen.getByRole("link", { name: "Infuser" })).toHaveAttribute( "href", "/infusers/new", ); const account = screen.getByLabelText("Account menu"); fireEvent.click(account); expect(screen.getByRole("link", { name: "View profile" })).toHaveAttribute( "href", "/profile/pat.tea", ); expect(screen.getByRole("link", { name: "My tea" })).toHaveAttribute( "href", "/my-tea", ); expect(screen.getByRole("link", { name: "Settings" })).toHaveAttribute( "href", "/settings", ); expect(screen.getByRole("link", { name: "Moderation" })).toHaveAttribute( "href", "/_mod", ); const logout = screen.getByRole("button", { name: "Log out" }); expect(logout.closest("form")).toHaveAttribute("action", "/logout"); expect(logout.closest("form")).toHaveAttribute("method", "post"); }); it("hides the account and create menus from signed-out visitors", async () => { renderShell(anonSession()); const login = screen.getByRole("link", { name: "Log in" }); expect(login.getAttribute("href")).toMatch(/^\/login\?return=/); expect(screen.queryByLabelText("Create new")).toBeNull(); expect(screen.queryByLabelText("Account menu")).toBeNull(); expect(screen.queryByRole("link", { name: "New steep" })).toBeNull(); expect(screen.queryByRole("link", { name: "Notifications" })).toBeNull(); }); it("closes open menus on an outside click or Escape", async () => { renderShell(); await waitForAccountMenu(); const details = screen.getByLabelText("Account menu").closest("details")!; details.open = true; fireEvent.click(document.body); expect(details.open).toBe(false); details.open = true; fireEvent.keyDown(document, { key: "Escape" }); expect(details.open).toBe(false); }); it("closes an open menu when an app link navigates", async () => { renderShell(); await waitForAccountMenu(); const details = screen.getByLabelText("Create new").closest("details")!; details.open = true; fireEvent.click(details.querySelector('a[href="/brews/new"]')!); expect(await screen.findByText("New steep page")).toBeInTheDocument(); await waitFor(() => expect(details.open).toBe(false)); }); });