hyp / apps /ui /tests /setup.ts
Leon4gr45's picture
Upload folder using huggingface_hub
1dbc34b verified
/**
* Test setup file for UI unit tests
*/
import '@testing-library/jest-dom/vitest';
import { beforeEach, vi } from 'vitest';
// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock ResizeObserver
globalThis.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
})) as unknown as typeof ResizeObserver;
// Mock IntersectionObserver
globalThis.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
})) as unknown as typeof IntersectionObserver;
// Mock scrollTo
window.scrollTo = vi.fn();
// Mock localStorage with full Storage API methods used in unit tests and Zustand persist middleware
const localStorageState = new Map<string, string>();
const localStorageMock = {
getItem: vi.fn((key: string) => localStorageState.get(key) ?? null),
setItem: vi.fn((key: string, value: string) => {
localStorageState.set(key, value);
}),
removeItem: vi.fn((key: string) => {
localStorageState.delete(key);
}),
clear: vi.fn(() => {
localStorageState.clear();
}),
key: vi.fn((index: number) => Array.from(localStorageState.keys())[index] ?? null),
get length() {
return localStorageState.size;
},
};
Object.defineProperty(window, 'localStorage', {
writable: true,
value: localStorageMock,
});
beforeEach(() => {
localStorageState.clear();
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
localStorageMock.removeItem.mockClear();
localStorageMock.clear.mockClear();
localStorageMock.key.mockClear();
});