Files
bip-kit/test/share-react.test.js
CatoandClaude cf92d01fdc feat(share): ShareRow is a grid of equal icon tiles, every target in view (#69)
On a 390px phone the sideways strip of text pills showed two and a half
choices, cut off at both edges; X, Bluesky, Hacker News, WhatsApp, Telegram
and Email were found only by scrolling. ShareRow now lays out every target
as one tile shape (icon over a short name, 44px+, equal height per row,
four to a row on a phone), the cross-post first and still the one primary.

- `text` on ShareRow/shareLinks: the words beside the link on X, Bluesky,
  WhatsApp, Telegram and the device sheet (defaults to title)
- ShareLink.name + defaultShareLinkNames; `linkNames` / `names` to translate
- crossPost.icon overrides the cross-post glyph
- 0.10.0, changelog and README updated


Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-15 02:04:42 +01:00

124 lines
5.1 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createElement as h } from "react";
import { renderToString } from "react-dom/server";
import { ShareRow } from "../dist/react/index.js";
import { crossPostUrl } from "../dist/index.js";
const post = { url: "https://solon.example/blog/t", title: "A & B", summary: "S." };
test("ShareRow renders server-side with no navigator, cross-post first", () => {
assert.equal(typeof globalThis.navigator?.share, "undefined");
const href = crossPostUrl("https://oc.example/articles/new", `${post.url}/article.json`);
const html = renderToString(
h(ShareRow, { ...post, crossPost: { href, label: "Post on OrangeCat" } }),
);
assert.match(html, /<nav[^>]*aria-label="Share this post"/);
const first = html.indexOf("Post on OrangeCat");
assert.ok(first > 0);
for (const later of ["Copy link", 'aria-label="Share on X"', 'aria-label="Email it"']) {
assert.ok(html.indexOf(later) > first, `${later} comes after the cross-post`);
}
assert.equal((html.match(/bp-share-primary/g) ?? []).length, 1, "one primary action");
assert.doesNotMatch(html, /Share…/, "the share sheet button waits for the browser to offer one");
});
test("ShareRow links open in a new tab safely, mail stays in place, exclude drops ids", () => {
const html = renderToString(h(ShareRow, { ...post, exclude: ["hackernews"] }));
assert.doesNotMatch(html, /Hacker News/);
assert.doesNotMatch(html, /bp-share-primary/, "no cross-post, no primary");
const anchors = html.match(/<a [^>]*>/g);
assert.equal(anchors.length, 6);
for (const a of anchors) assert.match(a, /aria-label="(Share|Send|Email|Submit)[^"]*"/);
for (const a of anchors) {
if (a.includes("mailto:")) assert.doesNotMatch(a, /target=/);
else
assert.match(
a,
/target="_blank"[^>]*rel="noopener noreferrer"|rel="noopener noreferrer"[^>]*target="_blank"/,
);
}
});
test("ShareRow takes the app's words", () => {
const html = renderToString(
h(ShareRow, { ...post, labels: { landmark: "Teilen", copy: "Link kopieren" } }),
);
assert.match(html, /aria-label="Teilen"/);
assert.match(html, /Link kopieren/);
});
test("ShareRow cross-post can act in place: a button, not a link", () => {
const html = renderToString(
h(ShareRow, { ...post, crossPost: { label: "Repost on OrangeCat", onClick: () => {} } }),
);
assert.match(
html,
/<button[^>]*bp-share-primary[^>]*>.*?<span class="bp-share-label">Repost on OrangeCat<\/span><\/button>/,
);
assert.doesNotMatch(html, /<a[^>]*bp-share-primary/);
});
test("ShareRow cross-post link can stay in the same tab", () => {
const html = renderToString(
h(ShareRow, { ...post, crossPost: { label: "Post here", href: "/share", newTab: false } }),
);
const primary = html.match(/<a[^>]*bp-share-primary[^>]*>/)[0];
assert.doesNotMatch(primary, /target=/);
});
test("ShareRow for restricted content offers only the cross-post and Copy link, and says why", () => {
const html = renderToString(
h(ShareRow, {
...post,
audience: "restricted",
crossPost: { label: "Repost on OrangeCat", onClick: () => {} },
}),
);
assert.match(html, /Copy link/);
assert.match(html, /Repost on OrangeCat/);
assert.doesNotMatch(html, /data-share=/);
assert.match(html, /Only people who can already see this can open the link\./);
});
test("ShareRow tiles: an icon over the service's short name, the verb as its accessible name", () => {
const html = renderToString(h(ShareRow, post));
const x = html.match(/<a [^>]*data-share="x"[^>]*>.*?<\/a>/)[0];
assert.match(x, /aria-label="Share on X"/);
assert.match(x, /<span class="bp-share-icon" aria-hidden="true"><svg/);
assert.match(x, /<span class="bp-share-label">X<\/span>/);
for (const name of ["LinkedIn", "Bluesky", "Hacker News", "WhatsApp", "Telegram", "Email"]) {
assert.match(html, new RegExp(`<span class="bp-share-label">${name}</span>`));
}
assert.equal((html.match(/<svg/g) ?? []).length, 8, "every tile has its glyph");
assert.match(html, /Copy link/);
});
test("ShareRow carries the app's words to X, Bluesky, WhatsApp and Telegram, the title elsewhere", () => {
const html = renderToString(
h(ShareRow, { ...post, text: "A & B: the short line", linkNames: { email: "E-Mail" } }),
);
const href = (id) =>
new URL(
html.match(new RegExp(`href="([^"]*)"[^>]*data-share="${id}"`))[1].replace(/&amp;/g, "&"),
);
assert.equal(href("x").searchParams.get("text"), "A & B: the short line");
assert.equal(href("telegram").searchParams.get("text"), "A & B: the short line");
assert.match(href("bluesky").searchParams.get("text"), /^A & B: the short line https:/);
assert.equal(href("hackernews").searchParams.get("t"), "A & B");
assert.match(html, /E-Mail/);
});
test("ShareRow cross-post takes the app's glyph", () => {
const html = renderToString(
h(ShareRow, {
...post,
crossPost: { label: "Post here", href: "/share", icon: h("svg", { "data-mine": "1" }) },
}),
);
assert.match(
html,
/bp-share-primary[^>]*><span class="bp-share-icon" aria-hidden="true"><svg data-mine="1"/,
);
});