Files
NODEDC_DESIGN_GUIDELINE/scripts/modal-size.test.mjs
T

41 lines
2.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { fitModalSize, createModalSizeController } from '../packages/ui-dom/dist/modalSize.js';
test('modal dimensions fit narrow and desktop screens without mutating preference', () => {
const requested = { width: 1600, height: 900 };
assert.deepEqual(fitModalSize(requested, { width: 375, height: 667 }), { width: 343, height: 635 });
assert.deepEqual(fitModalSize(requested, { width: 1920, height: 1080 }), requested);
assert.deepEqual(requested, { width: 1600, height: 900 });
});
test('only completed gestures commit; viewport fitting and cancellation preserve preference', () => {
class Element extends EventTarget {
style = { width: '', height: '', maxHeight: '' }; dataset = {}; attributes = new Map(); children = [];
setAttribute(k,v) { this.attributes.set(k,v); } getAttribute(k) { return this.attributes.get(k) ?? null; } removeAttribute(k) { this.attributes.delete(k); }
append(value) { this.children.push(value); } remove() {} setPointerCapture() {}
getClientRects() { return [1]; } getBoundingClientRect() { return { width: parseFloat(this.style.width), height: parseFloat(this.style.height) }; }
querySelector() { return head; }
}
const head = new Element(), dialog = new Element(), commits = [];
const oldWindow = globalThis.window, oldDocument = globalThis.document;
globalThis.window = Object.assign(new EventTarget(), { innerWidth: 1600, innerHeight: 1000 });
globalThis.document = { createElement: () => new Element() };
const dispatch = (target, type, values) => target.dispatchEvent(Object.assign(new Event(type, { cancelable: true }), values));
try {
const controller = createModalSizeController({ dialog, initialSize: { width: 800, height: 500 }, onSizeCommit: s => commits.push(s) });
const edge = dialog.children.find(node => node.dataset.edge === 'se');
dispatch(edge, 'pointerdown', {button: 0, pointerId: 1, clientX: 500, clientY: 500});
dispatch(window, 'pointermove', {pointerId: 1, clientX: 650, clientY: 600});
assert.equal(commits.length, 0);
dispatch(window, 'pointerup', {pointerId: 1});
assert.deepEqual(commits, [{width: 1100, height: 700}]);
window.innerWidth = 500; window.innerHeight = 400; window.dispatchEvent(new Event('resize'));
assert.equal(dialog.style.width, '468px'); assert.equal(commits.length, 1);
window.innerWidth = 1600; window.innerHeight = 1000; window.dispatchEvent(new Event('resize'));
assert.equal(dialog.style.width, '1100px');
dispatch(edge, 'pointerdown', {button: 0, pointerId: 2, clientX: 500, clientY: 500});
dispatch(window, 'pointermove', {pointerId: 2, clientX: 650, clientY: 600});
dispatch(window, 'pointercancel', {pointerId: 2}); assert.equal(dialog.style.width, '1100px'); assert.equal(commits.length, 1);
controller.destroy(); assert.equal(dialog.style.width, '');
} finally { globalThis.window = oldWindow; globalThis.document = oldDocument; }
});