Files
WebApp/src/app/features/indicadores/indicadores-cambio-dominio.spec.ts
T

61 lines
3.7 KiB
TypeScript

// Covers switching domains with requests still pending: nothing of the domain that was left may show up in
// the one that was chosen, and the controls particular to each domain stay as they were.
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { provideRouter } from '@angular/router';
import { IndicadoresPage } from './indicadores-page';
import { IndicadoresMemory } from './indicadores-memory';
const item = (id: string, titulo: string, contrato: string) => ({ id, titulo, contrato, figuras: ['f', 'g'], tipos_periodo: ['semestre', 'mes'] });
describe('Cambio de dominio con consultas pendientes', () => {
let fixture: ReturnType<typeof TestBed.createComponent<IndicadoresPage>>, http: HttpTestingController;
beforeEach(() => {
vi.stubGlobal('IntersectionObserver', class { observe() {} unobserve() {} disconnect() {} });
TestBed.configureTestingModule({ imports: [IndicadoresPage], providers: [provideRouter([]), provideHttpClient(), provideHttpClientTesting()] });
for (const d of ['tea', 'cacu', 'cama'] as const) TestBed.inject(IndicadoresMemory).finishInitialKpiPresentation(d);
fixture = TestBed.createComponent(IndicadoresPage); http = TestBed.inject(HttpTestingController);
http.expectOne('/tea/indicadores').flush([]); fixture.detectChanges();
});
afterEach(() => { fixture.destroy(); http.match(() => true); http.verify(); vi.unstubAllGlobals(); });
const text = () => (fixture.nativeElement as HTMLElement).textContent!;
it('el catálogo pendiente del dominio que se dejó se cancela y su respuesta tardía no se dibuja', () => {
// Fails if a late answer of CaMa shows its indicators in CaCu.
fixture.componentInstance.changeDominio('cama'); fixture.detectChanges();
const cama = http.expectOne('/cama/indicadores');
fixture.componentInstance.changeDominio('cacu'); fixture.detectChanges();
expect(cama.cancelled).toBe(true);
http.expectOne('/cacu/indicadores').flush([item('cacu_x', 'Indicador de CaCu', 'C2')]); fixture.detectChanges();
expect(text()).toContain('Indicador de CaCu');
expect(() => cama.flush([item('cama_x', 'Indicador de CaMa', 'C2')])).toThrow();
fixture.detectChanges();
expect(text()).not.toContain('Indicador de CaMa');
});
it('los controles propios de cada dominio se conservan al ir y volver', () => {
// CaCu keeps its multiple selection, search and pins; CaMa does not have them; neither state contaminates the other.
const abrir = (d: 'cacu' | 'cama', n: number) => {
fixture.componentInstance.changeDominio(d); fixture.detectChanges();
http.expectOne('/' + d + '/indicadores').flush(Array.from({ length: n }, (_, i) => item(`${d}_${i}`, `${d} ${i}`, 'C2'))); fixture.detectChanges();
http.match(() => true);
};
const el = () => fixture.nativeElement as HTMLElement;
abrir('cacu', 6);
expect(el().querySelector('.catalog-search')).not.toBeNull();
expect(el().querySelectorAll('.pin-toggle').length).toBe(6);
expect(el().textContent).toContain('Intervalo de análisis');
abrir('cama', 6);
expect(el().querySelector('.catalog-search')).toBeNull();
expect(el().querySelector('.pin-toggle')).toBeNull();
expect(el().textContent).not.toContain('Buscar indicadores');
expect(el().textContent).not.toContain('cacu_');
abrir('cacu', 6);
// On the way back the page state (not only the DOM) says CaCu has its search again.
expect(fixture.componentInstance.dominio).toBe('cacu');
expect(fixture.componentInstance.searchVisible()).toBe(true);
expect(el().textContent).toContain('Intervalo de análisis');
expect(el().textContent).not.toContain('cama_');
});
});