137 lines
8.0 KiB
TypeScript
137 lines
8.0 KiB
TypeScript
import { provideHttpClient } from '@angular/common/http';
|
|
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
|
import { TestBed } from '@angular/core/testing';
|
|
import { By } from '@angular/platform-browser';
|
|
import { provideRouter } from '@angular/router';
|
|
import { PLOTLY_LOADER } from '../../shared/plotly/plotly-runtime';
|
|
import { IndicadorHistopatologiaCama } from './indicador-histopatologia-cama';
|
|
import { IndicadorCatalogCard } from './indicador-catalog-card';
|
|
import { modelosHistopatologiaDemo } from './histopatologia-cama-demo';
|
|
import { recolorFigure } from './indicadores-c2-palette';
|
|
import { categoriasExteriores, donaHistopatologia, figuraTasas, seleccionBase, tasas } from './indicadores-histopatologia-cama';
|
|
import { IndicadoresMemory } from './indicadores-memory';
|
|
import { IndicadoresPage } from './indicadores-page';
|
|
|
|
const nombres = [
|
|
'Ductal (NST)', 'Lobulillar', 'Otros', 'Ductal-lobulillar mixto', 'Mucinoso', 'Paget', 'Papilar',
|
|
'Tubular / cribiforme', 'Micropapilar', 'SIN MAPEAR', 'Inflamatorio', 'Medular',
|
|
];
|
|
const [modelo] = modelosHistopatologiaDemo('mes', ['2025-02']);
|
|
const suma = (valores: number[]) => valores.reduce((a, b) => a + b, 0);
|
|
|
|
describe('Fuente sintética de histopatología CaMa', () => {
|
|
it('dibuja los 12 subtipos en orden, con colores distintos y con el subtotal del grupo exterior', () => {
|
|
const seleccion = seleccionBase(modelo);
|
|
const [, interior, exterior] = recolorFigure(donaHistopatologia(modelo, seleccion, 'claro')).data;
|
|
expect(exterior['labels']).toEqual(nombres);
|
|
expect(categoriasExteriores(modelo).map(c => c.etiqueta)).toEqual(nombres);
|
|
const colores = categoriasExteriores(modelo).map(c => c.color);
|
|
expect(new Set(colores).size).toBe(12);
|
|
expect(exterior['marker']).toMatchObject({ colors: colores });
|
|
expect(interior['labels']).toEqual(['No maligno', 'Riesgo a carcinoma']);
|
|
expect(suma(exterior['values'] as number[])).toBe((interior['values'] as number[])[1]);
|
|
expect(suma(interior['values'] as number[])).toBe(modelo.conResultado);
|
|
expect((exterior['values'] as number[]).every(n => n > 0)).toBe(true);
|
|
});
|
|
|
|
it('mantiene SIN MAPEAR en mayúsculas y evita porcentajes indefinidos en un anillo sin casos', () => {
|
|
const figura = figuraTasas(modelo, { grupo: modelo.base, categoria: 'SIN_MAPEAR' }, 'claro');
|
|
expect(figura.layout['xaxis']).toMatchObject({ title: { text: 'Porcentaje con SIN MAPEAR' } });
|
|
const benignos = modelo.totales['BENIGNO'];
|
|
const totales = { ...modelo.totales, ...Object.fromEntries(categoriasExteriores(modelo).map(c => [c.id, 0])) };
|
|
const sinCasos = { ...modelo, totales, conResultado: benignos };
|
|
const exterior = donaHistopatologia(sinCasos, seleccionBase(sinCasos), 'claro').data[2];
|
|
expect(exterior['customdata']).toEqual(nombres.map(() => ['0', '—']));
|
|
});
|
|
|
|
it('reconcilia barras, anillos y tabla para cada selección, incluidos los ceros', () => {
|
|
for (const { id, grupo } of modelo.categorias) {
|
|
const seleccion = { grupo, categoria: id };
|
|
const valores = tasas(modelo, seleccion);
|
|
expect(suma(valores.map(t => t.k!))).toBe(modelo.totales[id]);
|
|
for (const { fila, k, valor, bajo, alto } of valores) {
|
|
expect(k).toBe(fila.celdas[id].n);
|
|
expect(valor).toBe(100 * k! / fila.den!);
|
|
expect(bajo).toBeGreaterThanOrEqual(0);
|
|
expect(alto).toBeLessThanOrEqual(100);
|
|
expect(fila.total.n).toBe(fila.den! + fila.aparte['SIN_BIOPSIA'].n! + fila.aparte['SIN_DATO'].n!);
|
|
}
|
|
const [barra] = figuraTasas(modelo, seleccion, 'oscuro', true).data;
|
|
expect(barra['type']).toBe('bar');
|
|
expect(barra['x']).toEqual(valores.map(t => t.valor));
|
|
}
|
|
expect(tasas(modelo, { grupo: modelo.base, categoria: 'MICROPAPILAR' })[0].valor).toBe(0);
|
|
});
|
|
|
|
it('agrega los mismos meses en cada semestre y devuelve vacío fuera de cobertura', () => {
|
|
const semestres = modelosHistopatologiaDemo('semestre');
|
|
expect(semestres.map(m => m.periodo)).toEqual(['2025-S1', '2025-S2', '2026-S1', '2026-S2']);
|
|
const primerSemestre = modelosHistopatologiaDemo('mes').filter(m => m.periodo <= '2025-06');
|
|
for (const { id } of modelo.categorias) {
|
|
expect(semestres[0].totales[id]).toBe(suma(primerSemestre.map(m => m.totales[id]!)));
|
|
}
|
|
expect(modelosHistopatologiaDemo('mes', ['2025-03', '2025-02']).map(m => m.periodo)).toEqual(['2025-03', '2025-02']);
|
|
expect(modelosHistopatologiaDemo('mes', ['2024-10'])).toEqual([]);
|
|
});
|
|
|
|
it.each([true, false])('Resumen usa los datos incorporados con medida publicada = %s', publicada => {
|
|
TestBed.configureTestingModule({ imports: [IndicadoresPage], providers: [
|
|
provideRouter([]), provideHttpClient(), provideHttpClientTesting(),
|
|
{ provide: PLOTLY_LOADER, useValue: () => new Promise(() => {}) },
|
|
] });
|
|
const http = TestBed.inject(HttpTestingController);
|
|
const memory = TestBed.inject(IndicadoresMemory);
|
|
for (const dominio of ['cama', 'cacu', 'tea'] as const) memory.finishInitialKpiPresentation(dominio);
|
|
const f = TestBed.createComponent(IndicadoresPage);
|
|
const catalogo = (id: string) => [{ id, titulo: 'Indicador publicado', contrato: 'C2', figuras: [], tipos_periodo: ['semestre', 'mes'] }];
|
|
http.expectOne('/tea/indicadores').flush(catalogo('tea_prueba'));
|
|
f.componentInstance.changeDominio('cama');
|
|
const idsCama = ['cama_birads_456_mastografia', 'cama_birads_456_paciente'];
|
|
if (publicada) idsCama.push('cama_histopatologia_birads');
|
|
http.expectOne('/cama/indicadores').flush(idsCama.flatMap(catalogo));
|
|
f.detectChanges();
|
|
const tarjeta = f.debugElement.query(node => node.componentInstance instanceof IndicadorHistopatologiaCama);
|
|
expect(tarjeta).toBeTruthy();
|
|
const histo = tarjeta.componentInstance as IndicadorHistopatologiaCama;
|
|
const card = f.debugElement.queryAll(By.directive(IndicadorCatalogCard))
|
|
.map(node => node.componentInstance as IndicadorCatalogCard)
|
|
.find(item => item.indicador.id === 'cama_histopatologia_birads')!;
|
|
card.setVisible(true);
|
|
f.detectChanges();
|
|
expect(histo.status()).toBe('ready');
|
|
expect(histo.contextPeriod).toBe('2026-S2');
|
|
expect(f.nativeElement.textContent).toContain('Tiempo de diagnóstico a tratamiento');
|
|
expect(f.componentInstance.catalog().map(item => item.id)).toEqual(idsCama);
|
|
expect(f.nativeElement.querySelector('app-indicador-conteos-pareados')).not.toBeNull();
|
|
expect([...tarjeta.nativeElement.querySelectorAll('.child')].map((b: HTMLElement) => b.textContent!.trim())).toEqual(nombres);
|
|
expect(histo.selectionLabel).toBe('Riesgo a carcinoma');
|
|
|
|
histo.plotClicked({ points: [{ data: { name: 'categorias' }, pointNumber: 9 }] });
|
|
f.detectChanges();
|
|
expect(histo.selectionLabel).toBe('SIN MAPEAR');
|
|
expect(histo.ratesFigure!.data[0]['x']).toEqual(tasas(histo.model!, histo.selection!).map(t => t.valor));
|
|
expect(tarjeta.nativeElement.querySelector('.child[aria-pressed=true]').textContent.trim()).toBe('SIN MAPEAR');
|
|
histo.plotClicked({ points: [{ data: { name: 'grupos' }, pointNumber: 0 }] });
|
|
expect(histo.selectionLabel).toBe('No maligno');
|
|
histo.plotClicked({ points: [{ data: { name: 'centro' }, pointNumber: 0 }] });
|
|
expect(histo.selectionLabel).toBe('Riesgo a carcinoma');
|
|
histo.changePeriod(-1);
|
|
expect(histo.contextPeriod).toBe('2026-S1');
|
|
histo.showHelp();
|
|
f.detectChanges();
|
|
expect(tarjeta.nativeElement.querySelector('.methodology').textContent).toContain('El exterior muestra solo la distribución de los subtipos');
|
|
http.expectNone(req => /cama_histopatologia_birads\/(datos|metadatos)$/.test(req.url));
|
|
|
|
for (const dominio of ['cacu', 'tea'] as const) {
|
|
f.componentInstance.changeDominio(dominio);
|
|
http.expectOne('/' + dominio + '/indicadores').flush(catalogo(dominio + '_prueba'));
|
|
f.detectChanges();
|
|
expect(f.nativeElement.querySelector('app-indicador-histopatologia-cama')).toBeNull();
|
|
expect(f.nativeElement.querySelector('.catalog-grid').textContent).toContain('Indicador publicado');
|
|
}
|
|
f.destroy();
|
|
http.match(() => true);
|
|
http.verify();
|
|
});
|
|
});
|