Files
grimmory/frontend/src/test-setup.ts
T
brios 85e8d235b6 fix(dashboard): improve performance with OnPush change detection (#440)
* fix(dashboard): improve performance with OnPush change detection

* fix(dashboard): refactor book card component for improved readability and performance

* fix(tests): mock matchMedia for improved test compatibility

* fix(tests): matchMedia mock for better CI compatibility and support

* fix(dashboard): add clearAll method to BookBrowserScrollService and update tests

* fix(dashboard): improve performance with content visibility and refactor styles

* fix(dashboard): improve scroll performance with estimated total height and refactor layout

* fix(tests): add mock for getCurrentUser in userService for improved test coverage

* fix(dashboard): implement infinite scroll for book loading and scroll restoration

* fix(tests): fix lint error

* fix(tests): mock ResizeObserver and IntersectionObserver

* fix(tests): mock ResizeObserver and IntersectionObserver (again)

* fix(dashboard): improve accessibility and performance by updating aria attributes and optimizing animations

* fix(dashboard): improve submenu accessibility and optimize virtual scroller performance

* fix(dashboard): clean up imports and improve component structure

* fix(dashboard): correct mobile check in author browser component

* fix(dashboard): add OnDestroy lifecycle hook to BookBrowserComponent
2026-04-22 17:27:11 -04:00

124 lines
3.1 KiB
TypeScript

import '@angular/compiler';
import {vi} from 'vitest';
import '@testing-library/jest-dom/vitest';
import 'vitest-canvas-mock';
import {TestBed} from '@angular/core/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
declare global {
var __ANGULAR_TESTBED_INITIALIZED__: boolean | undefined;
}
class MockResizeObserver {
observe(): undefined { return undefined; }
unobserve(): undefined { return undefined; }
disconnect(): undefined { return undefined; }
}
class MockIntersectionObserver {
readonly root = null;
readonly rootMargin = '';
readonly thresholds = [0];
readonly scrollMargin = '';
observe(): undefined { return undefined; }
unobserve(): undefined { return undefined; }
disconnect(): undefined { return undefined; }
takeRecords(): IntersectionObserverEntry[] {
return [];
}
}
// Standard mock for matchMedia to support PrimeNG and other UI libraries in JSDOM
const matchMediaMock = (query: string): MediaQueryList => ({
matches: false,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(), // Deprecated but still used by some libraries
removeListener: vi.fn(), // Deprecated
dispatchEvent: vi.fn(),
});
Object.defineProperty(globalThis, 'matchMedia', {
writable: true,
configurable: true,
enumerable: true,
value: matchMediaMock,
});
Object.defineProperty(window, 'matchMedia', {
writable: true,
configurable: true,
enumerable: true,
value: matchMediaMock,
});
if (process.env['GITHUB_ACTIONS']) {
console.log('Test setup: Applied robust window.matchMedia mock');
}
if (!globalThis.ResizeObserver) {
globalThis.ResizeObserver = MockResizeObserver as typeof ResizeObserver;
}
if (!globalThis.IntersectionObserver) {
globalThis.IntersectionObserver = MockIntersectionObserver as typeof IntersectionObserver;
}
if (!globalThis.PointerEvent) {
globalThis.PointerEvent = MouseEvent as typeof PointerEvent;
}
if (!globalThis.DOMRect) {
globalThis.DOMRect = class DOMRectMock implements DOMRect {
bottom = 0;
height = 0;
left = 0;
right = 0;
top = 0;
width = 0;
x = 0;
y = 0;
toJSON(): Record<string, number> {
return {
bottom: this.bottom,
height: this.height,
left: this.left,
right: this.right,
top: this.top,
width: this.width,
x: this.x,
y: this.y
};
}
} as typeof DOMRect;
}
if (!window.scrollTo) {
window.scrollTo = () => undefined;
}
if (!window.HTMLElement.prototype.scrollIntoView) {
window.HTMLElement.prototype.scrollIntoView = () => undefined;
}
if (!window.URL.createObjectURL) {
window.URL.createObjectURL = () => 'blob:mock-url';
}
if (!window.URL.revokeObjectURL) {
window.URL.revokeObjectURL = () => undefined;
}
// Only initialize if not already initialized
if (!globalThis.__ANGULAR_TESTBED_INITIALIZED__) {
globalThis.__ANGULAR_TESTBED_INITIALIZED__ = true;
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{teardown: {destroyAfterEach: true}}
);
}