Skip to main content

mockDOMRect()

mockDOMRect(implementation?): MockDOMRectResult

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

note

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