b781ffeb66
Contain-fit waits for a real parent width, uses ResizeObserver, and shrinks width when maxH would otherwise stretch the CSS box. Pure fitDisplay is covered by node tests.
27 lines
961 B
TypeScript
27 lines
961 B
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { fitDisplay } from './fitDisplay.ts';
|
|
|
|
test('parentW=0 → null (no size until laid out)', () => {
|
|
assert.equal(fitDisplay(0, 1000, 750, 420), null);
|
|
assert.equal(fitDisplay(-5, 1000, 750, 420), null);
|
|
});
|
|
|
|
test('280 wide, 4:3 landscape → contain, under the height cap', () => {
|
|
const fit = fitDisplay(280, 1600, 1200, 420);
|
|
assert.ok(fit);
|
|
assert.equal(fit.fitW, 280);
|
|
assert.ok(Math.abs(fit.fitH - 210) < 1e-9);
|
|
assert.ok(fit.fitH <= 420);
|
|
});
|
|
|
|
test('portrait under maxH never stretches: height capped, width shrinks', () => {
|
|
// 1000 wide source, 3000 tall. Full-width fit would be 900 tall > 420.
|
|
const fit = fitDisplay(400, 1000, 3000, 420);
|
|
assert.ok(fit);
|
|
assert.equal(fit.fitH, 420);
|
|
assert.ok(Math.abs(fit.fitW - 140) < 1e-9);
|
|
// aspect preserved: fitW/fitH === imgW/imgH
|
|
assert.ok(Math.abs(fit.fitW / fit.fitH - 1000 / 3000) < 1e-9);
|
|
});
|