Enterprise-CMCS/macpro-mako

View on GitHub
react-app/src/hooks/UseMediaQuery.test.tsx

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { renderHook } from "@testing-library/react";
import { describe, it, vi, expect } from "vitest";
import { useMediaQuery } from "./useMediaQuery";

describe("UseMediaQuery", () => {
  // https://vitest.dev/api/vi.html#vi-stubglobal
  it("returns false if viewport doesn't match media query", () => {
    globalThis.window.matchMedia = vi.fn().mockImplementation(() => ({
      matches: false,
      addEventListener: () => {},
      removeEventListener: () => {},
    }));

    const { result } = renderHook(() => useMediaQuery("(min-width: 1024px)"));

    expect(result.current).toBe(false);
  });

  // cannot mock window being undefined
});