Files
2026-07-13 13:20:22 +08:00

100 lines
2.6 KiB
TypeScript

/**
* Vitest DOM Test Setup
* Configuration for React component and hook tests using jsdom
*/
import '@testing-library/jest-dom/vitest';
// Make this a module
// Extend global types for testing
interface ElectronAPI {
emit: () => Promise<void>;
on: () => void;
windowControls: {
minimize: () => Promise<void>;
maximize: () => Promise<void>;
unmaximize: () => Promise<void>;
close: () => Promise<void>;
isMaximized: () => Promise<boolean>;
onMaximizedChange: () => () => void;
};
}
declare global {
// eslint-disable-next-line no-var
var electronAPI: ElectronAPI;
}
const noop = () => Promise.resolve();
// Mock Electron APIs for testing
const windowControlsMock = {
minimize: noop,
maximize: noop,
unmaximize: noop,
close: noop,
isMaximized: () => Promise.resolve(false),
onMaximizedChange: (): (() => void) => () => void 0,
};
global.electronAPI = {
emit: noop,
on: () => {},
windowControls: windowControlsMock,
};
if (typeof window !== 'undefined') {
(window as unknown as { electronAPI: ElectronAPI }).electronAPI = global.electronAPI;
}
// Mock ResizeObserver for Virtuoso
class ResizeObserverMock {
observe() {}
unobserve() {}
disconnect() {}
}
global.ResizeObserver = ResizeObserverMock;
// Mock IntersectionObserver
class IntersectionObserverMock {
observe() {}
unobserve() {}
disconnect() {}
}
global.IntersectionObserver = IntersectionObserverMock as unknown as typeof IntersectionObserver;
// Mock requestAnimationFrame
global.requestAnimationFrame = (callback: FrameRequestCallback) => {
return setTimeout(() => callback(Date.now()), 0) as unknown as number;
};
global.cancelAnimationFrame = (id: number) => {
clearTimeout(id);
};
// Mock scrollTo
Element.prototype.scrollTo = () => {};
Element.prototype.scrollIntoView = () => {};
// Mock localStorage (not always available in jsdom)
if (typeof globalThis.localStorage === 'undefined' || typeof globalThis.localStorage?.clear !== 'function') {
const store = new Map<string, string>();
const localStorageMock = {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => store.set(key, String(value)),
removeItem: (key: string) => store.delete(key),
clear: () => store.clear(),
get length() {
return store.size;
},
key: (index: number) => [...store.keys()][index] ?? null,
};
Object.defineProperty(globalThis, 'localStorage', { value: localStorageMock, writable: true });
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'localStorage', { value: localStorageMock, writable: true });
}
}