Files
WebApp/src/app/features/indicadores/indicadores-navigation.spec.ts
T
2026-10-14 13:00:54 -06:00

113 lines
7.5 KiB
TypeScript

import { periodAvailabilityProvider } from './indicadores-periodos.testing';
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { provideRouter, Router } from '@angular/router';
import { RouterTestingHarness } from '@angular/router/testing';
import { By } from '@angular/platform-browser';
import { routes } from '../../app.routes';
import { IndicadorCatalogCard } from './indicador-catalog-card';
import { IndicadorFigurePanel } from './indicador-figure-panel';
import { IndicadoresPage } from './indicadores-page';
import { IndicadorFigureCard } from './indicador-figure-card';
import { IndicadorPeriodFilters } from './indicador-period-filters';
import { PlotlyChart } from '../../shared/plotly/plotly-chart';
import { PlotlyFigure } from './indicadores.models';
import { syntheticCatalogs } from './indicadores.synthetic-fixtures';
@Component({ selector: 'app-plotly-chart', inputs: ['figure', 'label', 'showNotes'], template: '' })
class ChartStub { figure?: PlotlyFigure; }
describe('UX-04 navegación Angular', () => {
let http: HttpTestingController;
async function setup() {
vi.stubGlobal('IntersectionObserver', class { observe() {} unobserve() {} disconnect() {} });
TestBed.configureTestingModule({ providers: [provideRouter(routes), provideHttpClient(), provideHttpClientTesting(), periodAvailabilityProvider] });
TestBed.overrideComponent(IndicadorFigureCard, { remove: { imports: [PlotlyChart] }, add: { imports: [ChartStub] } });
http = TestBed.inject(HttpTestingController);
return RouterTestingHarness.create();
}
function load(domain: 'tea' | 'cacu' | 'cama', harness: RouterTestingHarness) {
http.expectOne('/' + domain + '/indicadores').flush(syntheticCatalogs[domain]);
harness.detectChanges();
const page = harness.routeDebugElement!.componentInstance as IndicadoresPage;
vi.spyOn(page as unknown as { evaluateVisibility(): void }, 'evaluateVisibility').mockImplementation(() => {});
const card = harness.routeDebugElement!.query(By.directive(IndicadorCatalogCard)).componentInstance as IndicadorCatalogCard;
if (!card.visible) { card.setVisible(true); harness.detectChanges(); }
const req = http.expectOne(r => r.url.includes('/figuras/'));
req.flush({ data: [{ y: [0, null] }], layout: {} });
return req;
}
afterEach(() => { http.verify(); vi.unstubAllGlobals(); });
it.each(['tea', 'cacu', 'cama'] as const)('navegación %s mantiene tres enlaces, estado, decoración y foco independiente', async domain => {
const harness = await setup();
await harness.navigateByUrl('/indicadores/' + domain, IndicadoresPage);
load(domain, harness); harness.detectChanges();
const links = Array.from(harness.routeNativeElement!.querySelectorAll('nav a')) as HTMLAnchorElement[];
expect(links.map(a => a.textContent?.trim())).toEqual(['TEA', 'CaCu', 'CaMa']);
expect(links.map(a => a.getAttribute('href'))).toEqual(['/indicadores/tea', '/indicadores/cacu', '/indicadores/cama']);
const selected = links.find(a => a.getAttribute('aria-current') === 'page')!;
expect(selected.getAttribute('href')).toBe('/indicadores/' + domain);
expect(links.filter(a => a.hasAttribute('aria-current'))).toHaveLength(1);
// Check the compiled CSSOM rule against the actual encapsulated element.
// jsdom's computed link decoration can remain stale after aria-current changes.
const activeRule = Array.from(document.styleSheets).flatMap(sheet => Array.from(sheet.cssRules))
.find((rule): rule is CSSStyleRule => rule instanceof CSSStyleRule
&& rule.selectorText.includes('[aria-current=') && selected.matches(rule.selectorText))!;
expect(activeRule).toBeTruthy();
expect(activeRule.style.getPropertyValue('text-decoration')).toBe('none');
expect(activeRule.style.getPropertyValue('font-weight')).toBe('700');
expect(activeRule.style.getPropertyValue('background')).toBe('var(--issste-accent)');
for (const link of links) {
expect(link.tabIndex).toBe(0);
link.focus(); expect(document.activeElement).toBe(link);
const key = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true });
link.dispatchEvent(key); expect(key.defaultPrevented).toBe(false);
expect(selected.getAttribute('aria-current')).toBe('page');
}
http.expectNone(() => true);
});
it('mantiene la ruta, restaura borradores y reconsulta la aplicación entre TEA/CaCu/CaMa', async () => {
const harness = await setup();
const tea = await harness.navigateByUrl('/indicadores/tea', IndicadoresPage);
load('tea', harness); harness.detectChanges();
const control = harness.routeDebugElement!.query(By.directive(IndicadorPeriodFilters)).componentInstance as IndicadorPeriodFilters;
control.changeMode('manual'); control.year = '2025'; control.unit = 'S1'; control.add();
(harness.routeDebugElement!.componentInstance as IndicadoresPage).applyFilters(); harness.detectChanges();
http.expectOne(r => r.url.includes('/figuras/')).flush({ data: [], layout: {} });
control.unit = 'S2'; control.add();
await harness.navigateByUrl('/indicadores/cacu', IndicadoresPage); load('cacu', harness);
await harness.navigateByUrl('/indicadores/cama', IndicadoresPage); load('cama', harness);
const restored = await harness.navigateByUrl('/indicadores/tea', IndicadoresPage);
const req = load('tea', harness);
expect(req.request.params.get('periodos')).toBe('2025-S1');
expect((harness.routeDebugElement!.componentInstance as IndicadoresPage).filters?.editors.semestre?.draft).toEqual({ mode: 'explicit', periodos: ['2025-S1', '2025-S2'] });
expect(TestBed.inject(Router).url).toBe('/indicadores/tea');
});
it('un nuevo arranque conserva la URL de entrada y reinicia indicador, tema y filtros', async () => {
let harness = await setup();
const page = await harness.navigateByUrl('/indicadores/cacu', IndicadoresPage);
load('cacu', harness); harness.detectChanges();
page.changeTema('oscuro'); harness.detectChanges();
http.expectOne(r => r.url.includes('/figuras/')).flush({ data: [], layout: {} });
const control = harness.routeDebugElement!.query(By.directive(IndicadorPeriodFilters)).componentInstance as IndicadorPeriodFilters;
control.changeTipo('mes'); control.year = '2025'; control.unit = '01'; control.add();
(harness.routeDebugElement!.componentInstance as IndicadoresPage).applyFilters(); harness.detectChanges();
http.expectOne(r => r.url.includes('/figuras/')).flush({ data: [], layout: {} });
const url = TestBed.inject(Router).url;
http.match(r => r.url.endsWith('/datos')).forEach(r => r.flush('missing', { status: 404, statusText: 'Not Found' }));
http.verify(); TestBed.resetTestingModule();
harness = await setup();
const restarted = await harness.navigateByUrl(url, IndicadoresPage);
const req = load('cacu', harness);
expect(TestBed.inject(Router).url).toBe('/indicadores/cacu');
expect((harness.routeDebugElement!.query(By.directive(IndicadorCatalogCard)).componentInstance as IndicadorCatalogCard).visible).toBe(true);
expect(restarted.tema).toBe('claro');
expect((harness.routeDebugElement!.componentInstance as IndicadoresPage).filters?.tipo).toBe('semestre');
expect((harness.routeDebugElement!.componentInstance as IndicadoresPage).filters?.editors.mes?.draft.mode).toBe('empty');
expect(req.request.params.has('periodos')).toBe(false);
expect(req.request.params.get('tema')).toBe('claro');
});
});