All files / src/utils TestUtils.tsx

85.18% Statements 23/27
50% Branches 1/2
76.92% Functions 10/13
85.18% Lines 23/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 8873x     7x     7x             73x 1x     73x                     73x     1x     73x   1x 1x 1x 1x     1x     73x 61x   1x         61x   1262x                         73x   1x 1x       1x                        
export const mockWindowLocation = (): void => {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  delete window.location;
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  window.location = {
    assign: jest.fn(),
    reload: jest.fn(),
    replace: jest.fn(),
  };
};
 
export const mockWindowOpen = (): void => {
  window.open = jest.fn();
};
 
export const mockCreateObjectURL = (): void => {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  delete window.URL;
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  window.URL = {
    createObjectURL: jest.fn(),
  };
};
 
export const clearWindowOpenMock = (): void => {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  //@ts-ignore
  window.open.mockClear();
};
 
export const mockIntersectionObserver = (): void => {
  // IntersectionObserver isn't available in test environment
  const mockIntersectionObserver = jest.fn();
  mockIntersectionObserver.mockReturnValue({
    observe: () => null,
    unobserve: () => null,
    disconnect: () => null
  });
  window.IntersectionObserver = mockIntersectionObserver;
};
 
export const redefineMatchMedia = (): void => {
  Object.defineProperty(window, 'URL', {
    writable: true,
    value: jest.fn().mockImplementation(() => ({
      createObjectURL: jest.fn(),
    })),
  });
 
  Object.defineProperty(window, 'matchMedia', {
    writable: true,
    value: jest.fn().mockImplementation(query => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: jest.fn(), // deprecated
      removeListener: jest.fn(), // deprecated
      addEventListener: jest.fn(),
      removeEventListener: jest.fn(),
      dispatchEvent: jest.fn(),
    })),
  });
};
 
export const listenRejection = (): void => {
  // In Node v7 unhandled promise rejections will terminate the process
  Eif (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
    process.on('unhandledRejection', reason => {
      throw reason;
    });
    // Avoid memory leak by adding too many listeners
    process.env.LISTENING_TO_UNHANDLED_REJECTION = "true";
  }
 
  // window.addEventListener('unhandledrejection', event => {
  //   // Prevent error output on the console:
  //   event.preventDefault();
  //   console.log('Reason: ' + event.reason.stack);
  // });
  // window.addEventListener('rejectionhandled', event => {
  //   console.log('REJECTIONHANDLED' + event.reason);
  // });
};