PromucFlow_constructor/app/client/test/setup.ts
balajisoundar bacb77a352
chore: Render below the fold widget components when browser is idle using Intersection Observer (#18747)
## Description
In order to improve the first load of the applications, now we're only
rendering the widget components that are [above the
fold](https://en.wikipedia.org/wiki/Above_the_fold#In_web_design) right
away and rendering the other widget components whenever the browser is
idle. This decreases the amount of time it takes before the first paint
on the screen.

This is getting shipped behind a feature flag!
2023-02-08 16:53:39 +05:30

90 lines
2.0 KiB
TypeScript

import { setupServer } from "msw/node";
import { handlers } from "./__mocks__/apiHandlers";
import "../src/polyfills/requestIdleCallback";
export const server = setupServer(...handlers);
window.scrollTo = jest.fn();
Element.prototype.scrollIntoView = jest.fn();
Element.prototype.scrollBy = jest.fn();
beforeAll(() => {
window.IntersectionObserver = jest
.fn()
.mockImplementation((fn: (entry: any) => any) => {
return {
observe: () => {
fn([
{
isIntersecting: true,
boundingClientRect: {
top: 64,
left: 293,
},
intersectionRect: {
width: 1296,
height: 424,
top: 64,
left: 293,
},
},
]);
},
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
window.ResizeObserver = jest.fn().mockImplementation(() => {
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
});
// establish API mocking before all tests
beforeAll(() => server.listen());
// reset any request handlers that are declared as a part of our tests
// (i.e. for testing one-time error scenarios)
afterEach(() => server.resetHandlers());
// clean up once the tests are done
afterAll(() => server.close());
// popper.js fix for jest tests
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = jest.fn();
range.getClientRects = () => {
return {
item: () => null,
length: 0,
[Symbol.iterator]: jest.fn(),
};
};
return range;
};
// jest events doesnt seem to be handling scrollTo
Element.prototype.scrollTo = () => {};
class WorkerStub {
url: string;
onmessage: CallableFunction;
constructor(stringUrl: string) {
this.url = stringUrl;
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.onmessage = () => {};
}
postMessage(msg) {
this.onmessage(msg);
}
}
window.Worker = WorkerStub as any;