Files
webstudio/apps/builder/app/canvas/features/webstudio-component/webstudio-component.test.tsx
Oleg Isonen 905103c739 feat: When asset is deleted or uploaded - refresh asset resource (#5568)
## Description

When user is using asset resource and binding it on canvas, for example
video file bound to video tag to the src attribute and then deletes the
asset, it is expected that canvas reflects the deleted asset as well as
the variable.

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
0000)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2026-01-16 21:05:16 +01:00

82 lines
2.2 KiB
TypeScript

/**
* @vitest-environment jsdom
*/
import { describe, test, expect } from "vitest";
import { __testing__ } from "./webstudio-component";
const { computeComponentKey } = __testing__;
describe("computeComponentKey - key generation logic", () => {
test("prioritizes assetId over other props", () => {
expect(
computeComponentKey({
$webstudio$canvasOnly$assetId: "asset-123",
src: "/image.jpg",
defaultValue: "default",
})
).toBe("asset-123");
});
test("falls back to defaultValue when no assetId", () => {
expect(
computeComponentKey({
src: "/image.jpg",
defaultValue: "default-value",
})
).toBe("default-value");
});
test("uses src when no assetId or defaultValue", () => {
expect(
computeComponentKey({
src: "/path/to/video.mp4",
})
).toBe("/path/to/video.mp4");
});
test("returns undefined when no relevant props", () => {
expect(computeComponentKey({})).toBeUndefined();
});
test("handles null and undefined values", () => {
expect(
computeComponentKey({
src: null,
defaultValue: undefined,
})
).toBeUndefined();
});
test("coerces defaultValue to string", () => {
expect(computeComponentKey({ defaultValue: 42 })).toBe("42");
expect(computeComponentKey({ defaultValue: true })).toBe("true");
expect(computeComponentKey({ defaultValue: 0 })).toBe("0");
});
test("coerces src to string", () => {
expect(computeComponentKey({ src: "string-src" })).toBe("string-src");
expect(computeComponentKey({ src: 123 })).toBe("123");
expect(computeComponentKey({ src: undefined })).toBeUndefined();
});
test("different assetIds produce different keys", () => {
const key1 = computeComponentKey({
$webstudio$canvasOnly$assetId: "asset-123",
src: "/image.jpg",
});
const key2 = computeComponentKey({
$webstudio$canvasOnly$assetId: "asset-456",
src: "/image.jpg",
});
expect(key1).not.toBe(key2);
});
test("different src values produce different keys", () => {
const key1 = computeComponentKey({ src: "/assets/video1.mp4" });
const key2 = computeComponentKey({ src: "/assets/video2.mp4" });
expect(key1).not.toBe(key2);
});
});