All files / app/test utils.tsx

92.85% Statements 26/28
100% Branches 2/2
90% Functions 9/10
92% Lines 23/25

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 6860x 60x     60x   60x 51x 51x 51x           60x 33x 33x 33x     60x         60x 42x     60x 7x                 60x       1x 1x     60x 2x                       60x       796x    
import { fireEvent, screen } from "@testing-library/react";
import mediaQuery from "css-mediaquery";
import { TFunction } from "i18n";
import { StringMap, TOptions } from "i18next";
import i18n from "test/i18n";
 
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(),
  });
}
 
export const t: TFunction = (
  translationKey: Parameters<TFunction>[0],
  options?: string | TOptions<StringMap> | undefined
) => {
  return i18n.t(translationKey, options);
};