Files
portifolio-evandrodevbr/jest.setup.ts

101 lines
2.8 KiB
TypeScript

import "@testing-library/jest-dom";
if (typeof global.ResizeObserver === "undefined") {
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
global.ResizeObserver = ResizeObserver as typeof global.ResizeObserver;
}
// Mock IntersectionObserver if not available in JSDOM
if (typeof global.IntersectionObserver === "undefined") {
class IntersectionObserver {
root: Element | Document | null = null;
rootMargin: string = "";
thresholds: ReadonlyArray<number> = [];
private callback: IntersectionObserverCallback;
private elements: Set<Element> = new Set();
constructor(
callback: IntersectionObserverCallback,
options?: IntersectionObserverInit,
) {
this.callback = callback;
this.root = options?.root || null;
this.rootMargin = options?.rootMargin || "";
const threshold = options?.threshold ?? 0;
this.thresholds = Array.isArray(threshold) ? threshold : [threshold];
}
observe(target: Element) {
this.elements.add(target);
setTimeout(() => {
this.callback(
[
{
target,
isIntersecting: true,
intersectionRatio: 1,
boundingClientRect: target.getBoundingClientRect(),
intersectionRect: target.getBoundingClientRect(),
rootBounds:
this.root instanceof Element
? this.root.getBoundingClientRect()
: null,
time: Date.now(),
} as IntersectionObserverEntry,
],
this,
);
}, 0);
}
unobserve(target: Element) {
this.elements.delete(target);
}
disconnect() {
this.elements.clear();
}
takeRecords(): IntersectionObserverEntry[] {
return [];
}
}
global.IntersectionObserver =
IntersectionObserver as typeof global.IntersectionObserver;
}
// Mock PointerEvent if not available in JSDOM
if (typeof global.PointerEvent === "undefined") {
class PointerEvent extends MouseEvent {
pointerId: number;
pointerType: string;
isPrimary: boolean;
constructor(type: string, params: PointerEventInit = {}) {
super(type, params);
this.pointerId = params.pointerId || 0;
this.pointerType = params.pointerType || "mouse";
this.isPrimary = params.isPrimary || false;
}
}
global.PointerEvent = PointerEvent as typeof global.PointerEvent;
}
// Mock window.matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});