mirror of
https://github.com/bitbaum/accountkit.git
synced 2026-12-03 22:17:12 +01:00
An AccountMenu item or group may carry shown: "always" | "narrow" (default "always"), and extra has the same choice as extraShown. A narrow row is in the menu below 768px only: hidden by the package stylesheet in a media query (no width read in script, right on the server render and the first paint), skipped by the arrow keys, and a hidden group takes its divider with it. For a row that repeats a control the header carries from md up, such as OrangeCat's theme toggle, which its menu also showed as "Appearance". Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn Co-authored-by: Claude <noreply@anthropic.com>
224 lines
7.6 KiB
JavaScript
224 lines
7.6 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, groupShown } 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*\{/);
|
|
});
|
|
|
|
// The sign-in error screen. loki, skif and substrata showed Auth.js's bare
|
|
// "Error"; heidi's "Try again" went home. Try again must RESTART sign-in.
|
|
test("SignInError: Try again posts to the retry action, not home", async () => {
|
|
const { SignInError } = await import("../dist/index.js");
|
|
const html = renderToStaticMarkup(
|
|
h(SignInError, {
|
|
error: "OAuthCallbackError",
|
|
retry: "/sign-in?from=%2Fsettings",
|
|
home: "/",
|
|
}),
|
|
);
|
|
assert.match(
|
|
html,
|
|
/<a class="acct-signin-button [^"]*" href="\/sign-in\?from=%2Fsettings">Try again</,
|
|
);
|
|
assert.match(html, /Sign-in did not finish/);
|
|
assert.ok(
|
|
!/OAuthCallbackError/.test(html),
|
|
"the provider's code is never shown",
|
|
);
|
|
});
|
|
|
|
test("SignInError: a server-action retry renders a form with its fields", async () => {
|
|
const { SignInError } = await import("../dist/index.js");
|
|
const html = renderToStaticMarkup(
|
|
h(SignInError, { retry: async () => {}, retryFields: { from: "/a" } }),
|
|
);
|
|
assert.match(html, /<form/);
|
|
assert.match(html, /<input type="hidden" name="from" value="\/a"\/>/);
|
|
assert.match(
|
|
html,
|
|
/<button type="submit" class="acct-signin-button[^"]*">Try again</,
|
|
);
|
|
});
|
|
|
|
test("SignInError: no retry when sign-in is not configured; denial says so", async () => {
|
|
const { SignInError, signInErrorKind } = await import("../dist/index.js");
|
|
const config = renderToStaticMarkup(
|
|
h(SignInError, { error: "Configuration", retry: "/sign-in" }),
|
|
);
|
|
assert.ok(!/Try again/.test(config));
|
|
assert.equal(signInErrorKind("AccessDenied"), "denied");
|
|
assert.equal(signInErrorKind("Verification"), "failed");
|
|
assert.equal(signInErrorKind(undefined), "failed");
|
|
});
|
|
|
|
test("other accounts change nothing until there are some and a way to switch", () => {
|
|
// The panel is closed on the server, so what can be pinned here is that
|
|
// the new props are accepted and the trigger is the same either way.
|
|
const plain = renderToStaticMarkup(
|
|
h(AccountMenu, { user: { name: "Cato" }, onSignOut: noop }),
|
|
);
|
|
const withAccounts = renderToStaticMarkup(
|
|
h(AccountMenu, {
|
|
user: { name: "Cato" },
|
|
onSignOut: noop,
|
|
accounts: [{ id: "a", user: { name: "Alberto Caeiro" } }],
|
|
onSwitchAccount: noop,
|
|
}),
|
|
);
|
|
assert.equal(withAccounts, plain);
|
|
});
|
|
|
|
test("the stylesheet keeps an account row to one line each for name and handle", () => {
|
|
const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
|
|
assert.match(
|
|
css,
|
|
/\.acct-account-name,\s*\.acct-account-line\s*\{[^}]*white-space:\s*nowrap/,
|
|
);
|
|
assert.match(css, /\.acct-account-text\s*\{[^}]*min-inline-size:\s*0/);
|
|
});
|
|
|
|
test("something waiting in another account is said on the trigger, before it is opened", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(AccountMenu, {
|
|
user: { name: "Cato" },
|
|
onSignOut: noop,
|
|
accounts: [
|
|
{ id: "a", user: { name: "Alberto Caeiro" }, waiting: 2 },
|
|
{ id: "b", user: { name: "Ricardo Reis" } },
|
|
],
|
|
onSwitchAccount: noop,
|
|
}),
|
|
);
|
|
assert.match(html, /class="acct-trigger-dot"/);
|
|
assert.match(
|
|
html,
|
|
/aria-label="Account: Cato, something is waiting in another account"/,
|
|
);
|
|
});
|
|
|
|
test("nothing waiting, or a count that is not one, shows no dot", () => {
|
|
for (const waiting of [undefined, 0, -3, Number.NaN, 0.4]) {
|
|
const html = renderToStaticMarkup(
|
|
h(AccountMenu, {
|
|
user: { name: "Cato" },
|
|
onSignOut: noop,
|
|
accounts: [{ id: "a", user: { name: "Alberto Caeiro" }, waiting }],
|
|
onSwitchAccount: noop,
|
|
}),
|
|
);
|
|
assert.ok(!/acct-trigger-dot/.test(html), `no dot for ${waiting}`);
|
|
assert.match(html, /aria-label="Account: Cato"/);
|
|
}
|
|
});
|
|
|
|
test("a count without a way to switch shows nothing: there is nowhere to go", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(AccountMenu, {
|
|
user: { name: "Cato" },
|
|
onSignOut: noop,
|
|
accounts: [{ id: "a", user: { name: "Alberto Caeiro" }, waiting: 5 }],
|
|
}),
|
|
);
|
|
assert.ok(!/acct-trigger-dot/.test(html));
|
|
});
|
|
|
|
test("shown: a group goes wide only when it says so or every item does", () => {
|
|
const link = { id: "a", label: "A", href: "/a" };
|
|
const narrow = {
|
|
id: "t",
|
|
label: "Appearance",
|
|
onSelect: noop,
|
|
shown: "narrow",
|
|
};
|
|
assert.equal(groupShown({ id: "g", items: [link] }), "always");
|
|
assert.equal(groupShown({ id: "g", items: [narrow] }), "narrow");
|
|
assert.equal(groupShown({ id: "g", items: [link, narrow] }), "always");
|
|
assert.equal(
|
|
groupShown({ id: "g", items: [link], shown: "narrow" }),
|
|
"narrow",
|
|
);
|
|
assert.equal(
|
|
groupShown({ id: "g", items: [{ ...narrow, shown: "always" }] }),
|
|
"always",
|
|
);
|
|
assert.equal(groupShown({ id: "g", items: [] }), "always");
|
|
});
|
|
|
|
test("the stylesheet hides narrow rows from 768px up, in a media query", () => {
|
|
const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
|
|
const wide = css.match(/@media \(min-width: 768px\) \{([\s\S]*?)\n\}/);
|
|
assert.ok(wide, "a 768px media query");
|
|
assert.match(wide[1], /\.acct-panel \.acct-narrow\s*\{\s*display:\s*none/);
|
|
assert.match(
|
|
wide[1],
|
|
/\.acct-group:not\(\.acct-group:not\(\.acct-narrow\) ~ \*\)\s*\{\s*border-block-start:\s*0/,
|
|
"a hidden first group takes its divider with it",
|
|
);
|
|
});
|