feat(indicadores): implementar catalogo expandible UX-05B
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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 { By } from '@angular/platform-browser';
|
||||
import { IndicadoresPage } from './indicadores-page';
|
||||
import { IndicadorCatalogCard } from './indicador-catalog-card';
|
||||
import { PlotlyChart } from '../../shared/plotly/plotly-chart';
|
||||
import { PLOTLY_LOADER } from '../../shared/plotly/plotly-runtime';
|
||||
|
||||
describe('UX-05B ciclo de vida con componentes Plotly y runtime simulado', () => {
|
||||
let fixture: ReturnType<typeof TestBed.createComponent<IndicadoresPage>>;
|
||||
let http: HttpTestingController;
|
||||
const runtime = { react: vi.fn(), purge: vi.fn(), Plots: { resize: vi.fn() } };
|
||||
const disconnects: ReturnType<typeof vi.fn>[] = [];
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks(); disconnects.length = 0;
|
||||
runtime.react.mockResolvedValue(undefined); runtime.Plots.resize.mockResolvedValue(undefined);
|
||||
vi.stubGlobal('ResizeObserver', class {
|
||||
disconnect = vi.fn(); observe = vi.fn();
|
||||
constructor() { disconnects.push(this.disconnect); }
|
||||
});
|
||||
TestBed.configureTestingModule({ imports: [IndicadoresPage], providers: [provideRouter([]), provideHttpClient(), provideHttpClientTesting(),
|
||||
{ provide: PLOTLY_LOADER, useValue: () => Promise.resolve(runtime) }] });
|
||||
fixture = TestBed.createComponent(IndicadoresPage); http = TestBed.inject(HttpTestingController);
|
||||
http.expectOne('/tea/indicadores').flush([{ id: 'i', titulo: 'I', contrato: 'C1', figuras: ['f', 'g'], tipos_periodo: ['semestre'] }]);
|
||||
fixture.detectChanges();
|
||||
});
|
||||
afterEach(() => { fixture.destroy(); http.verify(); vi.unstubAllGlobals(); });
|
||||
async function settle() { await fixture.whenStable(); for (let i = 0; i < 12; i++) await Promise.resolve(); fixture.detectChanges(); }
|
||||
function respond() {
|
||||
http.match(r => r.url.includes('/figuras/')).forEach(r => r.flush({ data: [{ y: [0, null] }], layout: { annotations: [{ text: 'Nota' }] } }));
|
||||
fixture.detectChanges();
|
||||
}
|
||||
it('dos gráficos poseen hosts y etiquetas independientes y purgan al contraer', async () => {
|
||||
respond(); await settle(); const charts = fixture.debugElement.queryAll(By.directive(PlotlyChart));
|
||||
expect(charts).toHaveLength(2); expect(runtime.react).toHaveBeenCalledTimes(2);
|
||||
const hosts = charts.map(c => c.componentInstance.host.nativeElement);
|
||||
expect(hosts[0]).not.toBe(hosts[1]); expect(hosts[0].getAttribute('aria-label')).toBe('tea · i · f');
|
||||
expect(hosts[1].getAttribute('aria-label')).toBe('tea · i · g');
|
||||
const card = fixture.debugElement.query(By.directive(IndicadorCatalogCard)).componentInstance as IndicadorCatalogCard;
|
||||
card.toggle(); fixture.detectChanges();
|
||||
for (const host of hosts) expect(runtime.purge).toHaveBeenCalledWith(host);
|
||||
for (const disconnect of disconnects) expect(disconnect).toHaveBeenCalledTimes(1);
|
||||
expect(fixture.debugElement.queryAll(By.directive(PlotlyChart))).toHaveLength(0);
|
||||
});
|
||||
it('un error de renderizado y su reintento no afectan la otra instancia', async () => {
|
||||
runtime.react.mockRejectedValueOnce(new Error('render'));
|
||||
respond(); await settle(); const charts = fixture.debugElement.queryAll(By.directive(PlotlyChart));
|
||||
expect(charts[0].componentInstance.status()).toBe('error'); expect(charts[1].componentInstance.status()).toBe('ready');
|
||||
const otherHost = charts[1].componentInstance.host.nativeElement;
|
||||
charts[0].componentInstance.retry(); await settle();
|
||||
expect(charts[0].componentInstance.status()).toBe('ready'); expect(charts[1].componentInstance.status()).toBe('ready');
|
||||
expect(runtime.react.mock.calls.filter(c => c[0] === otherHost)).toHaveLength(1); http.expectNone(() => true);
|
||||
});
|
||||
it('destruir la página desconecta y purga todas las instancias salientes', async () => {
|
||||
respond(); await settle(); fixture.destroy();
|
||||
expect(runtime.purge).toHaveBeenCalledTimes(2); expect(disconnects).toHaveLength(2);
|
||||
for (const disconnect of disconnects) expect(disconnect).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user