UNPKG

1.96 kBPlain TextView Raw
1import AnimationFrame from './animation-frame';
2import RequestIdleCallback from './request-idle-callback';
3import Clock from './clock';
4import fetch from './fetch';
5import Location from './location';
6import MatchMedia from './match-media';
7import Storage from './storage';
8import Timer from './timer';
9import UserTiming from './user-timing';
10import IntersectionObserver from './intersection-observer';
11import Promise from './promise';
12import Dimension from './dimension';
13import {Connection} from './connection';
14
15export const connection = new Connection();
16
17export const animationFrame = new AnimationFrame();
18export const requestIdleCallback = new RequestIdleCallback();
19
20export const clock = new Clock();
21
22export {fetch};
23
24export const location = new Location();
25
26export const matchMedia = new MatchMedia();
27export {mediaQueryList} from './match-media';
28
29export const localStorage = new Storage();
30export const sessionStorage = new Storage();
31
32export const timer = new Timer();
33export const userTiming = new UserTiming();
34export const intersectionObserver = new IntersectionObserver();
35export const promise = new Promise();
36
37export const dimension = new Dimension();
38
39export function installMockStorage() {
40 if (typeof window !== 'undefined') {
41 Object.defineProperties(window, {
42 localStorage: {
43 value: localStorage,
44 },
45 sessionStorage: {
46 value: sessionStorage,
47 },
48 });
49 }
50}
51
52const mocksToEnsureReset = {
53 clock,
54 location,
55 timer,
56 promise,
57 animationFrame,
58 fetch,
59 matchMedia,
60 userTiming,
61 intersectionObserver,
62 connection,
63};
64
65export function ensureMocksReset() {
66 for (const mockName of Object.keys(mocksToEnsureReset)) {
67 if (mocksToEnsureReset[mockName].isMocked()) {
68 throw new Error(
69 `You did not reset the mocked ${mockName}. Make sure to call ${mockName}.restore() after your tests have run.`,
70 );
71 }
72 }
73
74 localStorage.restore();
75 sessionStorage.restore();
76}