mockDOMRect()
mockDOMRect(
implementation?): MockDOMRectResult
Creates a mock DOMRect constructor on globalThis for Node.js tests.
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β
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