47 lines
2.1 KiB
TypeScript
47 lines
2.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { act, type ReactNode } from 'react';
|
|
import { createRoot } from 'react-dom/client';
|
|
import { afterEach, expect, it, vi } from 'vitest';
|
|
import { AssessorApiClient } from '../../src/api/assessor-api-client';
|
|
import { useOptionalApp } from '../../src/app/AppProvider';
|
|
import type { RegulatoryMappingResponse } from '../../src/compliance/regulatory-mapping';
|
|
import { useRegulatoryMapping } from '../../src/compliance/ui/use-regulatory-mapping';
|
|
|
|
vi.mock('../../src/app/AppProvider', () => ({ useOptionalApp: vi.fn() }));
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
const response: RegulatoryMappingResponse = {
|
|
version: 1, projectId: 'dummy-project', resultProjectionId: 'scanner-finding:dummy', generationEnabled: true, state: 'empty', decisions: []
|
|
};
|
|
|
|
function Probe(): ReactNode {
|
|
const controller = useRegulatoryMapping('dummy-project', 'scanner-finding:dummy', 'connected');
|
|
return <button type="button" onClick={() => void controller.start()}>Map ISO controls</button>;
|
|
}
|
|
|
|
// The mapping history names where a single mapping came from, so `Map ISO controls` sends the shown timeslot.
|
|
it('sends the timeslot the Finding Reader shows with a single mapping', async () => {
|
|
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
|
|
vi.mocked(useOptionalApp).mockReturnValue({ activeAssessmentIds: ['dummy-assessment'], activeScanRunIds: ['dummy-scan-run'] } as never);
|
|
vi.spyOn(AssessorApiClient.prototype, 'getRegulatoryMappings').mockResolvedValue(response);
|
|
const generate = vi.spyOn(AssessorApiClient.prototype, 'generateRegulatoryMappings').mockResolvedValue(response);
|
|
const container = document.createElement('div');
|
|
document.body.append(container);
|
|
const root = createRoot(container);
|
|
try {
|
|
await act(async () => root.render(<Probe/>));
|
|
await act(async () => container.querySelector('button')!.click());
|
|
expect(generate).toHaveBeenCalledWith('dummy-project', {
|
|
resultProjectionId: 'scanner-finding:dummy', origin: { assessmentIds: ['dummy-assessment'], scanRunIds: ['dummy-scan-run'] }
|
|
});
|
|
} finally {
|
|
await act(async () => root.unmount());
|
|
container.remove();
|
|
}
|
|
});
|