UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

51 lines (50 loc) 1.55 kB
import { TextEncoder } from "node:util"; //#region src/utils/test-helpers.tsx global.ResizeObserver = jest.fn().mockImplementation(() => { return { observe: jest.fn(), disconnect: jest.fn(), unobserve: jest.fn() }; }); global.CSS = { escape: jest.fn(), supports: jest.fn().mockImplementation(() => { return false; }) }; global.TextEncoder = TextEncoder; /** * Required for internal usage of dialog in primer/react * this is not implemented in JSDOM, and until it is we'll need to polyfill * https://github.com/jsdom/jsdom/issues/3294 * https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom * bonus: we only want to mock browser globals in DOM (or js-dom) environments – not in SSR / node */ if (typeof document !== "undefined") { global.HTMLDialogElement.prototype.showModal = jest.fn(function mock() { this.open = true; }); global.HTMLDialogElement.prototype.close = jest.fn(function mock() { this.open = false; }); global.HTMLCanvasElement.prototype.getContext = jest.fn(); } if (global.Element.prototype.scrollIntoView === void 0) global.Element.prototype.scrollIntoView = jest.fn(); const setupMatchMedia = () => { Object.defineProperty(window, "matchMedia", { writable: true, value: jest.fn().mockImplementation((query) => ({ matches: false, media: query, onchange: null, addListener: jest.fn(), removeListener: jest.fn(), addEventListener: jest.fn(), removeEventListener: jest.fn(), dispatchEvent: jest.fn() })) }); }; //#endregion export { setupMatchMedia };