129 lines
9.1 KiB
TypeScript
129 lines
9.1 KiB
TypeScript
import fixture from './fixtures/eu7-c2-ejemplos.json';
|
|
import { IndicadorDatos } from './indicadores.models';
|
|
import { barras, clic, dona, modelo, porcentaje, periodosCompatibles, Seleccion } from './indicadores-eu7-escenario';
|
|
import { percentNumber } from './indicadores-format';
|
|
|
|
function eu7Datos() {
|
|
return structuredClone({ general: fixture.general as IndicadorDatos, escenarios: fixture.estatus_seguimiento as IndicadorDatos });
|
|
}
|
|
describe('EU7 modelo de presentación del entregable', () => {
|
|
let data: ReturnType<typeof eu7Datos>;
|
|
beforeEach(() => data = eu7Datos());
|
|
const periodo = '2025-S2';
|
|
const model = () => modelo(data.general, data.escenarios, periodo);
|
|
it('946 con resultado y 87 sin dato; anillos pie conservan categorías, colores y no mutan el original', () => {
|
|
const original = structuredClone(data), m = model(), f = dona(m, {}, 'claro');
|
|
expect(m.total).toBe(946); expect(m.sinDato.n).toBe(87);
|
|
expect(f.data.map(t => t['type'])).toEqual(['pie', 'pie', 'pie']);
|
|
expect(f.data[2]['labels']).not.toContain('Sin dato');
|
|
expect(f.data[2]['values']).toEqual([798, 62, 46, 17, 19, 4]);
|
|
expect(data).toEqual(original);
|
|
});
|
|
it('TOTAL → Anormal → LIEAG → grupo → TOTAL y toggles', () => {
|
|
const m = model(); let s: Seleccion = {};
|
|
const g = m.grupos.findIndex(g => g.id === 'ANORMAL'), c = m.categorias.findIndex(c => c.id === 'LIEAG');
|
|
s = clic(m, s, 'grupos', g); expect(s).toEqual({ grupo: 'ANORMAL' });
|
|
s = clic(m, s, 'categorias', c); expect(s).toEqual({ grupo: 'ANORMAL', categoria: 'LIEAG' });
|
|
expect(clic(m, s, 'categorias', c)).toEqual({ grupo: 'ANORMAL' });
|
|
s = clic(m, s, 'centro', 0); expect(s).toEqual({ grupo: 'ANORMAL' });
|
|
s = clic(m, s, 'centro', 0); expect(s).toEqual({});
|
|
expect(clic(m, { grupo: 'ANORMAL' }, 'grupos', g)).toEqual({});
|
|
expect(clic(m, { categoria: 'LIEAG' }, 'centro', 0)).toEqual({});
|
|
expect(clic(m, {}, 'categorias', c)).toEqual({ grupo: undefined, categoria: 'LIEAG' });
|
|
expect(clic(m, {}, 'otra', c)).toEqual({});
|
|
expect(clic(m, {}, 'grupos', -1)).toEqual({});
|
|
expect(clic(m, { grupo: 'ANORMAL' }, 'categorias', 0)).toEqual({ grupo: 'ANORMAL' });
|
|
});
|
|
it('cada escenario suma 100 % en TOTAL y Anormal conserva 5.0, 24.3, 21.7, 7.8 %', () => {
|
|
const m = model(), f = barras(m, {}, 'claro');
|
|
m.escenarios.forEach((s, i) => expect(f.data.reduce((n, t) => n + (t['x'] as number[])[i], 0)).toBeCloseTo(100, 8));
|
|
expect((f.layout['annotations'] as { text: string }[]).map(a => a.text)).toEqual(m.escenarios.map(s => `${s.den}`));
|
|
const selected = barras(m, { grupo: 'ANORMAL' }, 'claro');
|
|
expect(m.escenarios.map((s, i) => selected.data.reduce((n, t) => n + (t['x'] as number[])[i], 0).toFixed(1)))
|
|
.toEqual(['5.0', '24.3', '21.7', '7.8']);
|
|
const category = barras(m, { categoria: 'LIEAG' }, 'claro');
|
|
// Same traces in every selection (animation identity); unselected ones are zero-width.
|
|
expect(category.data.map(t => t['uid'])).toEqual(f.data.map(t => t['uid']));
|
|
const lieag = category.data.find(t => t['uid'] === 'LIEAG')!;
|
|
expect(lieag['x']).toEqual(m.escenarios.map(s => 100 * s.celdas['LIEAG'].n! / s.den!));
|
|
expect(category.data.filter(t => t !== lieag).every(t => (t['x'] as number[]).every(v => v === 0) && t['hoverinfo'] === 'skip')).toBe(true);
|
|
expect((category.layout['annotations'] as { text: string }[]).map(a => a.text))
|
|
.toEqual(m.escenarios.map(s => `<b>${percentNumber(100 * s.celdas['LIEAG'].n! / s.den!)} %</b> (${s.celdas['LIEAG'].n}/${s.den})`));
|
|
expect(m.escenarios.map(s => s.den)).toEqual([682, 140, 60, 64]);
|
|
});
|
|
it.each(['estrato', 'categorias', 'periodos', 'orden', 'duplicado', 'version', 'filtros', 'total', 'diferencia'])('rechaza incompatibilidad de %s', kind => {
|
|
if (kind === 'estrato') data.escenarios['estrato'] = 'grupo_edad';
|
|
if (kind === 'categorias') (data.escenarios['categorias'] as any)['LIEAG'].etiqueta = 'Otra';
|
|
if (kind === 'periodos') data.escenarios.periodos = ['2025-S1'];
|
|
if (kind === 'orden') data.escenarios.filas[0]['orden'] = 5;
|
|
if (kind === 'duplicado') data.escenarios.filas.push(data.escenarios.filas[0]);
|
|
if (kind === 'version') data.escenarios['version'] = 'otra';
|
|
if (kind === 'filtros') data.escenarios['filtros'] = { grupo_edad: '25-34' };
|
|
if (kind === 'total') (data.escenarios['totales_estrato'] as any[])[0].n++;
|
|
if (kind === 'diferencia') data.general.filas[0]['n'] = 800;
|
|
expect(model).toThrow();
|
|
});
|
|
it('no acepta periodos omitidos por el servidor ni respuestas fuera de la selección', () => {
|
|
expect(() => periodosCompatibles(data.general, data.escenarios, 'semestre', ['2025-S1', periodo])).toThrow();
|
|
expect(() => periodosCompatibles(data.general, data.escenarios, 'mes', [periodo])).toThrow();
|
|
});
|
|
it('rechaza procedencias diferentes entre respuestas incluso con conteos iguales', () => {
|
|
data.general['reportes'] = [{ periodo, report_id: 1 }]; data.escenarios['reportes'] = [{ periodo, report_id: 2 }];
|
|
expect(model).toThrow(/reportes distintos/);
|
|
});
|
|
it.each(['suprimido', 'nulo', 'sin fila', 'sin dato suprimido', 'total suprimido'])('escenario con %s: todos los puntos son null y no hay denominador inventado', kind => {
|
|
const row = data.escenarios.filas.find(f => f['estrato'] === 'VIGILANCIA' && f['categoria'] === (kind === 'sin dato suprimido' ? 'SIN_DATO' : 'LIEAG'))!;
|
|
if (kind === 'sin fila') data.escenarios.filas = data.escenarios.filas.filter(f => f !== row);
|
|
else if (kind === 'total suprimido') Object.assign((data.escenarios['totales_estrato'] as any[])[0], { n: 123456, suprimido: 1 });
|
|
else Object.assign(row, { n: kind === 'nulo' ? null : 123456, suprimido: kind === 'nulo' ? 0 : 1 });
|
|
const m = model(), f = barras(m, { categoria: 'LIEAG' }, 'claro');
|
|
expect(m.escenarios[0].den).toBeNull(); expect(f.data.find(t => t['uid'] === 'LIEAG')!['x']).toEqual([null, 100 * 9 / 140, 100 * 4 / 60, 100 / 64]);
|
|
expect(f.data.every(t => (t['x'] as unknown[])[0] === null)).toBe(true);
|
|
expect(f.data.filter(t => t['uid'] !== 'LIEAG').every(t =>
|
|
t['hoverinfo'] === 'skip' && (t['customdata'] as unknown[]).length === 0)).toBe(true);
|
|
expect(JSON.stringify(f)).not.toContain('123456');
|
|
expect(porcentaje(m.escenarios[0], 'LIEAG')).toBeNull();
|
|
});
|
|
it.each(['suprimido', 'nulo', 'sin fila'])('general con %s nunca produce una dona parcial ni convierte null en cero', kind => {
|
|
if (kind === 'sin fila') data.general.filas.splice(0, 1);
|
|
else Object.assign(data.general.filas[0], { n: null, suprimido: kind === 'suprimido' ? 1 : 0 });
|
|
const m = model(); expect(m.total).toBeNull(); expect(dona(m, {}, 'claro').data).toEqual([]);
|
|
});
|
|
it('escenario ausente no se inventa; solo se dibujan escenarios con filas publicadas', () => {
|
|
const removed = data.escenarios.filas.filter(f => f['estrato'] === 'INFO_INSUFICIENTE');
|
|
data.escenarios.filas = data.escenarios.filas.filter(f => f['estrato'] !== 'INFO_INSUFICIENTE');
|
|
data.general.filas.forEach(f => f['n'] = (f['n'] as number) - (removed.find(r => r['categoria'] === f['categoria'])!['n'] as number));
|
|
const m = model(); expect(m.escenarios.map(s => s.id)).not.toContain('INFO_INSUFICIENTE');
|
|
expect(barras(m, {}, 'claro').data[0]['x']).toHaveLength(3);
|
|
});
|
|
it('escenario vacío y periodo de ceros no producen porcentajes 0/0', () => {
|
|
data.general.filas.forEach(f => f['n'] = 0); data.escenarios.filas.forEach(f => f['n'] = 0);
|
|
(data.escenarios['totales_estrato'] as any[]).forEach(t => t.n = 0);
|
|
const m = model(); expect(m.total).toBe(0); expect(dona(m, {}, 'claro').data).toEqual([]);
|
|
expect(barras(m, {}, 'claro').data.every(t => (t['x'] as null[]).every(v => v === null))).toBe(true);
|
|
expect(barras(m, { categoria: 'LIEAG' }, 'claro').data.every(t => (t['x'] as null[]).every(v => v === null))).toBe(true);
|
|
expect(barras(m, {}, 'claro').layout['annotations']).toEqual(expect.arrayContaining([expect.objectContaining({ text: 'Sin pacientes con resultado' })]));
|
|
});
|
|
it('periodo sin pacientes con filas vacías es empty, sin fabricar celdas cero', () => {
|
|
data.general.filas = []; data.escenarios.filas = []; data.escenarios['totales_estrato'] = [];
|
|
const m = model(); expect(m.vacio).toBe(true); expect(m.total).toBe(0); expect(m.escenarios).toEqual([]);
|
|
expect(m.celdas['NORMAL'].n).toBeNull();
|
|
});
|
|
it('selección jerárquica y Volver conservan UID, orden, denominadores y datos fuente', () => {
|
|
const original = structuredClone(data), m = model(), snapshot = structuredClone(m);
|
|
const ids = m.categorias.map(c => c.id);
|
|
for (const selection of [{}, { grupo: 'ANORMAL' }, { grupo: 'ANORMAL', categoria: 'LIEAG' }, { categoria: 'LIEAG' }, {}]) {
|
|
const f = barras(m, selection, 'claro');
|
|
expect(f.data.map(t => t['uid'])).toEqual(ids);
|
|
expect(new Set(f.data.map(t => t['uid'])).size).toBe(ids.length);
|
|
f.data.forEach((t, i) => {
|
|
if (t['hoverinfo'] === 'skip') {
|
|
expect(t['customdata']).toEqual([]); expect(t['text']).toEqual(m.escenarios.map(() => ''));
|
|
} else expect(t['x']).toEqual(m.escenarios.map(s => porcentaje(s, ids[i])));
|
|
});
|
|
dona(m, selection, 'claro');
|
|
}
|
|
expect(m).toEqual(snapshot); expect(data).toEqual(original);
|
|
});
|
|
});
|