Aller au contenu principal

mockDOMRect()

mockDOMRect(implementation?): MockDOMRectResult

Creates a mock DOMRect constructor on globalThis for Node.js tests.

remarque

Always call restore() for cleanup. Auto-restored after each test if forgotten.


Parameters

implementation?

{(x?, y?, width?, height?): DOMRect; prototype: DOMRect; fromRect: DOMRect; }

Optional custom DOMRect implementation.

prototype: DOMRect

fromRect


Returns: MockDOMRectResult

Object containing the mock DOMRect and restore function.


Since

2.0.0


Example

const { DOMRect, restore } = mockDOMRect();
const rect = new DOMRect(0, 0, 100, 100);
expect(rect.width).toBe(100);
restore();

Use Cases

Simulate element dimensions 📌

Mock DOMRect for testing layout logic or geometry calculations in Node.js environments. Useful for testing components relying on getBoundingClientRect().

const { DOMRect, restore } = mockDOMRect();
const rect = new DOMRect(0, 0, 100, 50);
expect(rect.width).toBe(100);
restore();

Test intersection calculations

Verify collision detection or overlap logic.

const { DOMRect, restore } = mockDOMRect();
const rectA = new DOMRect(0, 0, 100, 100);
const rectB = new DOMRect(50, 50, 100, 100);

expect(rectsIntersect(rectA, rectB)).toBe(true);
restore();

MockDOMRectResult

Interface

Result of mockDOMRect().


Since

2.0.0


Properties

DOMRect: {(x?, y?, width?, height?): DOMRect; prototype: DOMRect; fromRect: DOMRect; }

x?: number
y?: number
width?: number
height?: number
Returns: DOMRect

prototype: DOMRect

fromRect(): MDN Reference

The fromRect() static method of the object with a given location and dimensions.

other?: DOMRectInit
Returns: DOMRect

restore(): () => void

Returns

void