|
|
|
@ -1,8 +1,27 @@ |
|
|
|
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 }), |
|
|
|
@ -13,7 +32,33 @@ describe("createSmartMediaNode", () => { |
|
|
|
vi.unstubAllGlobals(); |
|
|
|
}); |
|
|
|
|
|
|
|
it("passes media dimensions in node data", async () => { |
|
|
|
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({ |
|
|
|
@ -36,6 +81,30 @@ describe("createSmartMediaNode", () => { |
|
|
|
); |
|
|
|
}); |
|
|
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
|