// phone-frame.mjs's scale: a phone as tall as the stage allows, never // larger than the phone itself, and the stages it is mounted on. import test from 'node:test' import assert from 'node:assert/strict' import { phoneScale, mountPhoneFrame, PHONE_BLOCK } from './phone-frame.mjs' test('the scale is the room over the phone height, never above 1', () => { assert.equal(PHONE_BLOCK, 844) assert.equal(phoneScale(844), 1) assert.equal(phoneScale(2000), 1) assert.equal(phoneScale(422), 0.5) assert.equal(phoneScale(0), 0) }) test('the scale fits the width as well: the smaller of the two', () => { assert.equal(phoneScale(844, 195), 0.5) assert.equal(phoneScale(422, 390), 0.5) assert.equal(phoneScale(844, 0), 0) assert.equal(phoneScale(2000, 2000), 1) assert.equal(phoneScale(Infinity, 292.5), 0.75) }) test('the scale never rounds up past the room it was given', () => { for (const room of [100, 333, 447.5, 611, 843.9]) { const s = phoneScale(room) assert.ok(s * PHONE_BLOCK <= room, `${room}: ${s} × 844 = ${s * PHONE_BLOCK}`) assert.ok(room - s * PHONE_BLOCK < 0.1, `${room}: ${s} leaves ${room - s * PHONE_BLOCK}px`) } assert.equal(phoneScale(-5), 0) assert.equal(phoneScale(Number.NaN), 0) }) // A stand-in for the stages element and the frames in it. room is // what CSS says bounds the phone besides the width: 'block' when the // stages' height is the phone's to take. function fakeStages (height, width, room = 'block') { const frames = [0, 1].map(() => { const props = {} return { props, style: { setProperty: (k, v) => { props[k] = v } } } }) return { frames, room, clientHeight: height, clientWidth: width, querySelectorAll: sel => { assert.equal(sel, '[slides-stage-phone], [slides-stage-frame]') return frames } } } test('mountPhoneFrame writes the scale on every frame, and again when the stages resize', () => { const observed = [] globalThis.ResizeObserver = class { constructor (cb) { this.cb = cb; observed.push(this) } observe (el) { this.el = el } disconnect () { this.gone = true } } globalThis.getComputedStyle = el => ({ getPropertyValue: k => k === '--slides-phone-room' ? ' ' + el.room : '' }) try { const stagesEl = fakeStages(422, 1000) const heard = [] const frame = mountPhoneFrame({ stagesEl, onChange: s => heard.push(s) }) for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '0.5') assert.equal(observed[0].el, stagesEl) stagesEl.clientHeight = 2000 observed[0].cb() for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '1') observed[0].cb() // the same size again: nothing new to say // Narrower than a phone: the width bounds it. stagesEl.clientWidth = 292.5 observed[0].cb() for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '0.75') // Where the page scrolls, the stages' height is the phone's own and // not a bound: only the width counts. stagesEl.room = '' stagesEl.clientHeight = 300 stagesEl.clientWidth = 1000 observed[0].cb() for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '1') assert.deepEqual(heard, [0.5, 1, 0.75, 1]) frame.destroy() assert.equal(observed[0].gone, true) } finally { delete globalThis.ResizeObserver delete globalThis.getComputedStyle } }) test('update() measures again when the room changes and the stages do not resize', () => { // From 1100px the stages fill the pane in every view, so a switch to // the phone view changes what bounds the phone without changing the // stages' size, and no ResizeObserver hears of it. globalThis.ResizeObserver = class { observe () {} disconnect () {} } globalThis.getComputedStyle = el => ({ getPropertyValue: k => k === '--slides-phone-room' ? el.room : '' }) try { const stagesEl = fakeStages(422, 1000, '') const frame = mountPhoneFrame({ stagesEl }) for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '1') stagesEl.room = 'block' frame.update() for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '0.5') frame.destroy() } finally { delete globalThis.ResizeObserver delete globalThis.getComputedStyle } })