theme-page.ts
This file
Path
apps/web/test/theme-page.ts
136 lines · 3.8 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 1–136why, one step back1/**2 * A browser page as far as the theme reaches into one: the root element, the3 * head with two `theme-color` metas per scheme already in it, the system's4 * colour scheme, and a cookie jar that records every write, so a test can say none5 * happened.6 */7import {8 THEME_META,9 THEME_PREFERENCE_KEY,10} from "../src/features/shared/theme.ts";11import type { Theme } from "../src/features/shared/theme.ts";1213interface FakeMeta {14 readonly attributes: Map<string, string>;15 readonly setAttribute: (name: string, value: string) => void;16}1718const fakeMeta = (entries: readonly (readonly [string, string])[] = []) => {19 const attributes = new Map(entries);2021 const meta: FakeMeta = {22 attributes,23 setAttribute: (name, value) => {24 attributes.set(name, value);25 },26 };2728 return meta;29};3031type StorageListener = (event: { readonly key: string | null }) => void;3233const SYSTEM_DARK = "(prefers-color-scheme: dark)";3435const rendered = (scheme: Theme, content: string) =>36 fakeMeta([37 ["content", content],38 ["media", `(prefers-color-scheme: ${scheme})`],39 ["name", "theme-color"],40 ]);4142export const themePage = (system: Theme) => {43 const root: DOMStringMap = {};4445 const head = [46 rendered("light", "react-light"),47 rendered("dark", "react-dark"),48 ];4950 const cookies: string[] = [];51 const changes: (() => void)[] = [];52 const stored = new Set<StorageListener>();53 const scheme = { matches: system === "dark" };5455 const document = {56 documentElement: { dataset: root },57 get cookie() {58 return cookies.join("; ");59 },60 set cookie(value: string) {61 cookies.push(value);62 },63 head: {64 insertAdjacentHTML: (_: "afterbegin", html: typeof THEME_META) => {65 if (html === THEME_META) {66 head.unshift(67 fakeMeta([68 ["data-theme-color", ""],69 ["name", "theme-color"],70 ])71 );72 }73 },74 },75 querySelector: (selector: string) =>76 selector === "meta[data-theme-color]"77 ? (head.find(({ attributes }) => attributes.has("data-theme-color")) ??78 null)79 : null,80 };8182 const matchMedia = (query: string) => ({83 addEventListener: (_: "change", listener: () => void) => {84 changes.push(listener);85 },86 get matches() {87 return query === SYSTEM_DARK && scheme.matches;88 },89 });9091 const matching = (meta: FakeMeta) => {92 const media = meta.attributes.get("media");9394 return media === undefined || (media === SYSTEM_DARK) === scheme.matches;95 };9697 return {98 /** The window's, which hears another tab write to storage. */99 addEventListener: (_: "storage", listener: StorageListener) => {100 stored.add(listener);101 },102 cookies,103 document,104 matchMedia,105 /** Each `theme-color` meta in the head, in order, as its attributes. */106 metas: () => head.map(({ attributes }) => Object.fromEntries(attributes)),107 /** The browser's: the first `theme-color` meta whose media matches. */108 shownColour: () =>109 head110 .find(111 (meta) =>112 meta.attributes.get("name") === "theme-color" && matching(meta)113 )114 ?.attributes.get("content"),115 removeEventListener: (_: "storage", listener: StorageListener) => {116 stored.delete(listener);117 },118 /** Another tab writes to storage, under the theme's key unless named. */119 storageWritten: (key: string | null = THEME_PREFERENCE_KEY) => {120 for (const listener of stored) {121 listener({ key });122 }123 },124 /** The system's own scheme, as the head script names it. */125 system: () => root.system,126 theme: () => root.theme,127 /** The system turns the other way, as at sunset. */128 turnSystem: () => {129 scheme.matches = !scheme.matches;130131 for (const changed of changes) {132 changed();133 }134 },135 };136};