Files
CatoandClaude Sonnet 5.5 f5e0a24208 fix: the date box matches a native input exactly; structure survives an app's display:block (0.3.2) (#7)
- line-height: normal (the font's own, like a native input) instead of 1.3,
  and an icon that fits a normal line: the box is now exactly a native
  input's height (38 vs 38; it was 40 vs 38, a visible step in form rows).
- structure rules on span.wk-input so an app's own unlayered class with
  display:block on the same element cannot collapse the stacked layout.
- codemod: an object spread inside onChange is no longer flagged as a JSX
  prop spread.

Both browser checks were first run failing against the old CSS.

Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-08 06:47:10 -07:00

201 lines
8.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The standard, checked in a real browser — the part no unit test can see.
//
// pnpm run demo && node test/browser.mjs
//
// - every chip and day is at least 44px (a thumb has to land on it)
// - the date field's real input is 16px (below it iOS zooms on focus)
// - no horizontal page scroll at 390px — the day strip scrolls inside itself
// - a booking can be made in taps alone, and resolves to real instants
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { extname, join, normalize } from "node:path";
import { fileURLToPath } from "node:url";
import { chromium } from "playwright";
const root = fileURLToPath(new URL("..", import.meta.url));
const types = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".woff2": "font/woff2",
};
const server = createServer(async (req, res) => {
const path = normalize(join(root, decodeURIComponent(new URL(req.url, "http://x").pathname)));
if (!path.startsWith(root)) return res.writeHead(403).end();
try {
const body = await readFile(path);
res
.writeHead(200, { "content-type": types[extname(path)] ?? "application/octet-stream" })
.end(body);
} catch {
res.writeHead(404).end();
}
}).listen(0);
const base = `http://localhost:${server.address().port}/demo/index.html`;
let failures = 0;
const check = (ok, what) => {
console.log(`${ok ? "PASS" : "FAIL"} ${what}`);
if (!ok) failures++;
};
const browser = await chromium.launch();
try {
for (const [name, width, height, dark] of [
["desktop light", 1280, 800, false],
["phone light", 390, 844, false],
["phone dark", 390, 844, true],
]) {
const page = await browser.newPage({ viewport: { width, height } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.goto(base);
if (dark) await page.evaluate(() => document.documentElement.classList.add("dark"));
await page.waitForSelector(".wk-day");
check(
await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth),
`${name}: no horizontal page scroll`,
);
// Tap through an hourly booking: tomorrow, first start time, 2 hours.
const hourly = page.locator("#hourly");
await hourly.locator(".wk-day").nth(1).click();
await hourly.getByRole("radiogroup", { name: "Start" }).getByRole("radio").first().click();
await hourly
.getByRole("radiogroup", { name: "For how long" })
.getByRole("radio")
.nth(1)
.click();
const summary = (await hourly.locator(".wk-summary").innerText()).trim();
check(
/Tomorrow|\d/.test(summary) && /–/.test(summary),
`${name}: hourly summary reads "${summary}"`,
);
const resolved = JSON.parse((await page.locator("#hourly-resolved").innerText()) || "null");
check(
resolved && new Date(resolved.endsAt) - new Date(resolved.startsAt) === 2 * 3600_000,
`${name}: hourly resolves to a 2-hour window`,
);
await page.waitForTimeout(300);
await hourly.screenshot({
path: join(root, "demo", "dist", `${name.replace(/ /g, "-")}-hourly.png`),
});
// Tap a range on the daily picker.
const daily = page.locator("#daily");
await daily.locator(".wk-day").nth(2).click();
await daily.locator(".wk-day").nth(4).click();
const dailySummary = (await daily.locator(".wk-summary").innerText()).trim();
check(/3 days/.test(dailySummary), `${name}: daily range reads "${dailySummary}"`);
// Flexible needs no day at all.
await hourly.getByRole("radio", { name: "I'm flexible" }).click();
check(
/Flexible/.test(await hourly.locator(".wk-summary").innerText()),
`${name}: flexible request without a day`,
);
// The app's own classes win over whenkit's defaults — plain CSS and a
// layered (Tailwind-style) utility alike. Without @layer base for the
// defaults, whenkit's unlayered rules would beat the layered utility.
const own = await page.evaluate(() => {
const css = (sel) => getComputedStyle(document.querySelector(sel));
return {
plainBorder: css("#own-class .wk-input").borderTopColor,
plainWidth: css("#own-class .wk-input").borderTopWidth,
utilWidth: css("#util-class .wk-input").borderTopWidth,
utilStyle: css("#util-class .wk-input").borderTopStyle,
};
});
check(
own.plainBorder === "rgb(255, 0, 0)" && own.plainWidth === "3px",
`${name}: an app class beats whenkit's input look (${own.plainBorder} ${own.plainWidth})`,
);
check(
own.utilWidth === "5px" && own.utilStyle === "dashed",
`${name}: a layered utility beats it too (${own.utilWidth} ${own.utilStyle})`,
);
// A dense app input stays dense: the icon scales with the text instead of
// forcing a 20px line into a 12px field.
const compactHeight = await page.evaluate(
() => document.querySelector("#compact-class .wk-input").getBoundingClientRect().height,
);
check(
compactHeight <= 32,
`${name}: a 12px compact date input stays compact (${Math.round(compactHeight)}px)`,
);
// The date box is the same height as a native input given the same class —
// so a form row does not step when one field is a date.
const heights = await page.evaluate(() => ({
native: document.getElementById("match-native").getBoundingClientRect().height,
date: document.querySelector("#match-date .wk-input").getBoundingClientRect().height,
}));
check(
Math.abs(heights.native - heights.date) <= 1,
`${name}: date box matches a native input's height (${heights.date} vs ${heights.native})`,
);
// An app class with display:block on the same element must not break the layout.
const stacked = await page.evaluate(() => {
const box = document.querySelector("#block-class .wk-input");
return getComputedStyle(box).display;
});
check(stacked === "flex", `${name}: an app's display:block cannot break the box (${stacked})`);
const small = await page.evaluate(
() =>
[
...document.querySelectorAll(
".wk-chip, .wk-day, .wk-field .wk-input, .wk-other .wk-input",
),
]
.filter((el) => el.getClientRects().length > 0)
.map((el) => el.getBoundingClientRect())
.filter((r) => r.height < 43.5 || r.width < 43.5).length,
);
check(small === 0, `${name}: every chip, day and field is at least 44px (${small} too small)`);
const inputSize = await page.evaluate(() =>
parseFloat(getComputedStyle(document.querySelector(".wk-input-native")).fontSize),
);
check(inputSize >= 16, `${name}: date input is ${inputSize}px (iOS zooms below 16)`);
check(
await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth),
`${name}: still no horizontal scroll after picking`,
);
// Nothing may be wider than its column: a grid cell (like a dialog's) must
// not be propped open by the day strip.
const wider = await page.evaluate(
() =>
[...document.querySelectorAll(".grid > section, #dialog")].filter(
(s) =>
s.scrollWidth > s.clientWidth + 1 ||
s.getBoundingClientRect().right > window.innerWidth + 1,
).length,
);
check(wider === 0, `${name}: no column or dialog propped open by the picker (${wider})`);
const sendVisible = await page.evaluate(() => {
const r = document.getElementById("dialog-send").getBoundingClientRect();
return r.right <= window.innerWidth + 1;
});
check(sendVisible, `${name}: the dialog's button stays on screen`);
check(errors.length === 0, `${name}: no page errors ${errors.join(" | ")}`);
// Let chip colour transitions finish so the screenshot shows real states.
await page.waitForTimeout(300);
await page.screenshot({
path: join(root, "demo", "dist", `${name.replace(/ /g, "-")}.png`),
fullPage: true,
});
await page.close();
}
} finally {
await browser.close();
server.close();
}
process.exit(failures ? 1 : 0);