UNPKG

@backstage/core-components

Version:

Core components used by Backstage plugins and apps

64 lines (61 loc) 2.08 kB
import { act } from '@testing-library/react'; const originalMatchMedia = window.matchMedia; function mockBreakpoint(options) { const mediaQueries = []; let breakpoint = "lg"; if ("initialBreakpoint" in options && options.initialBreakpoint) { breakpoint = options.initialBreakpoint; } const breakpoints = "queryBreakpointMap" in options && typeof options.queryBreakpointMap === "object" ? options.queryBreakpointMap : { "(min-width:1920px)": "xl", "(min-width:1280px)": "lg", "(min-width:960px)": "md", "(min-width:600px)": "sm", "(min-width:0px)": "xs" }; Object.defineProperty(window, "matchMedia", { writable: true, value: jest.fn().mockImplementation((mediaQueryString) => { const mediaQueryListeners = /* @__PURE__ */ new Set(); const mediaQueryList = { matches: "matches" in options ? options.matches : breakpoints[mediaQueryString] === breakpoint, media: mediaQueryString, onchange: null, addListener: jest.fn((listener) => { mediaQueryListeners.add(listener); }), removeListener: jest.fn((listener) => { mediaQueryListeners.delete(listener); }), addEventListener: jest.fn(), removeEventListener: jest.fn(), dispatchEvent: jest.fn() }; mediaQueries.push({ mediaQueryString, mediaQueryList, mediaQueryListeners }); return mediaQueryList; }) }); return { set(newBreakpoint) { breakpoint = newBreakpoint; mediaQueries.forEach( ({ mediaQueryString, mediaQueryList, mediaQueryListeners }) => { act(() => { const matches = "matches" in options ? options.matches : breakpoints[mediaQueryString] === breakpoint; mediaQueryList.matches = matches; mediaQueryListeners.forEach((listener) => listener({ matches })); }); } ); }, remove() { window.matchMedia = originalMatchMedia; } }; } export { mockBreakpoint }; //# sourceMappingURL=testUtils.esm.js.map