perf(tests): use Angular recommended test config (#2632)
This commit is contained in:
@@ -1,123 +0,0 @@
|
||||
import '@angular/compiler';
|
||||
import {vi} from 'vitest';
|
||||
import '@testing-library/jest-dom/vitest';
|
||||
import 'vitest-canvas-mock';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {BrowserTestingModule, platformBrowserTesting} from '@angular/platform-browser/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 Optimus UI 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(
|
||||
BrowserTestingModule,
|
||||
platformBrowserTesting(),
|
||||
{teardown: {destroyAfterEach: true}}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user