All files / src/tests/testMocks ciaOptionsMocks.ts

94.11% Statements 16/17
14.28% Branches 1/7
87.5% Functions 7/8
93.33% Lines 14/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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                                                5x 5x               5x 5x 2x   5x 5x       5x       5x                                                                                                         5x                               2x                     21x               21x   21x                                                                  
// Use vi.hoisted to create mocks that can be used at the top level
import { Mock, vi } from "vitest";
import { SecurityLevel } from "../../types/cia";
 
// Define dataset type for Chart.js mock
interface ChartDataset {
  data: number[];
  label?: string;
  backgroundColor?: string | string[];
  borderColor?: string | string[];
}
 
// Define types for Chart.js mock
interface ChartMock {
  destroy: Mock;
  update: Mock;
  resize: Mock;
  data: { datasets: ChartDataset[] };
}
 
// ChartConstructor is a callable function that returns ChartMock
// with additional properties
type ChartConstructor = {
  (): ChartMock;
  register: Mock;
  defaults: {
    font: { family: string };
    plugins: { legend: { display: boolean } };
  };
};
 
// Proper hoisted mock for chart.js
const mockChartInstance = vi.hoisted(
  (): ChartMock => ({
    destroy: vi.fn(),
    update: vi.fn(),
    resize: vi.fn(),
    data: { datasets: [] },
  }),
);
 
const mockChartConstructor = vi.hoisted((): ChartConstructor => {
  const constructor = vi.fn(
    () => mockChartInstance,
  ) as unknown as ChartConstructor;
  constructor.register = vi.fn();
  constructor.defaults = {
    font: { family: "Arial" },
    plugins: { legend: { display: false } },
  };
  return constructor;
});
 
// Properly hoisted CIA options mock
const mockCIAOptionsData = vi.hoisted(() => ({
  None: {
    capex: 0,
    opex: 0,
    description: "No security controls implemented",
    technical: "No technical controls",
    businessImpact: "Critical business impact",
    bg: "#ffffff",
    text: "#000000",
    recommendations: [],
  },
  Low: {
    capex: 5,
    opex: 2,
    description: "Basic security controls",
    technical: "Basic technical controls",
    businessImpact: "High business impact",
    bg: "#f8d7da",
    text: "#721c24",
    recommendations: ["Basic recommendation"],
  },
  Moderate: {
    capex: 10,
    opex: 5,
    description: "Standard security controls",
    technical: "Standard technical controls",
    businessImpact: "Medium business impact",
    bg: "#fff3cd",
    text: "#856404",
    recommendations: ["Standard recommendation"],
  },
  High: {
    capex: 15,
    opex: 8,
    description: "Advanced security controls",
    technical: "Advanced technical controls",
    businessImpact: "Low business impact",
    bg: "#d4edda",
    text: "#155724",
    recommendations: ["Advanced recommendation"],
  },
  "Very High": {
    capex: 20,
    opex: 10,
    description: "Maximum security controls",
    technical: "Maximum technical controls",
    businessImpact: "Minimal business impact",
    bg: "#cce5ff",
    text: "#004085",
    recommendations: ["Maximum recommendation"],
  },
}));
 
const mockROIEstimates = vi.hoisted(() => ({
  NONE: { returnRate: "0%", description: "No ROI", value: "0%" },
  LOW: { returnRate: "50%", description: "Low ROI", value: "50%" },
  MODERATE: { returnRate: "150%", description: "Moderate ROI", value: "150%" },
  HIGH: { returnRate: "300%", description: "High ROI", value: "300%" },
  VERY_HIGH: {
    returnRate: "500%",
    description: "Very high ROI",
    value: "500%",
  },
}));
 
/**
 * Creates a standard Chart.js mock for testing
 */
export function createChartJsMock() {
  return {
    __esModule: true,
    default: mockChartConstructor,
  };
}
 
/**
 * Creates a standard CIA options mock for testing
 * @param customization Optional customizations to apply to the mock
 */
export function createCIAOptionsMock(customization: Record<string, any> = {}) {
  const baseMockOptions = {
    availabilityOptions: mockCIAOptionsData,
    integrityOptions: mockCIAOptionsData,
    confidentialityOptions: mockCIAOptionsData,
    roiEstimates: mockROIEstimates,
  };
 
  // Apply any customizations
  const mockOptions = { ...baseMockOptions, ...customization };
 
  return {
    __esModule: true,
    availabilityOptions: mockOptions.availabilityOptions,
    integrityOptions: mockOptions.integrityOptions,
    confidentialityOptions: mockOptions.confidentialityOptions,
    roiEstimates: mockOptions.roiEstimates,
    useCIAOptions: vi.fn().mockReturnValue({
      availabilityOptions: mockOptions.availabilityOptions,
      integrityOptions: mockOptions.integrityOptions,
      confidentialityOptions: mockOptions.confidentialityOptions,
      ROI_ESTIMATES: mockOptions.roiEstimates, // Upper case in hook return
    }),
  };
}
 
/**
 * Creates a mock security level provider for testing
 */
export function createMockSecurityLevelProvider(customLevels?: {
  availabilityLevel?: SecurityLevel;
  integrityLevel?: SecurityLevel;
  confidentialityLevel?: SecurityLevel;
}) {
  return {
    availabilityLevel: customLevels?.availabilityLevel || "Moderate",
    integrityLevel: customLevels?.integrityLevel || "Moderate",
    confidentialityLevel: customLevels?.confidentialityLevel || "Moderate",
    setAvailabilityLevel: vi.fn(),
    setIntegrityLevel: vi.fn(),
    setConfidentialityLevel: vi.fn(),
    resetLevels: vi.fn(),
  };
}