All files / src/tests/utils render.tsx

43.47% Statements 10/23
0% Branches 0/1
16.66% Functions 2/12
42.1% Lines 8/19

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                  1x             1x     1x       1x       1x       1x         1x                   1x                                                                                                                                                      
/**
 * React component rendering utilities for testing.
 *
 * This file provides specialized render functions for CIA Compliance Manager
 * components with appropriate context providers.
 *
 * @packageDocumentation
 */
 
import {
  fireEvent,
  render,
  RenderOptions,
  screen,
  waitFor,
} from "@testing-library/react";
import React from "react";
import { vi } from "vitest";
import { SecurityLevel } from "../../types/cia";
import { createSecurityLevelProps } from "./security";
 
type SecurityContext = ReturnType<typeof createSecurityLevelProps>;
 
const securityContextMock = vi.hoisted(() => ({
  current: undefined as SecurityContext | undefined,
}));
 
const themeMock = vi.hoisted(() => ({
  current: "light" as "light" | "dark",
}));
 
vi.mock("../../hooks/useSecurityLevelState", () => ({
  __esModule: true,
  useSecurityLevelState: () => securityContextMock.current,
}));
 
vi.mock("../../hooks/useTheme", () => ({
  __esModule: true,
  useTheme: () => ({
    theme: themeMock.current,
    toggleTheme: vi.fn(),
  }),
}));
 
// Import missing react-router-dom or create a mock if not needed
// Mocking BrowserRouter for testing
const BrowserRouter = ({ children }: { children: React.ReactNode }) => (
  <div>{children}</div>
);
 
/**
 * Renders a component with Router context
 * @param ui Component to render
 * @param options Render options
 * @returns Testing-library render result
 */
export function renderWithRouter(
  ui: React.ReactElement,
  options?: RenderOptions
) {
  return render(ui, {
    wrapper: ({ children }) => <BrowserRouter>{children}</BrowserRouter>,
    ...options,
  });
}
 
/**
 * Renders a component with security context
 * @param ui Component to render
 * @param securityLevels Security levels to inject
 * @param options Render options
 * @returns Testing-library render result
 */
export function renderWithSecurityContext(
  ui: React.ReactElement,
  securityLevels?: {
    availabilityLevel?: SecurityLevel;
    integrityLevel?: SecurityLevel;
    confidentialityLevel?: SecurityLevel;
  },
  options?: RenderOptions
) {
  const securityContext = createSecurityLevelProps(
    securityLevels?.availabilityLevel,
    securityLevels?.integrityLevel,
    securityLevels?.confidentialityLevel
  );
 
  securityContextMock.current = securityContext;
 
  return render(ui, options);
}
 
/**
 * Renders a component with theme context
 * @param ui Component to render
 * @param theme Theme to use
 * @param options Render options
 * @returns Testing-library render result
 */
export function renderWithTheme(
  ui: React.ReactElement,
  theme: "light" | "dark" = "light",
  options?: RenderOptions
) {
  themeMock.current = theme;
 
  return render(ui, options);
}
 
/**
 * Renders a component with basic test configuration
 */
export function renderWithTestConfig(ui: React.ReactElement) {
  return render(ui, {
    // Add any global test configuration here
  });
}
 
// Re-export from @testing-library/react for convenience
export { fireEvent, render, screen, waitFor };