|
| 1 | +/** |
| 2 | + * Feature assertion: the "Reduce motion" toggle in Settings → Appearance |
| 3 | + * actually applies and persists an app-wide reduced-motion preference. |
| 4 | + * |
| 5 | + * Drives the real UI over CDP entirely in the draft/no-session state (no seeded |
| 6 | + * conversation, no backend connection): opens Settings → Appearance, flips the |
| 7 | + * toggle, and asserts the observable effects — the switch state, the |
| 8 | + * `data-reduce-motion` attribute on <html>, and the persisted localStorage value. |
| 9 | + * Toggling twice proves it both applies and reverts, not merely renders. |
| 10 | + */ |
| 11 | + |
| 12 | +import type { Assertion } from '../runner'; |
| 13 | + |
| 14 | +const SETTINGS_NAV = '[data-testid="nav:settings"]'; |
| 15 | +const APPEARANCE_NAV = '[data-testid="settings-nav-appearance"]'; |
| 16 | +const TOGGLE = '[data-testid="reduce-motion-toggle"]'; |
| 17 | +const STORAGE_KEY = 'craft-reduce-motion'; |
| 18 | + |
| 19 | +/** Read the toggle's aria-checked ("true" | "false" | null). */ |
| 20 | +function ariaCheckedExpr(): string { |
| 21 | + return `(() => { |
| 22 | + const el = document.querySelector(${JSON.stringify(TOGGLE)}); |
| 23 | + return el ? el.getAttribute('aria-checked') : null; |
| 24 | + })()`; |
| 25 | +} |
| 26 | + |
| 27 | +/** True when <html> carries the reduce-motion marker attribute. */ |
| 28 | +function htmlMarkedExpr(): string { |
| 29 | + return `document.documentElement.getAttribute('data-reduce-motion') === 'true'`; |
| 30 | +} |
| 31 | + |
| 32 | +/** The persisted localStorage value for the preference. */ |
| 33 | +function storedValueExpr(): string { |
| 34 | + return `window.localStorage.getItem(${JSON.stringify(STORAGE_KEY)})`; |
| 35 | +} |
| 36 | + |
| 37 | +const assertion: Assertion = { |
| 38 | + name: 'reduce-motion toggle applies and persists an app-wide preference', |
| 39 | + async run(app) { |
| 40 | + const { session } = app; |
| 41 | + |
| 42 | + // App fully mounted. |
| 43 | + await session.waitForFunction( |
| 44 | + '!document.getElementById("_loader") && (document.getElementById("root")?.childElementCount ?? 0) > 0', |
| 45 | + { timeoutMs: 30000, message: 'app did not mount' }, |
| 46 | + ); |
| 47 | + |
| 48 | + // Open Settings → Appearance (real user path). |
| 49 | + await session.click(SETTINGS_NAV, { timeoutMs: 15000 }); |
| 50 | + await session.click(APPEARANCE_NAV, { timeoutMs: 15000 }); |
| 51 | + |
| 52 | + // The toggle is the feature under test — its presence is the first signal. |
| 53 | + await session.waitForSelector(TOGGLE, { |
| 54 | + timeoutMs: 15000, |
| 55 | + message: 'reduce-motion toggle did not render', |
| 56 | + }); |
| 57 | + |
| 58 | + // Initial state: off, no marker on <html>, not persisted true. |
| 59 | + const initialChecked = await session.evaluate<string | null>(ariaCheckedExpr()); |
| 60 | + if (initialChecked !== 'false') { |
| 61 | + throw new Error(`expected toggle off initially, saw aria-checked=${initialChecked}`); |
| 62 | + } |
| 63 | + if (await session.evaluate<boolean>(htmlMarkedExpr())) { |
| 64 | + throw new Error('expected no data-reduce-motion attribute before enabling'); |
| 65 | + } |
| 66 | + |
| 67 | + // Enable → toggle on, <html> marked, persisted true. |
| 68 | + await session.click(TOGGLE); |
| 69 | + await session.waitForFunction( |
| 70 | + `${ariaCheckedExpr()} === 'true'`, |
| 71 | + { timeoutMs: 5000, message: 'toggle did not switch on' }, |
| 72 | + ); |
| 73 | + await session.waitForFunction(htmlMarkedExpr(), { |
| 74 | + timeoutMs: 5000, |
| 75 | + message: 'data-reduce-motion was not applied to <html> when enabled', |
| 76 | + }); |
| 77 | + const storedOn = await session.evaluate<string | null>(storedValueExpr()); |
| 78 | + if (storedOn !== 'true') { |
| 79 | + throw new Error(`expected persisted "true" after enabling, saw ${JSON.stringify(storedOn)}`); |
| 80 | + } |
| 81 | + |
| 82 | + // Disable → toggle off, marker removed, persisted false. |
| 83 | + await session.click(TOGGLE); |
| 84 | + await session.waitForFunction( |
| 85 | + `${ariaCheckedExpr()} === 'false'`, |
| 86 | + { timeoutMs: 5000, message: 'toggle did not switch off' }, |
| 87 | + ); |
| 88 | + await session.waitForFunction(`!(${htmlMarkedExpr()})`, { |
| 89 | + timeoutMs: 5000, |
| 90 | + message: 'data-reduce-motion was not removed from <html> when disabled', |
| 91 | + }); |
| 92 | + const storedOff = await session.evaluate<string | null>(storedValueExpr()); |
| 93 | + if (storedOff !== 'false') { |
| 94 | + throw new Error(`expected persisted "false" after disabling, saw ${JSON.stringify(storedOff)}`); |
| 95 | + } |
| 96 | + }, |
| 97 | +}; |
| 98 | + |
| 99 | +export default assertion; |
0 commit comments