Files
suaveplan/packages/foundation/data/react-packer/tests/setup/polyfills.ts
2026-05-27 23:47:53 +01:00

51 lines
1.6 KiB
TypeScript

// React-tier polyfills. Base webcrypto comes from `@suaveplan/testing/polyfills`;
// this file adds the `IS_REACT_ACT_ENVIRONMENT` flag React DOM looks for when
// state updates happen outside an explicit `act(...)` wrapper, plus a minimal
// `URL.createObjectURL`/`revokeObjectURL` shim because happy-dom does not
// implement them and the underlying `PackedResourceManager` calls them on
// every load.
interface ReactActEnvWindow {
IS_REACT_ACT_ENVIRONMENT?: boolean;
}
const win = globalThis as unknown as ReactActEnvWindow;
if (win.IS_REACT_ACT_ENVIRONMENT === undefined) {
win.IS_REACT_ACT_ENVIRONMENT = true;
}
interface ObjectUrlGlobals {
URL: {
createObjectURL?: (obj: Blob) => string;
revokeObjectURL?: (url: string) => void;
};
}
const urlGlobals = globalThis as unknown as ObjectUrlGlobals;
let counter = 0;
if (typeof urlGlobals.URL.createObjectURL !== "function") {
urlGlobals.URL.createObjectURL = (_blob: Blob): string => {
counter += 1;
return `blob:react-packer/${counter}`;
};
}
if (typeof urlGlobals.URL.revokeObjectURL !== "function") {
urlGlobals.URL.revokeObjectURL = (_url: string): void => {
// no-op shim — happy-dom doesn't track blob URLs
};
}
interface FontShim {
fonts?: {
load?: (spec: string) => Promise<FontFace[]>;
add?: (font: FontFace) => unknown;
};
}
const docShim = (globalThis as { document?: FontShim }).document;
if (docShim !== undefined && docShim.fonts === undefined) {
docShim.fonts = {
load: async (_spec: string): Promise<FontFace[]> => [],
add: (_font: FontFace): unknown => undefined,
};
}