All files / app/test utils.tsx

87.5% Statements 21/24
100% Branches 2/2
88.88% Functions 8/9
86.36% Lines 19/22

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 5841x 41x   52x 52x 52x 52x           29x 29x 29x 29x               39x 39x     6x 6x                 1x       1x 1x     2x 2x                      
import { fireEvent, screen } from "@testing-library/react";
import mediaQuery from "css-mediaquery";
 
export function addDefaultUser(userId?: number) {
  window.localStorage.setItem("auth.authenticated", JSON.stringify(true));
  window.localStorage.setItem("auth.jailed", JSON.stringify(false));
  window.localStorage.setItem(
    "auth.userId",
    JSON.stringify(userId ?? defaultUser.userId)
  );
}
 
export async function assertErrorAlert(message: string) {
  const errorAlert = await screen.findByRole("alert");
  expect(errorAlert).toBeVisible();
  expect(errorAlert).toHaveTextContent(message);
}
 
export function assertFieldVisibleWithValue(field: HTMLElement, value: string) {
  expect(field).toBeVisible();
  expect(field).toHaveValue(value);
}
 
export function mockConsoleError() {
  jest.spyOn(console, "error").mockReturnValue(undefined);
}
 
export function wait(milliSeconds: number) {
  return new Promise<void>((resolve) => setTimeout(resolve, milliSeconds));
}
 
//eslint-disable-next-line
export type MockedService<T extends (...args: any) => any> = jest.Mock<
  ReturnType<T>,
  Parameters<T>
>;
 
export function keyPress(
  element: Window | Document | Node | Element,
  keyEvent: { code: string; key: string }
) {
  fireEvent.keyDown(element, keyEvent);
  fireEvent.keyUp(element, keyEvent);
}
 
export function createMatchMedia(width: number) {
  return (query: string) => ({
    matches: mediaQuery.match(query, { width }),
    media: "screen",
    addListener: jest.fn(),
    removeListener: jest.fn(),
    onchange: jest.fn(),
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    dispatchEvent: jest.fn(),
  });
}