// @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 type { RegulatoryMappingResponse } from '../../src/domain/regulatory-mapping'; import { useRegulatoryMapping } from '../../src/features/finding-reader/use-regulatory-mapping'; afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); }); function deferred() { let resolve!: (value: T) => void; const promise = new Promise(next => { resolve = next; }); return { promise, resolve }; } function response(projectId: string, occurrenceId: string): RegulatoryMappingResponse { return { version: 1, projectId, resultProjectionId: `scanner-finding:${occurrenceId}`, generationEnabled: true, state: 'empty', decisions: [] }; } function HookProbe({ occurrenceId }: { occurrenceId: string }): ReactNode { const controller = useRegulatoryMapping('project-1', `scanner-finding:${occurrenceId}`, 'connected'); return <> {controller.response?.resultProjectionId ?? 'empty'} ; } it('does not show a previous finding mapping when it resolves after the active finding', async () => { vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); const first = deferred(); const second = deferred(); const request = vi.spyOn(AssessorApiClient.prototype, 'getRegulatoryMappings').mockImplementation((_projectId, resultProjectionId) => resultProjectionId === 'scanner-finding:occurrence-a' ? first.promise : second.promise); const container = document.createElement('div'); document.body.append(container); const root = createRoot(container); try { await act(async () => root.render()); await act(async () => container.querySelector('button')!.click()); await act(async () => root.render()); await act(async () => container.querySelector('button')!.click()); expect(request).toHaveBeenCalledTimes(2); await act(async () => { second.resolve(response('project-1', 'occurrence-b')); await Promise.resolve(); }); expect(container.textContent).toContain('scanner-finding:occurrence-b'); await act(async () => { first.resolve(response('project-1', 'occurrence-a')); await Promise.resolve(); }); expect(container.textContent).toContain('scanner-finding:occurrence-b'); expect(container.textContent).not.toContain('scanner-finding:occurrence-a'); } finally { await act(async () => root.unmount()); container.remove(); } });