All files / src/tests/testUtils mockFactory.ts

37.5% Statements 6/16
0% Branches 0/1
25% Functions 3/12
40% Lines 6/15

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 123 124 125 126 127 128 129 130 131 132                        3x                       3x                             3x               3x 1x 1x                                                                                                                                                                    
import { vi } from "vitest";
import {
  CIADataProvider,
  CIADetails,
  SecurityLevel,
} from "../../types/cia-services";
 
/**
 * Mock the DOM APIs that are commonly needed in tests
 */
export function mockDOMAPIs() {
  // Mock canvas context
  HTMLCanvasElement.prototype.getContext = vi.fn().mockReturnValue({
    canvas: { width: 200, height: 200 },
    clearRect: vi.fn(),
    beginPath: vi.fn(),
    moveTo: vi.fn(),
    lineTo: vi.fn(),
    stroke: vi.fn(),
    arc: vi.fn(),
    fill: vi.fn(),
  });
 
  // Mock window.matchMedia
  Object.defineProperty(window, "matchMedia", {
    writable: true,
    value: vi.fn().mockImplementation((query) => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: vi.fn(),
      removeListener: vi.fn(),
      addEventListener: vi.fn(),
      removeEventListener: vi.fn(),
      dispatchEvent: vi.fn(),
    })),
  });
 
  // Mock ResizeObserver
  globalThis.ResizeObserver = class ResizeObserver {
    constructor(_callback: ResizeObserverCallback) {}
    observe() {}
    unobserve() {}
    disconnect() {}
  };
 
  // Mock requestAnimationFrame
  globalThis.requestAnimationFrame = (callback: FrameRequestCallback) => {
    callback(0);
    return 0;
  };
}
 
/**
 * Create a standardized mock for Chart.js
 */
export function createChartMock() {
  const chartInstance = {
    destroy: vi.fn(),
    update: vi.fn(),
    resize: vi.fn(),
    data: { datasets: [] },
    options: {},
  };
 
  // Create the mock constructor with proper typing
  type ChartConstructor = ReturnType<typeof vi.fn> & {
    register: ReturnType<typeof vi.fn>;
    defaults: {
      font: { family: string };
      plugins: { legend: { display: boolean } };
    };
  };
 
  const ChartMock = vi.fn(() => chartInstance) as ChartConstructor;
  ChartMock.register = vi.fn();
  ChartMock.defaults = {
    font: { family: "Arial" },
    plugins: { legend: { display: false } },
  };
 
  return {
    __esModule: true,
    default: ChartMock,
  };
}
 
/**
 * Creates a test data provider with mock data for testing
 *
 * @returns A mocked CIADataProvider instance
 */
export function createTestDataProvider(): CIADataProvider {
  return {
    // Remove the getSecurityLevelDetails method that's not in the interface
    // getSecurityLevelRecommendations: vi.fn().mockResolvedValue([]),
    availabilityOptions: {} as Record<SecurityLevel, CIADetails>,
    integrityOptions: {} as Record<SecurityLevel, CIADetails>,
    confidentialityOptions: {} as Record<SecurityLevel, CIADetails>,
    roiEstimates: {} as any,
    getDefaultSecurityIcon: vi.fn(),
    getDefaultValuePoints: vi.fn(),
    getComplianceFrameworks: vi.fn().mockResolvedValue([]),
    getComplianceRequirements: vi.fn().mockResolvedValue({}),
    getBusinessImpact: vi.fn().mockResolvedValue({}),
    getSecurityMetrics: vi.fn().mockResolvedValue({}),
    getSecurityResources: vi.fn().mockResolvedValue([]),
    getSLAMetrics: vi.fn().mockResolvedValue({}),
    getCostEstimates: vi.fn().mockResolvedValue({}),
    getValueCreationMetrics: vi.fn().mockResolvedValue({}),
    getImplementationDetails: vi.fn().mockResolvedValue({}),
    getRemediationSteps: vi.fn().mockResolvedValue([]),
  };
}
 
/**
 * Creates a typed mock object with all methods mocked
 */
export function createMock<T extends object>(
  implementation: Partial<T> = {}
): T {
  return {
    ...Object.fromEntries(
      Object.getOwnPropertyNames(Object.getPrototypeOf({})).map((key) => [
        key,
        vi.fn(),
      ])
    ),
    ...implementation,
  } as T;
}