Files
excalidraw/packages/element/tests/newElement.test.ts
T
David LuzarandMárk Tolmács 854d00c31b feat(editor): track element creation timestamps (#12060)
feat(editor): element creation timestamp tracking

Co-authored-by: Márk Tolmács <mark@lazycat.hu>
2026-09-08 15:26:14 +02:00

53 lines
1.4 KiB
TypeScript

import * as common from "@excalidraw/common";
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
import { newElement, newLinearElement } from "../src/newElement";
import { mutateElement, newElementWith } from "../src/mutateElement";
describe("element creation time", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it.each([
"rectangle",
"diamond",
"ellipse",
"text",
"line",
"arrow",
"freedraw",
"image",
"frame",
"magicframe",
"embeddable",
"iframe",
] as const)("initializes a new %s before any capture", (type) => {
vi.spyOn(common, "getUpdatedTimestamp").mockReturnValue(123);
const element = API.createElement({ type });
expect(element.created).toBe(123);
vi.mocked(common.getUpdatedTimestamp).mockReturnValue(456);
const edited = newElementWith(element, { x: 50 });
mutateElement(edited, new Map(), { y: 50 });
expect(edited).toMatchObject({ created: 123, updated: 456 });
});
it.each([123, 0, null])(
"preserves explicit created=%s when reconstructing the same element",
(created) => {
const original = newElement({
type: "rectangle",
x: 0,
y: 0,
created,
});
const converted = newLinearElement({ ...original, type: "line" });
expect(converted).toMatchObject({ id: original.id, created });
},
);
});