Files
kits/packages/sitekit/test/plan-table.test.js
CatoandClaude Fable 5.2 008af96425 feat(paykit 0.5.0, sitekit 0.9.0): plans with prices and a waitlist, before a product can charge (#21)
* feat(paykit 0.5.0, sitekit 0.9.0): plans with prices and a waitlist, before a product can charge

paykit/plans: Tier (open, waitlist, checkout, contact), formatMoney,
readWaitlistEntry, WaitlistStore and joinWaitlist: what a product
offers, how a price is written, and the whole of a waitlist route.

sitekit: PlanTable and WaitlistForm: plan cards with one action each;
a waitlist action opens a one-field form in the card. A tier goes on
sale by turning that action into a link to the checkout.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz

* fix(sitekit): the waitlist form's note is an alert only when something went wrong

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-30 20:12:30 +01:00

123 lines
4.6 KiB
JavaScript

/**
* The plan cards: every tier has the same parts in the same place, and what a
* visitor can do with each one is real markup (a link, a button, a form).
*/
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 { PlanTable, WaitlistForm, DEFAULT_PLAN_TABLE_LABELS } from '@bitbaum/sitekit/react';
const plans = [
{
id: 'free',
name: 'Free',
tagline: 'For anyone.',
price: 'CHF 0',
features: ['Read every act', 'Ask with free models'],
action: { kind: 'link', href: '/law', label: 'Start reading' },
},
{
id: 'plus',
name: 'Plus',
tagline: 'For one person.',
price: 'CHF 12',
per: 'per month',
badge: 'Planned',
features: ['A stronger model answers'],
note: 'Not on sale yet.',
action: { kind: 'waitlist', label: 'Join the waitlist' },
},
{
id: 'pro',
name: 'Pro',
tagline: 'For a practice.',
price: 'CHF 49',
per: 'per month',
features: ['API access'],
action: { kind: 'current', label: 'Your plan' },
},
];
const table = (props = {}) =>
renderToStaticMarkup(h(PlanTable, { plans, onJoin: async () => 'added', ...props }));
test('every plan is a card with its name, who it is for, price and contents', () => {
const html = table({ label: 'Plans' });
assert.match(html, /<ul class="sk-plans" aria-label="Plans"/);
assert.equal(html.match(/<li[^>]*class="sk-plan"/g).length, 3);
for (const plan of plans) {
assert.match(html, new RegExp(`<h3 id="sk-plan-${plan.id}"[^>]*>${plan.name}</h3>`));
assert.ok(html.includes(plan.tagline));
assert.ok(html.includes(`<span class="sk-plan-amount">${plan.price}</span>`));
for (const feature of plan.features) assert.ok(html.includes(`<li>${feature}</li>`));
}
});
test('what the price is per, the badge and the note appear only where given', () => {
const html = table();
assert.equal(html.match(/class="sk-plan-per"/g).length, 2);
assert.equal(html.match(/class="sk-plan-badge"/g).length, 1);
assert.match(html, /<p class="sk-plan-badge">Planned<\/p>/);
assert.match(html, /<p class="sk-plan-note">Not on sale yet\.<\/p>/);
});
test('each kind of action is the element that does it', () => {
const html = table();
assert.match(html, /<a class="sk-plan-button" href="\/law">Start reading<\/a>/);
assert.match(html, /<button type="button" class="sk-plan-button">Join the waitlist<\/button>/);
assert.match(html, /<p class="sk-plan-current">Your plan<\/p>/);
// No form until a visitor asks for one.
assert.doesNotMatch(html, /<form/);
});
test('a waitlist with nobody to hand the address to cannot be pressed', () => {
assert.match(
table({ onJoin: undefined }),
/<button type="button" class="sk-plan-button" disabled="">Join the waitlist<\/button>/,
);
});
test('the form is one labelled email field that says what the address is for', () => {
const html = renderToStaticMarkup(
h(WaitlistForm, { tier: 'plus', onJoin: async () => 'added', defaultEmail: 'ada@example.ch' }),
);
const id = html.match(/<label for="([^"]+)"/)[1];
assert.match(html, new RegExp(`<input id="${id}"[^>]*type="email"`));
assert.match(html, /autoComplete="email"|autocomplete="email"/);
assert.match(html, /value="ada@example\.ch"/);
assert.ok(html.includes(DEFAULT_PLAN_TABLE_LABELS.privacy));
assert.match(html, new RegExp(`aria-describedby="${id}-note"`));
assert.match(
html,
/<button type="submit" class="sk-plan-button">Tell me when it opens<\/button>/,
);
});
test("the form speaks the product's language when given it", () => {
const html = renderToStaticMarkup(
h(WaitlistForm, {
tier: 'plus',
onJoin: async () => 'added',
labels: { email: 'E-Mail', join: 'Benachrichtigen', privacy: 'Nur dafür verwendet.' },
}),
);
assert.match(html, />E-Mail<\/label>/);
assert.match(html, />Benachrichtigen<\/button>/);
assert.ok(html.includes('Nur dafür verwendet.'));
});
test('the styles keep a card inside a phone, a target tappable and a field unzoomed', () => {
const css = readFileSync(new URL('../styles.css', import.meta.url), 'utf8');
const rule = (selector) => {
const at = css.indexOf(`\n${selector} {`);
assert.ok(at >= 0, selector);
return css.slice(at, css.indexOf('}', at));
};
assert.match(rule('.sk-plans'), /minmax\(min\(100%, 15rem\), 1fr\)/);
assert.match(rule('.sk-plan'), /min-width: 0/);
assert.match(rule('.sk-plan-button'), /min-height: 2\.75rem/);
assert.match(rule('.sk-plan-input'), /font-size: 1rem/);
assert.match(rule('.sk-plan-input'), /min-height: 2\.75rem/);
});