snapshot of https://github.com/excalidraw/excalidraw.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Tests / test (push) Failing after 15s
Some checks failed
Tests / test (push) Failing after 15s
This commit is contained in:
111
packages/excalidraw/tests/helpers/mocks.ts
Normal file
111
packages/excalidraw/tests/helpers/mocks.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import * as MermaidToExcalidraw from "@excalidraw/mermaid-to-excalidraw";
|
||||
import React from "react";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import type { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw";
|
||||
import type { throttleRAF as throttleRAFType } from "@excalidraw/common";
|
||||
|
||||
type ThrottledFn<T extends unknown[]> = ((...args: T) => void) & {
|
||||
flush: () => void;
|
||||
cancel: () => void;
|
||||
};
|
||||
|
||||
export const mockThrottleRAF: typeof throttleRAFType = <T extends unknown[]>(
|
||||
fn: (...args: T) => void,
|
||||
) => {
|
||||
const ret = ((...args: T) => {
|
||||
fn(...args);
|
||||
}) as ThrottledFn<T>;
|
||||
|
||||
ret.flush = () => {};
|
||||
ret.cancel = () => {};
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
export const mockMermaidToExcalidraw = (opts: {
|
||||
parseMermaidToExcalidraw: typeof parseMermaidToExcalidraw;
|
||||
mockRef?: boolean;
|
||||
}) => {
|
||||
vi.mock("@excalidraw/mermaid-to-excalidraw", async (importActual) => {
|
||||
const module = (await importActual()) as any;
|
||||
|
||||
return {
|
||||
__esModule: true,
|
||||
...module,
|
||||
};
|
||||
});
|
||||
const parseMermaidToExcalidrawSpy = vi.spyOn(
|
||||
MermaidToExcalidraw,
|
||||
"parseMermaidToExcalidraw",
|
||||
);
|
||||
|
||||
parseMermaidToExcalidrawSpy.mockImplementation(opts.parseMermaidToExcalidraw);
|
||||
|
||||
if (opts.mockRef) {
|
||||
vi.spyOn(React, "useRef").mockReturnValue({
|
||||
current: {
|
||||
parseMermaidToExcalidraw: parseMermaidToExcalidrawSpy,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Mock for HTMLImageElement (use with `vi.unstubAllGlobals()`)
|
||||
// as jsdom.resources: "usable" throws an error on image load
|
||||
export const mockHTMLImageElement = (
|
||||
naturalWidth: number,
|
||||
naturalHeight: number,
|
||||
) => {
|
||||
vi.stubGlobal(
|
||||
"Image",
|
||||
class extends Image {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
Object.defineProperty(this, "naturalWidth", {
|
||||
value: naturalWidth,
|
||||
});
|
||||
Object.defineProperty(this, "naturalHeight", {
|
||||
value: naturalHeight,
|
||||
});
|
||||
|
||||
queueMicrotask(() => {
|
||||
this.onload?.({} as Event);
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
// Mocks for multiple HTMLImageElements (dimensions are assigned in the order of image initialization)
|
||||
export const mockMultipleHTMLImageElements = (
|
||||
sizes: (readonly [number, number])[],
|
||||
) => {
|
||||
const _sizes = [...sizes];
|
||||
|
||||
vi.stubGlobal(
|
||||
"Image",
|
||||
class extends Image {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const size = _sizes.shift();
|
||||
if (!size) {
|
||||
throw new Error("Insufficient sizes");
|
||||
}
|
||||
|
||||
Object.defineProperty(this, "naturalWidth", {
|
||||
value: size[0],
|
||||
});
|
||||
Object.defineProperty(this, "naturalHeight", {
|
||||
value: size[1],
|
||||
});
|
||||
|
||||
queueMicrotask(() => {
|
||||
this.onload?.({} as Event);
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user