// @vitest-environment jsdom import { act } from 'react'; import { createRoot, type Root } from 'react-dom/client'; import { afterEach, beforeEach, expect, it, vi } from 'vitest'; import { AssessorApiClient } from '../../src/api/assessor-api-client'; import { ENGINE_PREFERENCES_KEY, resetEnginePreferencesForTests } from '../../src/app/engine-preferences'; import { EnginesControl } from '../../src/components/EnginesControl'; // Importing the Data Hub ISO tile installs the link, as it does when the app loads. import '../../src/compliance/ui/IsoCorpusLibrary'; let host: HTMLDivElement; let root: Root; beforeEach(() => { vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); window.localStorage.removeItem(ENGINE_PREFERENCES_KEY); resetEnginePreferencesForTests(); host = document.createElement('div'); document.body.append(host); root = createRoot(host); }); afterEach(async () => { await act(async () => root.unmount()); host.remove(); window.localStorage.removeItem(ENGINE_PREFERENCES_KEY); resetEnginePreferencesForTests(); vi.restoreAllMocks(); vi.unstubAllGlobals(); }); it('switches automatic ISO mapping off when the Compliance Engine is unticked in the Engines popup, and not back on', async () => { const setAutoMapping = vi.spyOn(AssessorApiClient.prototype, 'setIsoAutoMapping') .mockResolvedValue({ version: 1, configured: true, active: null, job: null, autoMapping: { enabled: false } }); await act(async () => root.render()); await act(async () => host.querySelector('.engines-button')!.click()); const compliance = () => [...document.querySelectorAll('.engines-option')] .find(option => option.textContent?.includes('Compliance Engine'))!.querySelector('input')!; await act(async () => { compliance().click(); await Promise.resolve(); }); expect(compliance().checked).toBe(false); expect(setAutoMapping).toHaveBeenCalledTimes(1); expect(setAutoMapping).toHaveBeenCalledWith(false); await act(async () => { compliance().click(); await Promise.resolve(); }); expect(compliance().checked).toBe(true); expect(setAutoMapping).toHaveBeenCalledTimes(1); }); it('leaves automatic mapping alone when another engine is switched off', async () => { const setAutoMapping = vi.spyOn(AssessorApiClient.prototype, 'setIsoAutoMapping') .mockResolvedValue({ version: 1, configured: true, active: null, job: null }); await act(async () => root.render()); await act(async () => host.querySelector('.engines-button')!.click()); const context = [...document.querySelectorAll('.engines-option')] .find(option => option.textContent?.includes('Context Engine'))!.querySelector('input')!; await act(async () => { context.click(); await Promise.resolve(); }); expect(setAutoMapping).not.toHaveBeenCalled(); });