Files
WebApp/src/app/features/indicadores/indicadores-seguimiento-ficha.spec.ts
T
2026-11-22 23:41:31 -06:00

125 lines
7.7 KiB
TypeScript

// Covers how CaMa's follow-up table opens and closes an individual record: the control that opens it, what stays in
// the table while the record is open (page, filters), that no request is made, the focus on return, and the page
// being told. Fixture: fixtures/cama_seguimiento.json, served as the first page of 5 rows.
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import fixtureData from './fixtures/cama_seguimiento.json';
import { IndicadoresSeguimientoTabla } from './indicadores-seguimiento-tabla';
import { IndicadoresTracking } from './indicadores-tracking';
const pagina = () => {
const d = structuredClone(fixtureData) as unknown as Record<string, any>, todos = d['items'] as unknown[];
d['items'] = todos.slice(0, 5);
d['paginacion'] = { page: 1, page_size: 5, total_items: todos.length, total_pages: Math.ceil(todos.length / 5) };
return d;
};
describe('Seguimiento de CaMa: ficha del paciente', () => {
let fixture: ReturnType<typeof TestBed.createComponent<IndicadoresSeguimientoTabla>>, http: HttpTestingController, abierta: boolean[];
beforeEach(async () => {
TestBed.configureTestingModule({ imports: [IndicadoresSeguimientoTabla], providers: [provideHttpClient(), provideHttpClientTesting()] });
fixture = TestBed.createComponent(IndicadoresSeguimientoTabla); http = TestBed.inject(HttpTestingController);
abierta = [];
fixture.componentInstance.fichaAbierta.subscribe(v => abierta.push(v));
fixture.componentRef.setInput('dominio', 'cama'); fixture.componentRef.setInput('active', true); fixture.detectChanges();
http.expectOne(r => r.url === '/cama/seguimiento').flush(pagina());
await asentar();
});
afterEach(() => { fixture.destroy(); http.verify(); });
const root = () => fixture.nativeElement as HTMLElement;
async function asentar() { fixture.detectChanges(); await fixture.whenStable(); fixture.detectChanges(); }
const tabla = () => root().querySelector('div[hidden], div:not([hidden])')!.parentElement!.querySelector('form')!.parentElement as HTMLElement;
const enlace = (n = 0) => root().querySelectorAll<HTMLButtonElement>('.patient-link')[n];
const ficha = () => root().querySelector('app-cama-patient-card');
// Fails if the export buttons leave the title row or come back under the table.
it('has the export buttons in the row of the section title, and nothing after the pagination', () => {
const row = root().querySelector('.title-row')!;
// Fails if the section heading goes back to «Seguimiento de …» instead of the name of the section tab.
expect(row.querySelector('h2')!.textContent).toBe('Análisis de riesgos');
expect([...row.querySelectorAll('button')].map(b => b.textContent!.trim())).toEqual(['Exportar Excel (.xlsx)', 'Exportar PDF']);
expect(root().querySelector('nav')!.nextElementSibling).toBeNull();
expect(root().textContent).not.toContain('Exportar resultados');
});
// Fails if the identifier stops being a control, which leaves the record reachable only by mouse.
it('draws each identifier as a button named after the patient', () => {
expect(root().querySelectorAll('.patient-link')).toHaveLength(5);
expect(enlace().getAttribute('aria-label')).toBe(`Ver ficha del paciente ${enlace().textContent!.trim()}`);
});
// Fails if opening a record fetches anything: the card reads only the row, and no identifier may travel to a server.
it('opens the record of the patient from the row, with no request', async () => {
const id = enlace(1).textContent!.trim();
enlace(1).click(); await asentar();
expect(ficha()!.querySelector('h2')!.textContent).toContain(`Paciente ${id}`);
expect(abierta).toEqual([true]);
http.expectNone(() => true);
});
// Fails if the list is replaced instead of hidden: returning would lose the page and the filters being looked at.
it('hides the table while the record is open and shows the same table afterwards', async () => {
const before = root().querySelector('tbody')!;
enlace().click(); await asentar();
const wrapper = root().querySelector('form')!.parentElement as HTMLElement;
expect(wrapper.hidden).toBe(true);
[...ficha()!.querySelectorAll('button')].find(b => b.textContent!.includes('Regresar'))!.click(); await asentar();
expect(ficha()).toBeNull();
expect(wrapper.hidden).toBe(false);
expect(root().querySelector('tbody')).toBe(before);
expect(abierta).toEqual([true, false]);
});
// Fails if focus is lost on return, which strands keyboard users at the top of the page.
it('returns focus to the identifier that opened the record', async () => {
enlace(2).click(); await asentar();
[...ficha()!.querySelectorAll('button')].find(b => b.textContent!.includes('Regresar'))!.click(); await asentar();
await new Promise(r => setTimeout(r));
expect(document.activeElement).toBe(enlace(2));
});
// Fails if a click on the row (outside its button) stops opening the record, or if opening happens twice.
it('also opens the record from a click on the row, once', async () => {
const fila = root().querySelectorAll<HTMLElement>('tbody tr')[3];
fila.querySelector<HTMLElement>('td:nth-child(2)')!.click(); await asentar();
expect(ficha()).not.toBeNull();
expect(abierta).toEqual([true]);
});
// Fails if the page stays titled for a record after the domain changed and the record went away.
it('closes the record and tells the page when the domain changes', async () => {
enlace().click(); await asentar();
fixture.componentRef.setInput('dominio', 'cacu'); fixture.detectChanges(); await asentar();
// The table asks for the new domain's list; that answer is not what this test is about.
http.match(r => r.url === '/cacu/seguimiento').forEach(r => r.flush({ error: 'not under test' }, { status: 500, statusText: 'Server Error' }));
expect(ficha()).toBeNull();
expect(abierta).toEqual([true, false]);
});
});
describe('Seguimiento: ficha de CaMa y cambio de dominio', () => {
// Fails if switching domain with CaMa's record open leaves the page titled for a record that no longer shows:
// the table that held the record is removed without being told, so the section above it must tell the page.
it('tells the page that no record shows when the domain changes while one is open', async () => {
TestBed.configureTestingModule({ imports: [IndicadoresTracking], providers: [provideHttpClient(), provideHttpClientTesting()] });
const fixture = TestBed.createComponent(IndicadoresTracking), http = TestBed.inject(HttpTestingController);
const abierta: boolean[] = [];
fixture.componentInstance.fichaPacienteAbierta.subscribe(v => abierta.push(v));
fixture.componentRef.setInput('domainLabel', 'cáncer de mama'); fixture.componentRef.setInput('dominio', 'cama'); fixture.componentRef.setInput('active', true);
fixture.detectChanges();
http.expectOne(r => r.url === '/cama/seguimiento').flush(pagina());
await fixture.whenStable(); fixture.detectChanges();
(fixture.nativeElement as HTMLElement).querySelector<HTMLButtonElement>('.patient-link')!.click();
await fixture.whenStable(); fixture.detectChanges();
expect(abierta).toEqual([true]);
expect((fixture.nativeElement as HTMLElement).querySelector('.title-row')!.closest('[hidden]')).not.toBeNull();
fixture.componentRef.setInput('dominio', 'tea'); fixture.detectChanges();
expect(abierta).toEqual([true, false]);
// TEA asks for its own list as soon as it is active.
for (const req of http.match(r => r.url === '/tea/seguimiento')) req.flush({}, { status: 404, statusText: 'Pending' });
http.verify(); fixture.destroy();
});
});