|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { test } from 'node:test' |
| 3 | +import { runInNewContext } from 'node:vm' |
| 4 | +import { DOCS_WIDTH_BOOTSTRAP } from '../src/utils/docs-width' |
| 5 | + |
| 6 | +function applyPreference(value: string | null, blocked = false) { |
| 7 | + const properties = new Map<string, string>() |
| 8 | + runInNewContext(DOCS_WIDTH_BOOTSTRAP, { |
| 9 | + localStorage: { |
| 10 | + getItem(key: string) { |
| 11 | + assert.equal(key, 'docsFullWidth') |
| 12 | + if (blocked) throw new Error('Storage is unavailable') |
| 13 | + return value |
| 14 | + }, |
| 15 | + }, |
| 16 | + document: { |
| 17 | + documentElement: { |
| 18 | + style: { |
| 19 | + setProperty(key: string, value: string) { |
| 20 | + properties.set(key, value) |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + Date: { now: () => 1000 }, |
| 26 | + }) |
| 27 | + return properties.get('--docs-content-width') |
| 28 | +} |
| 29 | + |
| 30 | +test('saved expanded width applies before React mounts', () => { |
| 31 | + assert.equal(applyPreference('{"value":true,"ttl":null}'), '100%') |
| 32 | + assert.equal(applyPreference('{"value":true}'), '100%') |
| 33 | + assert.equal(applyPreference('{"value":true,"ttl":1000}'), '100%') |
| 34 | + assert.equal(applyPreference('{"value":true,"ttl":2000}'), '100%') |
| 35 | +}) |
| 36 | + |
| 37 | +test('constrained, missing, expired and invalid preferences retain the default width', () => { |
| 38 | + for (const value of [ |
| 39 | + null, |
| 40 | + 'null', |
| 41 | + '{}', |
| 42 | + 'invalid json', |
| 43 | + '{"value":false}', |
| 44 | + '{"value":"true"}', |
| 45 | + '{"value":true,"ttl":999}', |
| 46 | + ]) { |
| 47 | + assert.equal(applyPreference(value), undefined) |
| 48 | + } |
| 49 | +}) |
| 50 | + |
| 51 | +test('blocked storage does not interrupt document initialization', () => { |
| 52 | + assert.equal(applyPreference(null, true), undefined) |
| 53 | +}) |
0 commit comments