import { FigureHighlight, highlightAt, highlightFigure, highlightLabel, highlightRows, toggleHighlight } from './indicadores-highlight'; import { PlotlyFigure } from './indicadores.models'; // A double donut like dona(): 0 centre, 1 groups, 2 categories. const figure: PlotlyFigure = { data: [ { type: 'pie', values: [1], text: ['10
TOTAL'] }, { type: 'pie', values: [6, 4], marker: { colors: ['rgba(10, 20, 30, 1)', '#FF0000'] } }, { type: 'pie', values: [2, 4, 4], marker: { colors: ['#000000', '#111111', '#222222'] } }, ], layout: {} }; const h: FigureHighlight = { traces: [1, 2], center: { trace: 0 }, allLabel: 'TOTAL', groups: [{ id: 'A', label: 'Grupo A', color: '#000000', points: [{ trace: 1, index: 0 }, { trace: 2, index: 0 }, { trace: 2, index: 1 }], center: 'A' }, { id: 'B', label: 'Grupo B', color: '#FF0000', points: [{ trace: 1, index: 1 }, { trace: 2, index: 2 }], center: 'B' }], items: [{ id: 'a1', label: 'A · 1', short: '1', color: '#000000', group: 'A', points: [{ trace: 2, index: 0 }], center: 'a1' }, { id: 'a2', label: 'A · 2', short: '2', color: '#111111', group: 'A', points: [{ trace: 2, index: 1 }] }, { id: 'b1', label: 'B · 1', short: '1', color: '#222222', group: 'B', points: [{ trace: 2, index: 2 }] }] }; const colors = (f: PlotlyFigure, t: number) => (f.data[t]['marker'] as { colors: string[] }).colors; describe('Botones que resaltan una parte de la figura', () => { it('sin selección deja la figura intacta', () => { expect(highlightFigure(figure, h, {})).toBe(figure); expect(highlightLabel(h, {})).toBe('TOTAL'); }); it('un grupo conserva su parte de ambos anillos y atenúa el resto, también colores rgba', () => { const f = highlightFigure(figure, h, { group: 'B' }); expect(colors(f, 1)).toEqual(['rgba(10, 20, 30, 0.3)', '#FF0000']); expect(colors(f, 2)).toEqual(['rgba(0, 0, 0, 0.3)', 'rgba(17, 17, 17, 0.3)', '#222222']); expect(f.data[0]['text']).toEqual(['B']); expect(colors(figure, 2)).toEqual(['#000000', '#111111', '#222222']); }); it('una categoría conserva su grupo en el anillo interior', () => { const f = highlightFigure(figure, h, { item: 'a1' }); expect(colors(f, 1)).toEqual(['rgba(10, 20, 30, 1)', 'rgba(255, 0, 0, 0.3)']); expect(colors(f, 2)).toEqual(['#000000', 'rgba(17, 17, 17, 0.3)', 'rgba(34, 34, 34, 0.3)']); expect(f.data[0]['text']).toEqual(['a1']); }); it('en barras atenúa con la opacidad de cada barra', () => { const bars: PlotlyFigure = { data: [{ type: 'bar', x: ['a', 'b', 'c'], y: [1, 2, 3], marker: { color: '#000000' } }], layout: {} }; const hb: FigureHighlight = { groups: [], traces: [0], allLabel: 'Todas', items: ['a', 'b', 'c'].map((id, i) => ({ id, label: id, color: '#000000', points: [{ trace: 0, index: i }] })) }; expect((highlightFigure(bars, hb, { item: 'b' }).data[0]['marker'] as { opacity: number[] }).opacity).toEqual([.3, 1, .3]); }); it('alterna como CaCu: repetir vuelve un paso y un grupo limita las categorías', () => { expect(toggleHighlight(h, {}, 'group', 'A')).toEqual({ group: 'A' }); expect(toggleHighlight(h, { group: 'A' }, 'group', 'A')).toEqual({}); expect(toggleHighlight(h, { group: 'A' }, 'item', 'a2')).toEqual({ group: 'A', item: 'a2' }); expect(toggleHighlight(h, { group: 'A', item: 'a2' }, 'item', 'a2')).toEqual({ group: 'A' }); expect(toggleHighlight(h, { group: 'A' }, 'item', 'b1')).toEqual({ group: 'A' }); expect(toggleHighlight(h, {}, 'item', 'b1')).toEqual({ item: 'b1' }); }); it('un clic en el anillo exterior es la categoría; en el interior, el grupo', () => { expect(highlightAt(h, { trace: 2, index: 1 })).toEqual({ kind: 'item', id: 'a2' }); expect(highlightAt(h, { trace: 1, index: 1 })).toEqual({ kind: 'group', id: 'B' }); expect(highlightAt(h, { trace: 0, index: 0 })).toBeUndefined(); }); it('agrupa los botones en filas: cada grupo seguido de sus categorías', () => { expect(highlightRows(h).map(r => [r.group?.id, r.items.map(i => i.id)])).toEqual([['A', ['a1', 'a2']], ['B', ['b1']]]); }); });