/** Pins EEG totals and visible-step polling with HTTP fixtures and a fake clock. */ import { provideHttpClient } from '@angular/common/http'; import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { provideRouter } from '@angular/router'; import { ResultStepComponent } from './result-step'; const summary = '/governance/tea/eeg/summary'; const indicators = '/reportes/indicadores'; describe('ResultStepComponent', () => { let http: HttpTestingController; beforeEach(() => { TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])] }); http = TestBed.inject(HttpTestingController); }); afterEach(() => http.verify()); /** Fails if totals disappear or status stops refreshing at five seconds or continues on a closed step. */ it('refreshes indicators while active and shows EEG and FHIR counts', async () => { vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); const hidden = vi.spyOn(document, 'hidden', 'get').mockReturnValue(false); try { const fixture = TestBed.createComponent(ResultStepComponent); fixture.componentRef.setInput('active', true); fixture.detectChanges(); http.expectOne(summary).flush({ eegs: 6, analysed: 5, valid: 3, not_valid: 1, failed: 1, metrics: 12, metrics_with_z: 10, high: 2, low: 1, fhir_eegs: 5, fhir_metrics: 12, }); http.expectOne(indicators).flush({ tea: { estado: 'en_curso', pendiente: true } }); await fixture.whenStable(); fixture.detectChanges(); expect(fixture.nativeElement.querySelectorAll('dd')).toHaveLength(11); expect(fixture.nativeElement.textContent).toContain('En curso · Pendiente: Sí'); vi.advanceTimersByTime(5000); fixture.detectChanges(); http.expectOne(indicators).flush({ tea: { estado: 'ok', pendiente: false } }); await fixture.whenStable(); fixture.detectChanges(); expect(fixture.nativeElement.textContent).toContain('Actualizado · Pendiente: No'); hidden.mockReturnValue(true); document.dispatchEvent(new Event('visibilitychange')); fixture.detectChanges(); vi.advanceTimersByTime(5000); http.expectNone(indicators); hidden.mockReturnValue(false); document.dispatchEvent(new Event('visibilitychange')); fixture.detectChanges(); http.expectOne(indicators).flush({ tea: { estado: 'ok', pendiente: false } }); fixture.componentRef.setInput('active', false); fixture.detectChanges(); vi.advanceTimersByTime(5000); http.expectNone(indicators); fixture.destroy(); } finally { hidden.mockRestore(); vi.useRealTimers(); } }); /** Fails if failures hide the refresh control or refreshing skips one of the two endpoints. */ it('shows load failures and lets both requests be retried', async () => { const hidden = vi.spyOn(document, 'hidden', 'get').mockReturnValue(false); try { const fixture = TestBed.createComponent(ResultStepComponent); fixture.componentRef.setInput('active', true); fixture.detectChanges(); http.expectOne(summary).flush(null, { status: 500, statusText: 'Server Error' }); http.expectOne(indicators).flush(null, { status: 500, statusText: 'Server Error' }); await fixture.whenStable(); fixture.detectChanges(); expect(fixture.nativeElement.querySelectorAll('[role=alert]')).toHaveLength(2); fixture.nativeElement.querySelector('button').click(); fixture.detectChanges(); http.expectOne(summary).flush({ eegs: 0 }); http.expectOne(indicators).flush({ tea: { estado: 'sin_correr', pendiente: true } }); await fixture.whenStable(); fixture.detectChanges(); expect(fixture.nativeElement.textContent).toContain('Todavía no hay EEGs registrados.'); } finally { hidden.mockRestore(); } }); });