3b246b4f1f
Rows name their patient with pac_id (a decimal string) or the legacy id_seudonimo. pac_id wins; an invalid pac_id is never replaced by the legacy field, and a page that repeats a patient or mixes both fields is not drawn or exported. The table, patient record, PDF and spreadsheet follow the announced identifier column and label. CaCu requests risk only with a pac_id.
178 lines
9.2 KiB
TypeScript
178 lines
9.2 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
|
import { ESTUDIOS_DEMO, RESULTADOS_DEMO } from './paciente-registro.catalogos';
|
|
import { PacienteRegistroDialog } from './paciente-registro-dialog';
|
|
import { NuevoRegistroTemporal, TipoRegistroTemporal } from './paciente-registro.models';
|
|
import { crearRegistroTemporal, esFechaReal } from './paciente-registro.validation';
|
|
import { PacienteRegistrosSesion } from './paciente-registros-sesion';
|
|
import { identidadDe } from '../paciente-identidad';
|
|
|
|
describe('Fecha de calendario de registros temporales', () => {
|
|
it('acepta días reales, incluyendo años bisiestos y límites de año', () => {
|
|
for (const fecha of ['2024-02-29', '2000-02-29', '2026-04-30', '0001-01-01', '9999-12-31']) {
|
|
expect(esFechaReal(fecha), fecha).toBe(true);
|
|
}
|
|
});
|
|
|
|
it('rechaza fechas imposibles, normalizaciones y cadenas arbitrarias', () => {
|
|
for (const fecha of ['', 'ayer', '2026-02-29', '1900-02-29', '2026-04-31', '2026-00-01', '2026-13-01',
|
|
'2026-01-00', '0000-01-01', '2026-1-1', '26/10/2026', ' 2026-10-26', '2026-10-26T00:00:00Z', null, new Date()]) {
|
|
expect(esFechaReal(fecha), String(fecha)).toBe(false);
|
|
}
|
|
});
|
|
});
|
|
|
|
const casos: { tipo: TipoRegistroTemporal; seleccion: string; registro: NuevoRegistroTemporal }[] = [
|
|
{ tipo: 'condicion', seleccion: 'embarazo', registro: { tipo: 'condicion', condicion: 'embarazo', fecha: '2026-10-26' } },
|
|
{ tipo: 'procedimiento', seleccion: 'colposcopia', registro: { tipo: 'procedimiento', procedimiento: 'colposcopia', fecha: '2026-10-26' } },
|
|
{ tipo: 'resultado', seleccion: 'vph', registro: { tipo: 'resultado', estudio: 'vph', resultado: 'negativo', fecha: '2026-10-26' } },
|
|
];
|
|
|
|
describe.each(casos)('Formulario de $tipo', caso => {
|
|
let fixture: ReturnType<typeof TestBed.createComponent<PacienteRegistroDialog>>;
|
|
let component: PacienteRegistroDialog;
|
|
let cerrar: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
cerrar = vi.fn();
|
|
TestBed.configureTestingModule({ providers: [
|
|
{ provide: MAT_DIALOG_DATA, useValue: { tipo: caso.tipo, paciente: identidadDe({ id_seudonimo: '001' })! } },
|
|
{ provide: MatDialogRef, useValue: { close: cerrar } },
|
|
] });
|
|
fixture = TestBed.createComponent(PacienteRegistroDialog);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
});
|
|
|
|
it('muestra destino, campos obligatorios y confirmación deshabilitada sin texto técnico', () => {
|
|
const root: HTMLElement = fixture.nativeElement;
|
|
expect(root.textContent).not.toMatch(/demostración|sintético|persistencia|solo existe en esta sesión/i);
|
|
expect(root.textContent).toContain('Paciente 001');
|
|
expect(root.querySelector<HTMLButtonElement>('[type="submit"]')!.disabled).toBe(true);
|
|
component.agregar();
|
|
fixture.detectChanges();
|
|
expect(root.textContent).toContain('Selecciona una opción');
|
|
expect(root.textContent).toContain('Ingresa una fecha válida');
|
|
expect(cerrar).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rechaza opciones ajenas al catálogo y fechas inválidas antes de cerrar', () => {
|
|
component.form.patchValue({ seleccion: 'toString', fecha: '2026-02-30' });
|
|
component.agregar();
|
|
fixture.detectChanges();
|
|
expect(component.form.invalid).toBe(true);
|
|
expect(fixture.nativeElement.textContent).toContain('Ingresa una fecha real');
|
|
expect(cerrar).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('confirma exactamente una vez con un registro válido y tipado', () => {
|
|
component.form.patchValue({ seleccion: caso.seleccion, fecha: caso.registro.fecha });
|
|
if (caso.registro.tipo === 'resultado') component.form.controls.resultado.setValue(caso.registro.resultado);
|
|
fixture.detectChanges();
|
|
expect(component.form.valid).toBe(true);
|
|
expect(fixture.nativeElement.querySelector('[type="submit"]').disabled).toBe(false);
|
|
fixture.nativeElement.querySelector('form').dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
|
|
component.agregar();
|
|
expect(cerrar).toHaveBeenCalledExactlyOnceWith(caso.registro);
|
|
});
|
|
});
|
|
|
|
describe('Resultados dependientes del estudio', () => {
|
|
let component: PacienteRegistroDialog;
|
|
let cerrar: ReturnType<typeof vi.fn>;
|
|
beforeEach(() => {
|
|
cerrar = vi.fn();
|
|
TestBed.configureTestingModule({ providers: [
|
|
{ provide: MAT_DIALOG_DATA, useValue: { tipo: 'resultado', paciente: identidadDe({ id_seudonimo: 'A' })! } },
|
|
{ provide: MatDialogRef, useValue: { close: cerrar } },
|
|
] });
|
|
const fixture = TestBed.createComponent(PacienteRegistroDialog);
|
|
component = fixture.componentInstance;
|
|
fixture.detectChanges();
|
|
});
|
|
|
|
it('limpia el resultado al cambiar de estudio y exige elegir uno compatible', () => {
|
|
expect(component.form.controls.resultado.disabled).toBe(true);
|
|
component.form.patchValue({ seleccion: 'vph', fecha: '2026-10-26' });
|
|
component.form.controls.resultado.setValue('positivo_16_18');
|
|
expect(component.form.valid).toBe(true);
|
|
component.form.controls.seleccion.setValue('citologia');
|
|
expect(component.form.controls.resultado.value).toBe('');
|
|
expect(component.form.invalid).toBe(true);
|
|
expect(component.resultados.map(r => r.valor)).not.toContain('positivo_16_18');
|
|
component.form.controls.resultado.setValue('positivo_16_18');
|
|
component.agregar();
|
|
expect(cerrar).not.toHaveBeenCalled();
|
|
component.form.controls.resultado.setValue('asc_us');
|
|
expect(component.form.valid).toBe(true);
|
|
component.form.controls.seleccion.setValue('');
|
|
expect(component.form.controls.resultado.value).toBe('');
|
|
expect(component.form.controls.resultado.disabled).toBe(true);
|
|
expect(component.resultados).toEqual([]);
|
|
expect(component.form.invalid).toBe(true);
|
|
});
|
|
|
|
it('admite cada pareja de los tres catálogos y rechaza combinaciones cruzadas', () => {
|
|
for (const estudio of Object.keys(ESTUDIOS_DEMO) as (keyof typeof ESTUDIOS_DEMO)[]) {
|
|
component.form.patchValue({ seleccion: estudio, fecha: '2026-10-26' });
|
|
expect(component.resultados.map(r => r.valor)).toEqual(Object.keys(RESULTADOS_DEMO[estudio]));
|
|
for (const resultado of Object.keys(RESULTADOS_DEMO[estudio])) {
|
|
component.form.controls.resultado.setValue(resultado);
|
|
expect(component.form.valid).toBe(true);
|
|
expect(crearRegistroTemporal('resultado', estudio, '2026-10-26', resultado)).toEqual({ tipo: 'resultado', estudio, fecha: '2026-10-26', resultado });
|
|
}
|
|
}
|
|
expect(crearRegistroTemporal('resultado', 'histologia', '2026-10-26', 'positivo_16_18')).toBeNull();
|
|
expect(crearRegistroTemporal('resultado', 'vph', '2026-10-26', 'nic3')).toBeNull();
|
|
expect(crearRegistroTemporal('resultado', 'toString', '2026-10-26', 'toString')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('Memoria de registros temporales', () => {
|
|
it('asocia las tres altas al seudónimo exacto sin convertir ni mezclar identidades', () => {
|
|
const sesion = TestBed.inject(PacienteRegistrosSesion);
|
|
for (const caso of casos) expect(sesion.agregar('001', caso.registro)).toBe(true);
|
|
expect(sesion.paraPaciente('001').map(r => r.tipo)).toEqual(['condicion', 'procedimiento', 'resultado']);
|
|
expect(sesion.paraPaciente('001').every(r => r.paciente === '001' && r.origen === 'sesion-demostracion')).toBe(true);
|
|
for (const id of ['1', ' 001', '001 ', 'otro', 'toString']) expect(sesion.paraPaciente(id)).toEqual([]);
|
|
sesion.agregar('toString', casos[0].registro);
|
|
expect(sesion.paraPaciente('toString')).toHaveLength(1);
|
|
expect(sesion.paraPaciente('001')).toHaveLength(3);
|
|
});
|
|
|
|
it('conserva instantáneas inmutables y no retiene el objeto de entrada', () => {
|
|
const sesion = TestBed.inject(PacienteRegistrosSesion);
|
|
const nuevo = { ...casos[0].registro };
|
|
const vacio = sesion.paraPaciente('A');
|
|
sesion.agregar('A', nuevo);
|
|
const primera = sesion.paraPaciente('A');
|
|
nuevo.fecha = '2000-01-01';
|
|
sesion.agregar('A', casos[1].registro);
|
|
expect(vacio).toEqual([]);
|
|
expect(primera).toHaveLength(1);
|
|
expect(primera[0].fecha).toBe('2026-10-26');
|
|
expect(Object.isFrozen(primera)).toBe(true);
|
|
expect(Object.isFrozen(primera[0])).toBe(true);
|
|
expect(new Set(sesion.paraPaciente('A').map(r => r.id)).size).toBe(2);
|
|
});
|
|
|
|
it('defiende la escritura en memoria de fechas y combinaciones inválidas', () => {
|
|
const sesion = TestBed.inject(PacienteRegistrosSesion);
|
|
expect(sesion.agregar('A', { ...casos[0].registro, fecha: 'ayer' })).toBe(false);
|
|
expect(sesion.agregar('', casos[0].registro)).toBe(false);
|
|
expect(sesion.agregar('A', { tipo: 'resultado', estudio: 'vph', resultado: 'nic3', fecha: '2026-10-26' } as unknown as NuevoRegistroTemporal)).toBe(false);
|
|
expect(sesion.paraPaciente('A')).toEqual([]);
|
|
});
|
|
|
|
it('una nueva instancia de aplicación empieza vacía, sin leer ni escribir almacenamiento web', () => {
|
|
const storage = [vi.spyOn(Storage.prototype, 'setItem'), vi.spyOn(Storage.prototype, 'getItem')];
|
|
const anterior = TestBed.inject(PacienteRegistrosSesion);
|
|
anterior.agregar('001', casos[0].registro);
|
|
TestBed.resetTestingModule();
|
|
const nueva = TestBed.inject(PacienteRegistrosSesion);
|
|
expect(nueva).not.toBe(anterior);
|
|
expect(nueva.paraPaciente('001')).toEqual([]);
|
|
for (const spy of storage) { expect(spy).not.toHaveBeenCalled(); spy.mockRestore(); }
|
|
});
|
|
});
|