@shopify/jest-dom-mocks
Version:
Jest mocking utilities for working with the DOM
65 lines (62 loc) • 2.15 kB
JavaScript
import AnimationFrame from './animation-frame.mjs';
import RequestIdleCallback from './request-idle-callback.mjs';
import Clock from './clock.mjs';
import fetch from './fetch.mjs';
export { default as fetch } from './fetch.mjs';
import Location from './location.mjs';
import MatchMedia from './match-media.mjs';
export { mediaQueryList } from './match-media.mjs';
import Storage from './storage.mjs';
import Timer from './timer.mjs';
import Performance from './user-timing.mjs';
import IntersectionObserverMock from './intersection-observer.mjs';
import Promise$1 from './promise.mjs';
import Dimension from './dimension.mjs';
import { Connection } from './connection.mjs';
const connection = new Connection();
const animationFrame = new AnimationFrame();
const requestIdleCallback = new RequestIdleCallback();
const clock = new Clock();
const location = new Location();
const matchMedia = new MatchMedia();
const localStorage = new Storage();
const sessionStorage = new Storage();
const timer = new Timer();
const userTiming = new Performance();
const intersectionObserver = new IntersectionObserverMock();
const promise = new Promise$1();
const dimension = new Dimension();
function installMockStorage() {
if (typeof window !== 'undefined') {
Object.defineProperties(window, {
localStorage: {
value: localStorage
},
sessionStorage: {
value: sessionStorage
}
});
}
}
const mocksToEnsureReset = {
clock,
location,
timer,
promise,
animationFrame,
fetch,
matchMedia,
userTiming,
intersectionObserver,
connection
};
function ensureMocksReset() {
for (const mockName of Object.keys(mocksToEnsureReset)) {
if (mocksToEnsureReset[mockName].isMocked()) {
throw new Error(`You did not reset the mocked ${mockName}. Make sure to call ${mockName}.restore() after your tests have run.`);
}
}
localStorage.restore();
sessionStorage.restore();
}
export { animationFrame, clock, connection, dimension, ensureMocksReset, installMockStorage, intersectionObserver, localStorage, location, matchMedia, promise, requestIdleCallback, sessionStorage, timer, userTiming };