All files / src/utils testUtils.tsx

29.41% Statements 5/17
0% Branches 0/4
0% Functions 0/7
29.41% Lines 5/17

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                  13x                 13x                 13x                 13x                 13x                                                                                                                                                                
import { render, RenderOptions, waitFor, screen } from '@testing-library/react';
import type { ReactElement } from 'react';
import { expect } from 'vitest';
import { SecurityLevel } from '../types/cia';
 
/**
 * Common widget test props with standard security levels
 * Used as baseline props for widget testing
 */
export const mockWidgetProps = {
  availabilityLevel: 'Moderate' as SecurityLevel,
  integrityLevel: 'Moderate' as SecurityLevel,
  confidentialityLevel: 'Moderate' as SecurityLevel,
};
 
/**
 * Props for testing high security scenario
 */
export const mockHighSecurityProps = {
  availabilityLevel: 'High' as SecurityLevel,
  integrityLevel: 'High' as SecurityLevel,
  confidentialityLevel: 'High' as SecurityLevel,
};
 
/**
 * Props for testing low security scenario
 */
export const mockLowSecurityProps = {
  availabilityLevel: 'Low' as SecurityLevel,
  integrityLevel: 'Low' as SecurityLevel,
  confidentialityLevel: 'Low' as SecurityLevel,
};
 
/**
 * Props for testing mixed security levels
 */
export const mockMixedSecurityProps = {
  availabilityLevel: 'High' as SecurityLevel,
  integrityLevel: 'Moderate' as SecurityLevel,
  confidentialityLevel: 'Low' as SecurityLevel,
};
 
/**
 * Props for testing Very High security scenario
 */
export const mockVeryHighSecurityProps = {
  availabilityLevel: 'Very High' as SecurityLevel,
  integrityLevel: 'Very High' as SecurityLevel,
  confidentialityLevel: 'Very High' as SecurityLevel,
};
 
/**
 * Custom render function with common providers
 * Currently no providers are needed, but this provides a consistent API
 * for future enhancements
 * 
 * @param ui - React element to render
 * @param options - Additional render options
 * @returns Render result from @testing-library/react
 */
export function renderWidget(
  ui: ReactElement,
  options?: RenderOptions
) {
  return render(ui, {
    ...options,
  });
}
 
/**
 * Wait for widget to finish loading
 * Checks that the loading indicator is no longer present
 * 
 * @param testId - Base test ID of the widget
 * @throws Will throw if the loading state doesn't clear within timeout
 * 
 * @example
 * await waitForWidgetLoad('widget-cost-estimation');
 */
export async function waitForWidgetLoad(testId: string) {
  await waitFor(() => {
    const loadingIndicator = screen.queryByTestId(`${testId}-loading`);
    const containerLoading = screen.queryByTestId(`widget-container-loading-container-${testId}`);
    
    // Both loading indicators should be absent
    expect(loadingIndicator).not.toBeInTheDocument();
    expect(containerLoading).not.toBeInTheDocument();
  }, { timeout: 3000 });
}
 
/**
 * Wait for widget content to be visible
 * Checks that the widget's content area is present
 * 
 * @param testId - Base test ID of the widget
 * @throws Will throw if content doesn't appear within timeout
 * 
 * @example
 * await waitForWidgetContent('widget-cost-estimation');
 */
export async function waitForWidgetContent(testId: string) {
  await waitFor(() => {
    const content = screen.queryByTestId(`${testId}-content`) || 
                   screen.queryByTestId(`widget-container-content-${testId}`);
    expect(content).toBeInTheDocument();
  }, { timeout: 3000 });
}
 
/**
 * Wait for widget error state to be visible
 * Checks that the widget's error message is present
 * 
 * @param testId - Base test ID of the widget
 * @throws Will throw if error doesn't appear within timeout
 * 
 * @example
 * await waitForWidgetError('widget-cost-estimation');
 */
export async function waitForWidgetError(testId: string) {
  await waitFor(() => {
    const error = screen.queryByTestId(`${testId}-error`) || 
                 screen.queryByTestId(`widget-container-error-${testId}`);
    expect(error).toBeInTheDocument();
  }, { timeout: 3000 });
}