Something went wrong. Try again.
Tea journaling on ATProto (alpha) forked from arabica.social/arabica
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import { 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(() => ( <MemoryRouter history={history} explicitLinks root={(props) => ( <SessionProvider> <AppShell>{props.children}</AppShell> </SessionProvider> )} > <Route path="/" component={() => <p>Feed content</p>} /> <Route path="/brews/new" component={() => <p>New steep page</p>} /> </MemoryRouter> )), };}
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)); });});