import { bars, grouped, hiddenNote, Mode, subsequentHistory, unavailable } from './bars'; import { scenarioBars } from './figures'; import { Reading } from './measures'; describe('bars', () => { it('list every category, so the shares add up', () => { const r: Reading = { populations: {}, strata: { category: { a: { 'initial-population': 3 }, other: { 'initial-population': 1 } }, }, }; const catalog = [{ code: 'a', label: 'A', color: '#000' }]; const t = bars({ kind: 'counts', label: 'x', measure: 'M', catalog }, () => r)!; expect(t.rows.map((x) => x.cells)).toEqual([ ['3', '75.0 %'], ['1', '25.0 %'], ]); }); it('aggregates only named abnormal cytology results without changing the denominator', () => { const cell = (n: number) => ({ 'initial-population': n }); const r: Reading = { populations: { 'initial-population': 10 }, strata: { category: { NILM: cell(4), LSIL: cell(2), HSIL: cell(1), no_data: cell(3) }, 'age_group-category': { '25-34|NILM': cell(4), '25-34|LSIL': cell(2), '25-34|HSIL': cell(1), '25-34|no_data': cell(3), }, }, }; const catalog = ['NILM', 'LSIL', 'HSIL'].map((code) => ({ code, label: code, color: '#000' })); const aggregate = { label: 'Anormal', color: '#f00', codes: ['LSIL', 'HSIL'] }; const t = bars({ kind: 'counts', label: 'x', measure: 'M', catalog, aggregate }, () => r)!; expect(t.rows[1].cells).toEqual(['3', '30.0 %']); const g = grouped( { kind: 'grouped', label: 'x', measure: 'M', catalog, aggregate, stratum: 'age_group', order: ['25-34'], unit: 'pacientes', ofAll: true, }, () => r, )!; expect(g.groups[0].rows[1].text).toBe('3/10 · 30.0 %'); }); it('shows the form history within subsequent screening without treating missing answers as a category', () => { const r: Reading = { populations: {}, strata: { round: { subsequent: { 'initial-population': 5 } }, subsequent_history: { within_1_year: { 'initial-population': 1 }, '3_or_more_years': { 'initial-population': 2 }, no_data: { 'initial-population': 2 }, }, }, }; expect(subsequentHistory(r)).toEqual([ { label: 'Un año o menos', text: '1/5 · 20.0 %' }, { label: 'Dos años', text: '0/5 · 0.0 %' }, { label: 'Tres o más años', text: '2/5 · 40.0 %' }, ]); expect(subsequentHistory({ populations: {}, strata: {} })).toBeUndefined(); }); it('a group with no patients has no rate', () => { const r: Reading = { populations: {}, strata: { s: { initial: { numerator: 0, denominator: 0 } } }, }; const mode = { kind: 'shares', label: 'x', measure: 'M', stratum: 's', order: ['initial'] }; expect(bars(mode as any, () => r)!.rows[0].cells).toEqual(['0/0', '—']); }); describe('screening rounds (complete)', () => { const ORDER = ['initial', 'subsequent', 'no_data']; const cell = (numerator: number, denominator: number) => ({ numerator, denominator }); const ppv = (strata: Record, total: number): Reading => ({ populations: { 'initial-population': total, denominator: total }, strata: { round: strata }, }); const mode = { kind: 'shares', label: 'x', measure: 'M', stratum: 'round', order: ORDER, complete: true, } as const; it('lists the three groups in order with their own counts', () => { const r = ppv({ no_data: cell(1, 4), subsequent: cell(6, 10), initial: cell(0, 6) }, 20); const t = bars(mode, () => r)!; expect(t.rows.map((x) => x.label)).toEqual([ 'Detección inicial', 'Detección subsecuente', 'Sin dato', ]); expect(t.rows.map((x) => x.cells)).toEqual([ ['0/6', '0.0 %'], ['6/10', '60.0 %'], ['1/4', '25.0 %'], ]); expect(t.note).toBeUndefined(); }); it('a group the report leaves out is zero patients when the counts add up', () => { const r = ppv({ no_data: cell(0, 3), subsequent: cell(6, 10) }, 13); const initial = bars(mode, () => r)!.rows[0]; expect(initial.cells).toEqual(['0/0', '—']); expect(initial.hint).toBe('Sin pacientes en el periodo'); }); it('a group left out is not available when the counts do not add up', () => { const r = ppv({ subsequent: cell(6, 10) }, 13); const t = bars(mode, () => r)!; expect(t.rows[0].cells).toEqual(['—', '—']); expect(t.rows[0].hint).toBe('No disponible'); expect(t.note).toContain('no concilian'); }); it('a zero numerator over a positive denominator is 0 %, a zero denominator has no rate', () => { const r = ppv({ initial: cell(0, 5), subsequent: cell(0, 0), no_data: cell(2, 3) }, 8); const t = bars(mode, () => r)!; expect(t.rows[0].cells).toEqual(['0/5', '0.0 %']); expect(t.rows[1].cells).toEqual(['0/0', '—']); expect(t.rows[1].hint).toBe('Sin pacientes en el periodo'); }); it('warns when only "Sin dato" holds patients', () => { const r = ppv({ no_data: cell(3, 7) }, 7); const t = bars(mode, () => r)!; expect(t.rows[2].cells).toEqual(['3/7', '42.9 %']); expect(t.note).toContain('información no registrada'); }); it('draws nothing without patients and says "No disponible" without the stratifier', () => { expect(bars(mode, () => ppv({}, 0))).toBeUndefined(); expect(unavailable(mode, () => ppv({}, 0))).toBeUndefined(); expect(unavailable(mode, () => ppv({}, 9))).toContain('No disponible'); expect(bars(mode, () => ppv({}, 9))).toBeUndefined(); }); it('a cell that is not whole or has a numerator above its denominator is not available', () => { const r = ppv({ initial: cell(5, 3), subsequent: cell(1, 2), no_data: cell(0, 1) }, 6); expect(bars(mode, () => r)!.rows[0].hint).toBe('No disponible'); }); it('VPH positives: groups from the crossed cells, absent ones reconciled', () => { const r: Reading = { populations: { 'initial-population': 12 }, strata: { 'round-category': { 'subsequent|positive': { 'initial-population': 3 }, 'subsequent|negative': { 'initial-population': 7 }, 'no_data|negative': { 'initial-population': 2 }, }, }, }; const t = bars( { kind: 'positives', label: 'x', measure: 'M', stratum: 'round', order: ORDER, complete: true, }, () => r, )!; expect(t.rows.map((x) => x.cells)).toEqual([ ['0/0', '—'], ['3/10', '30.0 %'], ['0/2', '0.0 %'], ]); }); }); describe('cytology by round', () => { const cats = [ { code: 'NILM', label: 'Normal', color: '#0a0', group: '' }, { code: 'HSIL', label: 'LIEAG', color: '#a00', group: '' }, ]; const reading = (cells: Record, total: number): Reading => ({ populations: { 'initial-population': total }, strata: { 'round-category': Object.fromEntries( Object.entries(cells).map(([k, n]) => [k, { 'initial-population': n }]), ), }, }); const draw = (r: Reading) => scenarioBars( r, { groups: [], categories: cats }, 'round', (c) => c === 'initial' ? 'Detección inicial' : c === 'subsequent' ? 'Detección subsecuente' : 'Sin dato', ['initial', 'subsequent', 'no_data'], { fill: true }, ); it('keeps the order and shows each bar with its n, an empty one included', () => { const f = draw( reading({ 'subsequent|NILM': 6, 'subsequent|HSIL': 2, 'no_data|NILM': 2 }, 10), ); const y = (f.data[0] as any).y; expect(y).toEqual([ 'Detección inicial
Pacientes: 0', 'Detección subsecuente
Pacientes: 8', 'Sin dato
Pacientes: 2', ]); expect((f.data[0] as any).x).toEqual([0, 75, 100]); }); it('leaves out a group that does not reconcile', () => { const f = draw(reading({ 'subsequent|NILM': 6 }, 10)); expect((f.data[0] as any).y).toEqual(['Detección subsecuente
Pacientes: 6']); }); }); }); describe('shared views of cytology and HPV by genotype', () => { const HPV = [ { code: 'negative', label: 'Negativo', color: '#42A77E', group: '' }, { code: 'positive_other', label: 'Positivo sin genotipo', color: '#A8C243', group: '' }, ]; const reading = (cells: Record): Reading => ({ populations: { 'initial-population': Object.values(cells).reduce((a, b) => a + b, 0) }, strata: { category: { negative: { 'initial-population': 5 }, positive_other: { 'initial-population': 2 }, positive_incomplete: { 'initial-population': 3 }, }, 'age_group-category': Object.fromEntries( Object.entries(cells).map(([k, n]) => [k, { 'initial-population': n }]), ), }, }); const counts: Mode = { kind: 'counts', label: 'General', measure: 'M', catalog: HPV, hidden: { codes: ['positive_incomplete', 'inconsistent'], unit: 'reportes' }, }; it('does not list a hidden category but keeps it in the total and the percentages', () => { const t = bars(counts, () => reading({}))!; expect(t.rows.map((x) => x.label)).toEqual(['Negativo', 'Positivo sin genotipo']); expect(t.rows.map((x) => x.cells)).toEqual([ ['5', '50.0 %'], ['2', '20.0 %'], ]); // of 10 reports expect(t.note).toBe( '3 reportes (Positivo con genotipificación incompleta) no se muestran desglosados; están incluidos en el total y en los porcentajes.', ); }); it('says nothing when no report is hidden', () => { const r: Reading = { populations: {}, strata: { category: { negative: { 'initial-population': 1 } } }, }; expect(hiddenNote(counts, () => r)).toBeUndefined(); }); it('a figure by groups is not available without its breakdown, and counts every record of the group on request', () => { const mode = { kind: 'figure', label: 'Por grupo de edad', measure: 'M', figure: 'scenarios', stratum: 'age_group', catalog: HPV, } as const; const noBreakdown: Reading = { populations: { 'initial-population': 4 }, strata: { category: {} }, }; expect(unavailable(mode, () => noBreakdown)).toBe( 'No disponible: el informe de este periodo no publica el desglose «Por grupo de edad».', ); const r: Reading = { populations: { 'initial-population': 10 }, strata: { 'age_group-category': { '25-34|negative': { 'initial-population': 4 }, '25-34|positive_other': { 'initial-population': 1 }, '25-34|positive_incomplete': { 'initial-population': 5 }, }, }, }; const label = (c: string) => c; const own = scenarioBars(r, { groups: [], categories: HPV }, 'age_group', label, ['25-34']); const all = scenarioBars(r, { groups: [], categories: HPV }, 'age_group', label, ['25-34'], { ofAll: true, }); expect((own.data[0] as any).x).toEqual([80]); // 4 of the 5 the catalog draws expect((all.data[0] as any).x).toEqual([40]); // 4 of the group's 10 reports expect((all.data[0] as any).y).toEqual(['25-34
Reportes: 10']); }); }); describe('independent bars by group', () => { const cats = [ { code: 'NILM', label: 'Normal', color: '#408651' }, { code: 'HSIL', label: 'LIEAG', color: '#CE4778' }, ]; const mode = (more: object = {}): Mode => ({ kind: 'grouped', label: 'x', measure: 'M', catalog: cats, stratum: 'round', order: ['initial', 'subsequent', 'no_data'], unit: 'pacientes', ...more, }) as Mode; const reading = (cells: Record, total?: number): Reading => ({ populations: { 'initial-population': total ?? Object.values(cells).reduce((a, b) => a + b, 0) }, strata: { 'round-category': Object.fromEntries( Object.entries(cells).map(([k, n]) => [k, { 'initial-population': n }]), ), }, }); it('draws one bar per category in each group: count, denominator and percent over its own records', () => { const g = grouped(mode(), () => reading({ 'initial|NILM': 3, 'initial|HSIL': 1, 'subsequent|NILM': 6 }), )!; expect(g.groups.map((x) => [x.label, x.total])).toEqual([ ['Detección inicial', 4], ['Detección subsecuente', 6], ]); expect(g.groups[0].rows.map((r) => [r.label, r.text, r.percent, r.color])).toEqual([ ['Normal', '3/4 · 75.0 %', 75, '#408651'], ['LIEAG', '1/4 · 25.0 %', 25, '#CE4778'], ]); expect(g.groups[1].rows.map((r) => r.text)).toEqual(['6/6 · 100.0 %', '0/6 · 0.0 %']); // a zero numerator is 0 % }); it('an empty group has no percent; with complete it is drawn only when the counts reconcile', () => { const empty = grouped(mode({ complete: true }), () => reading({ 'subsequent|NILM': 2, 'no_data|HSIL': 1 }), )!; expect(empty.groups.map((x) => x.code)).toEqual(['initial', 'subsequent', 'no_data']); expect(empty.groups[0]).toMatchObject({ total: 0, hint: 'Sin pacientes en el grupo' }); expect(empty.groups[0].rows.map((r) => [r.text, r.percent])).toEqual([ ['0/0 · —', undefined], ['0/0 · —', undefined], ]); expect(empty.note).toContain('una categoría en cero significa'); const unreconciled = grouped(mode({ complete: true }), () => reading({ 'subsequent|NILM': 2 }, 5), )!; expect(unreconciled.groups.map((x) => x.code)).toEqual(['subsequent']); expect(unreconciled.note).toContain('no concilian'); const open = grouped(mode(), () => reading({ 'subsequent|NILM': 2 }))!; expect(open.groups.map((x) => x.code)).toEqual(['subsequent']); // without complete: only the listed groups }); it('each group may count all its records, hidden categories included', () => { const r = reading({ 'initial|NILM': 2, 'initial|positive_incomplete': 2 }); const own = grouped(mode(), () => r)!; const all = grouped(mode({ ofAll: true }), () => r)!; expect(own.groups[0].total).toBe(2); expect(all.groups[0].total).toBe(4); expect(all.groups[0].rows[0].text).toBe('2/4 · 50.0 %'); }); it('is not available without the breakdown, and draws nothing without records', () => { const none: Reading = { populations: { 'initial-population': 4 }, strata: {} }; expect(grouped(mode(), () => none)).toBeUndefined(); expect(unavailable(mode(), () => none)).toContain('No disponible'); expect(grouped(mode(), () => reading({}, 0))).toBeUndefined(); }); it('says that unclassified records leave a classified group empty, and warns when none is classified', () => { const lone = grouped(mode({ complete: true }), () => reading({ 'no_data|NILM': 3 }))!; expect(lone.note).toContain('información no registrada'); }); it('a proportion split into its parts shows both in units and shares', () => { const r: Reading = { populations: { numerator: 1, denominator: 4 }, strata: {} }; const t = bars({ kind: 'split', label: 'x', measure: 'M', yes: 'Con', no: 'Sin' }, () => r)!; expect(t.rows.map((x) => [x.label, ...x.cells])).toEqual([ ['Con', '1', '25.0 %'], ['Sin', '3', '75.0 %'], ]); }); it('rows with patients and no biopsy are not rows with no patients; malignant counts only graded', () => { const r: Reading = { populations: {}, strata: { birads: { '4A': { 'initial-population': 3 }, '5': { 'initial-population': 4 }, '6': { 'initial-population': 2 }, }, 'birads-category': { '4A|no_biopsy': { 'initial-population': 3 }, '5|benign': { 'initial-population': 1 }, '5|invasive': { 'initial-population': 1 }, '5|no_data': { 'initial-population': 2 }, '6|no_data': { 'initial-population': 2 }, }, }, }; const results = [ { code: 'benign', label: 'Benigno', color: '#1' }, { code: 'invasive', label: 'Invasor', color: '#2' }, { code: 'no_biopsy', label: 'Sin biopsia', color: '#3' }, { code: 'no_data', label: 'Sin dato', color: '#4' }, ]; const rows = ['4A', '5', '6', '4B'].map((code) => ({ code, label: code, color: '#0' })); const mode = { kind: 'outcomes', label: 'x', measure: 'M', stratum: 'birads', rows, results, malignant: ['invasive'], }; const t = bars(mode as any, () => r)!; expect(t.rows.map((x) => x.cells)).toEqual([ ['3', '0', '0', '3', '0', 'Sin biopsias'], ['4', '1', '1', '0', '2', '1/2 (50.0 %)'], ['2', '0', '0', '0', '2', 'Sin biopsias clasificables'], ['0', '—', '—', '—', '—', '—'], ]); expect(t.rows.map((x) => x.none)).toEqual([ undefined, undefined, undefined, 'Sin pacientes en el periodo', ]); expect(t.rows[1].segments!.map((x) => x.width)).toEqual([25, 25, 0, 50]); }); it('bars on a percent scale are as long as the shares shown', () => { const r: Reading = { populations: {}, strata: { category: { a: { 'initial-population': 1 }, b: { 'initial-population': 3 } } }, }; const catalog = [ { code: 'a', label: 'A', color: '#0' }, { code: 'b', label: 'B', color: '#1' }, { code: 'c', label: 'C', color: '#2' }, ]; const t = bars({ kind: 'counts', label: 'x', measure: 'M', catalog, percent: true }, () => r)!; expect(t.rows.map((x) => x.width)).toEqual([25, 75, 0]); expect(t.rows.map((x) => x.cells)).toEqual([ ['1', '25.0 %'], ['3', '75.0 %'], ['0', '0.0 %'], ]); }); it('a split takes the colours it is given', () => { const r: Reading = { populations: { numerator: 1, denominator: 4 }, strata: {} }; const mode = { kind: 'split', label: 'x', measure: 'M', yes: 'Con', no: 'Sin', colors: ['#a', '#b'], }; expect(bars(mode as any, () => r)!.rows.map((x) => x.color)).toEqual(['#a', '#b']); }); });