|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * Unit tests for the scatter / treemap / sankey drill-event mappers. These lock |
| 6 | + * the Recharts-payload → { category, series, value } contract that ObjectChart's |
| 7 | + * drill drawer depends on. |
| 8 | + */ |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { mapScatterClick, mapTreemapClick, mapSankeyClick } from '../chartDrillEvents'; |
| 11 | + |
| 12 | +const series = [{ dataKey: 'sales' }]; |
| 13 | + |
| 14 | +describe('mapScatterClick', () => { |
| 15 | + it('reads category (xAxisKey) and value from node.payload', () => { |
| 16 | + const node = { payload: { region: 'West', sales: 4200 } }; |
| 17 | + expect(mapScatterClick(node, 'region', series)).toEqual({ |
| 18 | + category: 'West', |
| 19 | + series: 'sales', |
| 20 | + value: 4200, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('falls back to top-level node fields when no payload', () => { |
| 25 | + const node = { region: 'East', sales: 9 }; |
| 26 | + expect(mapScatterClick(node, 'region', series)).toEqual({ |
| 27 | + category: 'East', |
| 28 | + series: 'sales', |
| 29 | + value: 9, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('coerces a non-string category and omits a non-numeric value', () => { |
| 34 | + const node = { payload: { region: 2025, sales: 'n/a' } }; |
| 35 | + expect(mapScatterClick(node, 'region', series)).toEqual({ |
| 36 | + category: '2025', |
| 37 | + series: 'sales', |
| 38 | + value: undefined, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns null when there is no category', () => { |
| 43 | + expect(mapScatterClick({ payload: { sales: 1 } }, 'region', series)).toBeNull(); |
| 44 | + expect(mapScatterClick(null, 'region', series)).toBeNull(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('defaults the series key to "value"', () => { |
| 48 | + expect(mapScatterClick({ x: 'A', value: 3 }, 'x', undefined)?.series).toBe('value'); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('mapTreemapClick', () => { |
| 53 | + it('maps tile name + value', () => { |
| 54 | + expect(mapTreemapClick({ name: 'Tech', value: 1200 }, series)).toEqual({ |
| 55 | + category: 'Tech', |
| 56 | + series: 'sales', |
| 57 | + value: 1200, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('falls back to size, then payload.name', () => { |
| 62 | + expect(mapTreemapClick({ name: 'A', size: 7 }, series)?.value).toBe(7); |
| 63 | + expect(mapTreemapClick({ payload: { name: 'B' } }, series)?.category).toBe('B'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns null without a name', () => { |
| 67 | + expect(mapTreemapClick({ value: 1 }, series)).toBeNull(); |
| 68 | + expect(mapTreemapClick(null, series)).toBeNull(); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('mapSankeyClick', () => { |
| 73 | + it('drills on a category node', () => { |
| 74 | + expect(mapSankeyClick({ payload: { name: 'Web', depth: 1, value: 500 } }, series)).toEqual({ |
| 75 | + category: 'Web', |
| 76 | + series: 'sales', |
| 77 | + value: 500, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('ignores the synthetic root node (depth 0)', () => { |
| 82 | + expect(mapSankeyClick({ payload: { name: 'Total', depth: 0 } }, series)).toBeNull(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('ignores links (no name)', () => { |
| 86 | + expect(mapSankeyClick({ payload: { source: 0, target: 1, value: 5 } }, series)).toBeNull(); |
| 87 | + expect(mapSankeyClick(null, series)).toBeNull(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments