You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
3.0 KiB

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createSmartMediaNode } from "@/utils/smartMediaNode";
class MockImage {
static shouldError = false;
onload: (() => void) | null = null;
onerror: (() => void) | null = null;
width = 1200;
height = 800;
set src(_value: string) {
setTimeout(() => {
if (MockImage.shouldError) {
this.onerror?.();
return;
}
this.onload?.();
}, 0);
}
}
describe("createSmartMediaNode", () => {
beforeEach(() => {
MockImage.shouldError = false;
vi.stubGlobal("fetch", vi.fn(async () => ({
ok: true,
json: async () => ({ success: true }),
})));
});
afterEach(() => {
vi.unstubAllGlobals();
});
it("uses read image dimensions before supplied dimensions", async () => {
vi.stubGlobal("Image", MockImage);
const addNode = vi.fn(() => "smartImage-1");
await createSmartMediaNode({
source: {
kind: "image",
url: "data:image/png;base64,test",
filename: "test.png",
dimensions: { width: 1600, height: 900 },
},
position: { x: 100, y: 200 },
addNode,
});
expect(addNode).toHaveBeenCalledWith(
"smartImage",
{ x: 100, y: 200 },
expect.objectContaining({
dimensions: { width: 1200, height: 800 },
})
);
});
it("falls back to supplied dimensions when image dimensions cannot be read", async () => {
MockImage.shouldError = true;
vi.stubGlobal("Image", MockImage);
const addNode = vi.fn(() => "smartImage-1");
await createSmartMediaNode({
source: {
kind: "image",
url: "data:image/png;base64,test",
filename: "test.png",
dimensions: { width: 1600, height: 900 },
},
position: { x: 100, y: 200 },
addNode,
});
expect(addNode).toHaveBeenCalledWith(
"smartImage",
{ x: 100, y: 200 },
expect.objectContaining({
dimensions: { width: 1600, height: 900 },
})
);
});
it("reads image dimensions when dimensions are null", async () => {
vi.stubGlobal("Image", MockImage);
const addNode = vi.fn(() => "smartImage-1");
await createSmartMediaNode({
source: {
kind: "image",
url: "https://example.com/test.png",
filename: "test.png",
dimensions: null,
},
position: { x: 100, y: 200 },
addNode,
});
expect(addNode).toHaveBeenCalledWith(
"smartImage",
{ x: 100, y: 200 },
expect.objectContaining({
dimensions: { width: 1200, height: 800 },
})
);
});
it("keeps default node size when media has no dimensions", async () => {
const addNode = vi.fn(() => "smartAudio-1");
await createSmartMediaNode({
source: {
kind: "audio",
url: "data:audio/mp3;base64,test",
filename: "test.mp3",
},
position: { x: 100, y: 200 },
addNode,
});
expect(addNode).toHaveBeenCalledWith(
"smartAudio",
{ x: 100, y: 200 },
expect.any(Object)
);
});
});