Files
pepperplus-ui/test/setupFlowTests.ts
JGerla b10dbae488 test: added tests for the ProgramStore
also added documentation

ref: N25B-428
2025-12-20 22:58:22 +01:00

96 lines
2.1 KiB
TypeScript

import '@testing-library/jest-dom';
import { cleanup } from '@testing-library/react';
import useFlowStore from '../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx';
if (!globalThis.structuredClone) {
globalThis.structuredClone = (obj: any) => {
return JSON.parse(JSON.stringify(obj));
};
}
// To make sure that the tests are working, it's important that you are using
// this implementation of ResizeObserver and DOMMatrixReadOnly
class ResizeObserver {
callback: globalThis.ResizeObserverCallback;
constructor(callback: globalThis.ResizeObserverCallback) {
this.callback = callback;
}
observe(target: Element) {
this.callback([{ target } as globalThis.ResizeObserverEntry], this);
}
unobserve() {}
disconnect() {}
}
class DOMMatrixReadOnly {
m22: number;
constructor(transform: string) {
const scale = transform?.match(/scale\(([1-9.])\)/)?.[1];
this.m22 = scale !== undefined ? +scale : 1;
}
}
// Only run the shim once when requested
let init = false;
export const mockReactFlow = () => {
if (init) return;
init = true;
globalThis.ResizeObserver = ResizeObserver;
// @ts-expect-error included in advised setup code provided in ReactFlow documentation
global.DOMMatrixReadOnly = DOMMatrixReadOnly;
Object.defineProperties(globalThis.HTMLElement.prototype, {
offsetHeight: {
get() {
return parseFloat(this.style.height) || 1;
},
},
offsetWidth: {
get() {
return parseFloat(this.style.width) || 1;
},
},
});
// @ts-expect-error included in advised setup code provided in ReactFlow documentation
(globalThis.SVGElement as never).prototype.getBBox = () => ({
x: 0,
y: 0,
width: 200,
height: 200,
});
};
beforeAll(() => {
useFlowStore.setState({
nodes: [],
edges: [],
past: [],
future: [],
isBatchAction: false,
edgeReconnectSuccessful: true
});
});
afterEach(() => {
cleanup();
useFlowStore.setState({
nodes: [],
edges: [],
past: [],
future: [],
isBatchAction: false,
edgeReconnectSuccessful: true
});
});