| 1 | // phone-frame.mjs's scale: a phone as tall as the stage allows, never |
| 2 | // larger than the phone itself, and the stages it is mounted on. |
| 3 | import test from 'node:test' |
| 4 | import assert from 'node:assert/strict' |
| 5 | import { phoneScale, mountPhoneFrame, PHONE_BLOCK } from './phone-frame.mjs' |
| 6 | |
| 7 | test('the scale is the room over the phone height, never above 1', () => { |
| 8 | assert.equal(PHONE_BLOCK, 844) |
| 9 | assert.equal(phoneScale(844), 1) |
| 10 | assert.equal(phoneScale(2000), 1) |
| 11 | assert.equal(phoneScale(422), 0.5) |
| 12 | assert.equal(phoneScale(0), 0) |
| 13 | }) |
| 14 | |
| 15 | test('the scale fits the width as well: the smaller of the two', () => { |
| 16 | assert.equal(phoneScale(844, 195), 0.5) |
| 17 | assert.equal(phoneScale(422, 390), 0.5) |
| 18 | assert.equal(phoneScale(844, 0), 0) |
| 19 | assert.equal(phoneScale(2000, 2000), 1) |
| 20 | assert.equal(phoneScale(Infinity, 292.5), 0.75) |
| 21 | }) |
| 22 | |
| 23 | test('the scale never rounds up past the room it was given', () => { |
| 24 | for (const room of [100, 333, 447.5, 611, 843.9]) { |
| 25 | const s = phoneScale(room) |
| 26 | assert.ok(s * PHONE_BLOCK <= room, `${room}: ${s} × 844 = ${s * PHONE_BLOCK}`) |
| 27 | assert.ok(room - s * PHONE_BLOCK < 0.1, `${room}: ${s} leaves ${room - s * PHONE_BLOCK}px`) |
| 28 | } |
| 29 | assert.equal(phoneScale(-5), 0) |
| 30 | assert.equal(phoneScale(Number.NaN), 0) |
| 31 | }) |
| 32 | |
| 33 | // A stand-in for the stages element and the frames in it. room is |
| 34 | // what CSS says bounds the phone besides the width: 'block' when the |
| 35 | // stages' height is the phone's to take. |
| 36 | function fakeStages (height, width, room = 'block') { |
| 37 | const frames = [0, 1].map(() => { |
| 38 | const props = {} |
| 39 | return { props, style: { setProperty: (k, v) => { props[k] = v } } } |
| 40 | }) |
| 41 | return { |
| 42 | frames, |
| 43 | room, |
| 44 | clientHeight: height, |
| 45 | clientWidth: width, |
| 46 | querySelectorAll: sel => { |
| 47 | assert.equal(sel, '[slides-stage-phone], [slides-stage-frame]') |
| 48 | return frames |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | test('mountPhoneFrame writes the scale on every frame, and again when the stages resize', () => { |
| 54 | const observed = [] |
| 55 | globalThis.ResizeObserver = class { |
| 56 | constructor (cb) { this.cb = cb; observed.push(this) } |
| 57 | observe (el) { this.el = el } |
| 58 | disconnect () { this.gone = true } |
| 59 | } |
| 60 | globalThis.getComputedStyle = el => ({ getPropertyValue: k => k === '--slides-phone-room' ? ' ' + el.room : '' }) |
| 61 | try { |
| 62 | const stagesEl = fakeStages(422, 1000) |
| 63 | const heard = [] |
| 64 | const frame = mountPhoneFrame({ stagesEl, onChange: s => heard.push(s) }) |
| 65 | for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '0.5') |
| 66 | assert.equal(observed[0].el, stagesEl) |
| 67 | |
| 68 | stagesEl.clientHeight = 2000 |
| 69 | observed[0].cb() |
| 70 | for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '1') |
| 71 | observed[0].cb() // the same size again: nothing new to say |
| 72 | |
| 73 | // Narrower than a phone: the width bounds it. |
| 74 | stagesEl.clientWidth = 292.5 |
| 75 | observed[0].cb() |
| 76 | for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '0.75') |
| 77 | |
| 78 | // Where the page scrolls, the stages' height is the phone's own and |
| 79 | // not a bound: only the width counts. |
| 80 | stagesEl.room = '' |
| 81 | stagesEl.clientHeight = 300 |
| 82 | stagesEl.clientWidth = 1000 |
| 83 | observed[0].cb() |
| 84 | for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '1') |
| 85 | assert.deepEqual(heard, [0.5, 1, 0.75, 1]) |
| 86 | |
| 87 | frame.destroy() |
| 88 | assert.equal(observed[0].gone, true) |
| 89 | } finally { |
| 90 | delete globalThis.ResizeObserver |
| 91 | delete globalThis.getComputedStyle |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | test('update() measures again when the room changes and the stages do not resize', () => { |
| 96 | // From 1100px the stages fill the pane in every view, so a switch to |
| 97 | // the phone view changes what bounds the phone without changing the |
| 98 | // stages' size, and no ResizeObserver hears of it. |
| 99 | globalThis.ResizeObserver = class { |
| 100 | observe () {} |
| 101 | disconnect () {} |
| 102 | } |
| 103 | globalThis.getComputedStyle = el => ({ getPropertyValue: k => k === '--slides-phone-room' ? el.room : '' }) |
| 104 | try { |
| 105 | const stagesEl = fakeStages(422, 1000, '') |
| 106 | const frame = mountPhoneFrame({ stagesEl }) |
| 107 | for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '1') |
| 108 | stagesEl.room = 'block' |
| 109 | frame.update() |
| 110 | for (const f of stagesEl.frames) assert.equal(f.props['--slides-phone-scale'], '0.5') |
| 111 | frame.destroy() |
| 112 | } finally { |
| 113 | delete globalThis.ResizeObserver |
| 114 | delete globalThis.getComputedStyle |
| 115 | } |
| 116 | }) |
| 117 | |