Files
accountkit/test/render.test.js
T
CatoandClaude Opus 5.5 54dc11cf47 feat: one account menu for every product (v0.1.0) (#1)
Each product wrote its own account menu, and each learned half of what
the other had:

  Loki AccountMenu              OrangeCat UserProfileDropdown
  initials from the name only   never rebuilds a name from the email
  custom rows (Appearance)      photo with a fallback, arrow keys
  36px trigger                  44px trigger
  name falls back to the        —
    email local-part

and Loki's public pages had no menu at all, so a signed-in person could
not find sign-out (loki#870 papered over that by reusing the app's menu).

This is the one menu with both halves. It knows nothing about how a
product signs people in — NextAuth, Supabase, better-auth all pass the
person, the places, and a sign-out function — and keeps routing with a
renderLink seam (Next <Link>; a plain <a> by default).

Shared and not configurable, because each shipped broken somewhere:
- the name is never built from the email (OrangeCat's rule; Loki broke it)
- initials only from the name; else a person glyph
- a broken photo falls back to initials
- 44px trigger and rows; the panel never runs off a 390px screen
- Escape closes and returns focus wherever focus was
- sign-out says it is happening, and says when it FAILED (menu stays open)

The real-browser check caught a bug before release: Escape did nothing
after a MOUSE open, because the handler sat on the panel and focus was
on the trigger. It is now a document listener while open.

Styled from @bitbaum/design-tokens through --acct-* variables at zero
specificity (chatkit's pattern), so an app's own :root wins and an OKLCH
app maps its tokens once.

dist/ is committed so `github:bitbaum/accountkit#v0.1.0` installs with no
build and no allowBuilds entry; CI fails if it drifts from the source.

verify: format, lint, types, build, 13 unit+render tests, browser checks
at 390px light and dark. Proved by mutation — letting the name fall back
to the email local-part fails "a name is NEVER reconstructed from the
email address".

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 05:45:04 -07:00

73 lines
2.4 KiB
JavaScript

// The menu renders on the server (Next RSC/SSR) — a component that cannot
// render without a browser breaks every page it is put on.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { createElement as h } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { AccountMenu } from "../dist/index.js";
const noop = () => {};
test("the trigger names the person, announces a menu, and starts closed", () => {
const html = renderToStaticMarkup(
h(AccountMenu, { user: { name: "Cato" }, onSignOut: noop }),
);
assert.match(html, /aria-haspopup="menu"/);
assert.match(html, /aria-expanded="false"/);
assert.match(html, /aria-label="Account: Cato"/);
assert.match(html, />C</, "initials drawn");
assert.ok(!/role="menu"/.test(html), "closed on first render");
});
test("no name and no photo: a person glyph, not a letter from the email", () => {
const html = renderToStaticMarkup(
h(AccountMenu, { user: { email: "butaeff@gmail.com" }, onSignOut: noop }),
);
assert.match(html, /class="acct-glyph"/);
assert.ok(!/>B</.test(html));
});
test("a photo is drawn when there is one", () => {
const html = renderToStaticMarkup(
h(AccountMenu, {
user: { name: "Cato", image: "https://x/a.png" },
onSignOut: noop,
}),
);
assert.match(
html,
/<img[^>]*class="acct-avatar"[^>]*src="https:\/\/x\/a.png"/,
);
});
test("labels are translatable (heidi is not in English)", () => {
const html = renderToStaticMarkup(
h(AccountMenu, {
user: { name: "Cato" },
onSignOut: noop,
labels: { trigger: "Konto: {name}" },
}),
);
assert.match(html, /aria-label="Konto: Cato"/);
});
test("the stylesheet guarantees 44px targets and a panel that fits a phone", () => {
const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
assert.match(css, /--acct-target:\s*2\.75rem/, "44px");
assert.match(
css,
/\.acct-trigger\s*\{[^}]*block-size:\s*var\(--acct-target\)/,
);
assert.match(
css,
/\.acct-row\s*\{[^}]*min-block-size:\s*var\(--acct-target\)/,
);
assert.match(css, /\.acct-panel\s*\{[^}]*inline-size:\s*min\([^)]*100vw/);
});
test("defaults sit at zero specificity so an app's own variables win", () => {
const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
assert.match(css, /:where\(:root\)\s*\{/);
});