2026-02-25 15:47:25 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2026 Element Creations Ltd.
|
|
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-08-04 19:01:20 +02:00
|
|
|
import { describe, expect, test } from "vitest";
|
2026-02-25 15:47:25 +01:00
|
|
|
|
2026-08-04 19:01:20 +02:00
|
|
|
import { autoVideoFit } from "./videoFit";
|
2026-02-25 15:47:25 +01:00
|
|
|
|
|
|
|
|
describe("videoFit$ defaults", () => {
|
|
|
|
|
test.each([
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: 1920 / 1080,
|
|
|
|
|
tileAspectRatio: NaN,
|
2026-02-25 15:47:25 +01:00
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: 1080 / 1920,
|
|
|
|
|
tileAspectRatio: NaN,
|
2026-02-25 15:47:25 +01:00
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: NaN,
|
|
|
|
|
tileAspectRatio: 1920 / 1080,
|
2026-02-25 15:47:25 +01:00
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: NaN,
|
|
|
|
|
tileAspectRatio: 1080 / 1920,
|
2026-02-25 15:47:25 +01:00
|
|
|
},
|
|
|
|
|
])(
|
2026-08-04 19:01:20 +02:00
|
|
|
"videoFit$ returns `cover` when videoAspectRatio is $videoAspectRatio and tileAspectRatio is $tileAspectRatio",
|
|
|
|
|
({ videoAspectRatio, tileAspectRatio }) =>
|
|
|
|
|
expect(autoVideoFit(videoAspectRatio, tileAspectRatio)).toBe("cover"),
|
2026-02-25 15:47:25 +01:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-04 19:01:20 +02:00
|
|
|
const VIDEO_480_L = 640 / 480;
|
|
|
|
|
const VIDEO_720_L = 1280 / 720;
|
|
|
|
|
const VIDEO_1080_L = 1920 / 1080;
|
2026-02-25 15:47:25 +01:00
|
|
|
|
|
|
|
|
// Some sizes from real world testing, which don't match the standard video sizes exactly
|
2026-08-04 19:01:20 +02:00
|
|
|
const TILE_SIZE_1_L = 180 / 135;
|
|
|
|
|
const TILE_SIZE_3_P = 379 / 542;
|
|
|
|
|
const TILE_SIZE_4_L = 957 / 542;
|
2026-02-25 15:47:25 +01:00
|
|
|
// This is the size of an iPhone Xr in portrait mode
|
2026-08-04 19:01:20 +02:00
|
|
|
const TILE_SIZE_5_P = 414 / 896;
|
2026-02-25 15:47:25 +01:00
|
|
|
|
2026-08-04 19:01:20 +02:00
|
|
|
function inverse(ratio: number): number {
|
|
|
|
|
return 1 / ratio;
|
2026-02-25 15:47:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: VIDEO_480_L,
|
|
|
|
|
tileAspectRatio: TILE_SIZE_1_L,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "cover",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: inverse(VIDEO_480_L),
|
|
|
|
|
tileAspectRatio: TILE_SIZE_1_L,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "contain",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: VIDEO_720_L,
|
|
|
|
|
tileAspectRatio: TILE_SIZE_4_L,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "cover",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: inverse(VIDEO_720_L),
|
|
|
|
|
tileAspectRatio: TILE_SIZE_4_L,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "contain",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: inverse(VIDEO_1080_L),
|
|
|
|
|
tileAspectRatio: TILE_SIZE_3_P,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "cover",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: VIDEO_1080_L,
|
|
|
|
|
tileAspectRatio: TILE_SIZE_5_P,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "contain",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: inverse(VIDEO_1080_L),
|
|
|
|
|
tileAspectRatio: TILE_SIZE_5_P,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "cover",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// square video
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: 400 / 400,
|
|
|
|
|
tileAspectRatio: VIDEO_480_L,
|
2026-02-25 15:47:25 +01:00
|
|
|
expected: "contain",
|
|
|
|
|
},
|
2026-03-02 15:38:43 +01:00
|
|
|
{
|
|
|
|
|
// Should default to cover if the initial size is 0:0.
|
|
|
|
|
// Or else it will cause a flash of "contain" mode until the real size is loaded, which can be jarring.
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: VIDEO_480_L,
|
|
|
|
|
tileAspectRatio: 0 / 0,
|
2026-03-02 15:38:43 +01:00
|
|
|
expected: "cover",
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-08-04 19:01:20 +02:00
|
|
|
videoAspectRatio: 0 / 0,
|
|
|
|
|
tileAspectRatio: VIDEO_480_L,
|
2026-03-02 15:38:43 +01:00
|
|
|
expected: "cover",
|
|
|
|
|
},
|
2026-02-25 15:47:25 +01:00
|
|
|
])(
|
2026-08-04 19:01:20 +02:00
|
|
|
"videoFit$ returns $expected when videoAspectRatio is $videoAspectRatio and tileAspectRatio is $tileAspectRatio",
|
|
|
|
|
({ videoAspectRatio, tileAspectRatio, expected }) =>
|
|
|
|
|
expect(autoVideoFit(videoAspectRatio, tileAspectRatio)).toBe(expected),
|
2026-02-25 15:47:25 +01:00
|
|
|
);
|