MCPcopy Create free account
hub / github.com/bvaughn/react-window / mockLayout

Function mockLayout

lib/utils/test/mockLayout.ts:17–54  ·  view source on GitHub ↗
(getLayout: GetLayout)

Source from the content-addressed store, hash-verified

15
16// jsdom doesn't implement layout; this simulates element geometry (in client coordinates) for tests
17export function mockLayout(getLayout: GetLayout) {
18 const prototype = HTMLElement.prototype;
19
20 vi.spyOn(prototype, "getBoundingClientRect").mockImplementation(
21 function getBoundingClientRect(this: HTMLElement) {
22 const layout = getLayout(this);
23 return layout
24 ? new DOMRect(layout.left, layout.top, layout.width, layout.height)
25 : new DOMRect(0, 0, 0, 0);
26 }
27 );
28
29 const properties = {
30 clientHeight: (layout: Layout) => layout.clientHeight ?? layout.height,
31 clientWidth: (layout: Layout) => layout.clientWidth ?? layout.width,
32 scrollHeight: (layout: Layout) =>
33 layout.scrollHeight ?? layout.clientHeight ?? layout.height,
34 scrollWidth: (layout: Layout) =>
35 layout.scrollWidth ?? layout.clientWidth ?? layout.width
36 } as const;
37
38 for (const [name, getValue] of Object.entries(properties)) {
39 Object.defineProperty(prototype, name, {
40 configurable: true,
41 get(this: HTMLElement) {
42 const layout = getLayout(this);
43 return layout ? getValue(layout) : 0;
44 }
45 });
46 }
47
48 onTestFinished(() => {
49 for (const name of Object.keys(properties)) {
50 // @ts-expect-error Remove the override so Element.prototype's getter is used again
51 delete prototype[name];
52 }
53 });
54}
55
56// Simulates scrolling the window (jsdom doesn't implement window.scrollTo)
57export function mockWindowScroll({

Callers 4

setupFunction · 0.90
setupFunction · 0.90

Calls

no outgoing calls

Tested by 2

setupFunction · 0.72
setupFunction · 0.72