import { bars, Mode } from './bars'; import { Reading } from './measures'; const read = (r: Reading) => () => r; const cells = (mode: Mode, r: Reading) => bars(mode, read(r))!.rows.map((x) => x.cells); 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' }]; expect(cells({ kind: 'counts', label: 'x', measure: 'M', catalog }, r)).toEqual([ ['3', '75.0 %'], ['1', '25.0 %'], ]); }); it('add up an aggregate after the first row and indent its parts', () => { const r: Reading = { populations: {}, strata: { category: { n: { 'initial-population': 6 }, a: { 'initial-population': 3 }, b: { 'initial-population': 1 }, }, }, }; const catalog = ['n', 'a', 'b'].map((code) => ({ code, label: code, color: '#000' })); const aggregate = { label: 'Anormal', color: '#f00', codes: ['a', 'b'] }; const t = bars({ kind: 'counts', label: 'x', measure: 'M', catalog, aggregate }, read(r))!; expect(t.rows.map((x) => [x.label, x.cells[0], !!x.sub])).toEqual([ ['n', '6', false], ['Anormal', '4', false], ['a', '3', true], ['b', '1', true], ]); }); it('draw a proportion with its interval as marks', () => { const r: Reading = { populations: { numerator: 51, denominator: 70 }, strata: {}, score: 0.729, low: 0.615, high: 0.819, }; const t = bars({ kind: 'ratio', label: 'Total', measure: 'M' }, read(r))!; expect(t.rows[0].cells).toEqual(['51/70', '72.9 %', '61.5 %–81.9 %']); expect(t.rows[0].marks.map(Math.round)).toEqual([62, 82]); }); it('stagger steps from where the step they follow ends', () => { const at: Record = { A: { populations: { 'measure-observation': 3 }, strata: {}, score: 10 }, B: { populations: { 'measure-observation': 2 }, strata: {}, score: 30 }, C: { populations: { 'measure-observation': 1 }, strata: {}, score: 20 }, }; const steps = [ { label: 'a', measure: 'A' }, { label: 'b', measure: 'B' }, { label: 'c', measure: 'C', after: 0 }, { label: 'd', measure: 'D' }, ]; const mode: Mode = { kind: 'days', label: 'x', steps, staggered: true, scale: true }; const t = bars(mode, (m) => at[m])!; expect(t.rows.map((x) => x.offset)).toEqual([0, 25, 25, undefined]); expect(t.rows[3].none).toBe('Sin mediciones en el periodo'); expect(t.note).toBe('Escala: 0 a 40 días.'); }); });