hicity-toolkit/packages/pancake-uikit/src/testHelpers.tsx

49 lines
1.1 KiB
TypeScript

import React, { ReactNode } from "react";
import { render, RenderResult } from "@testing-library/react";
import { ThemeProvider } from "styled-components";
import { light } from "./theme";
/* eslint-disable import/prefer-default-export */
export const renderWithTheme = (component: ReactNode, theme = light): RenderResult => {
return render(<ThemeProvider theme={theme}>{component}</ThemeProvider>);
};
export const setupMockIntersectionObserver = (): void => {
/* eslint-disable class-methods-use-this */
class MockIntersectionObserver {
readonly root: Element | null;
readonly rootMargin: string;
readonly thresholds: ReadonlyArray<number>;
constructor() {
this.root = null;
this.rootMargin = "";
this.thresholds = [];
}
disconnect() {
return jest.fn;
}
observe() {
return jest.fn;
}
takeRecords(): IntersectionObserverEntry[] {
return [];
}
unobserve() {
return jest.fn;
}
}
Object.defineProperty(window, "IntersectionObserver", {
writable: true,
configurable: true,
value: MockIntersectionObserver,
});
};