|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Dashboard chart readability (objectstack#27520 rows 15 and 18). |
| 11 | + * |
| 12 | + * - A month bucket key (`2025-10`) reads `Oct 2025` on the category axis. |
| 13 | + * - The legend draws a swatch for a bar series (whose mark fills from a |
| 14 | + * gradient, so recharts hands the legend a `url(#…)` it cannot draw), and a |
| 15 | + * comparison series' swatch is hollow, apart from the primary's solid one. |
| 16 | + * - A horizontal bar's category label is drawn in full on a phone, wrapped |
| 17 | + * into the axis width, instead of cut to eight characters; a label that |
| 18 | + * still has to be cut carries the full text as its tooltip. |
| 19 | + */ |
| 20 | + |
| 21 | +import React from 'react'; |
| 22 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 23 | +import { render, cleanup, act } from '@testing-library/react'; |
| 24 | + |
| 25 | +vi.mock('recharts', async () => { |
| 26 | + const actual = await vi.importActual<any>('recharts'); |
| 27 | + return { |
| 28 | + ...actual, |
| 29 | + ResponsiveContainer: ({ children }: any) => |
| 30 | + React.cloneElement(children, { width: 360, height: 320 }), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +import AdvancedChartImpl from './AdvancedChartImpl'; |
| 35 | +import { ChartContainer, ChartTooltipContent } from './ChartContainerImpl'; |
| 36 | + |
| 37 | +const DESKTOP_WIDTH = window.innerWidth; |
| 38 | + |
| 39 | +afterEach(() => { |
| 40 | + cleanup(); |
| 41 | + (window as any).innerWidth = DESKTOP_WIDTH; |
| 42 | +}); |
| 43 | + |
| 44 | +// The legend payload registers from a layout effect; read it after a tick. |
| 45 | +const settle = () => act(async () => { await new Promise((r) => setTimeout(r, 60)); }); |
| 46 | + |
| 47 | +const swatches = (c: HTMLElement) => |
| 48 | + Array.from(c.querySelectorAll<HTMLElement>('.recharts-legend-wrapper .rounded-\\[2px\\]')); |
| 49 | + |
| 50 | +const categoryTicks = (c: HTMLElement) => |
| 51 | + Array.from(c.querySelectorAll<SVGTextElement>('.recharts-yAxis .recharts-cartesian-axis-tick-value, .recharts-cartesian-axis-tick-labels text')); |
| 52 | + |
| 53 | +describe('month bucket keys on the category axis (row 15)', () => { |
| 54 | + it('reads `2025-10` as `Oct 2025`', () => { |
| 55 | + const { container } = render( |
| 56 | + <AdvancedChartImpl |
| 57 | + chartType="line" |
| 58 | + data={[{ month: '2025-10', n: 3 }, { month: '2025-11', n: 5 }, { month: '2025-12', n: 4 }]} |
| 59 | + xAxisKey="month" |
| 60 | + series={[{ dataKey: 'n', label: 'Count' }]} |
| 61 | + isAnimationActive={false} |
| 62 | + />, |
| 63 | + ); |
| 64 | + const text = container.textContent ?? ''; |
| 65 | + expect(text).toContain('Oct 2025'); |
| 66 | + expect(text).toContain('Dec 2025'); |
| 67 | + expect(text).not.toContain('2025-10'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('CONTROL: a category that only looks partly like a date is drawn as written', () => { |
| 71 | + const { container } = render( |
| 72 | + <AdvancedChartImpl |
| 73 | + chartType="bar" |
| 74 | + data={[{ q: '2025-Q4', n: 3 }, { q: '2026-Q1', n: 5 }]} |
| 75 | + xAxisKey="q" |
| 76 | + series={[{ dataKey: 'n', label: 'Count' }]} |
| 77 | + isAnimationActive={false} |
| 78 | + />, |
| 79 | + ); |
| 80 | + expect(container.textContent).toContain('2025-Q4'); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('legend swatches (row 15)', () => { |
| 85 | + const compared = [ |
| 86 | + { dataKey: 'n', label: 'Count', variant: 'current' as const }, |
| 87 | + { dataKey: 'n__compare', label: 'Count · vs last quarter', variant: 'comparison' as const }, |
| 88 | + ]; |
| 89 | + const rows = [{ stage: 'Prospecting', n: 3, n__compare: 2 }, { stage: 'Proposal', n: 5, n__compare: 4 }]; |
| 90 | + |
| 91 | + it('a bar chart draws a colour on every swatch, never a gradient reference', async () => { |
| 92 | + const { container } = render( |
| 93 | + <AdvancedChartImpl chartType="bar" data={rows} xAxisKey="stage" series={compared} isAnimationActive={false} />, |
| 94 | + ); |
| 95 | + await settle(); |
| 96 | + const drawn = swatches(container); |
| 97 | + expect(drawn).toHaveLength(2); |
| 98 | + for (const swatch of drawn) { |
| 99 | + const color = swatch.style.getPropertyValue('--color-bg'); |
| 100 | + expect(color).not.toBe(''); |
| 101 | + expect(color).not.toMatch(/^url\(/); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it.each(['bar', 'line', 'area'] as const)('%s: the comparison swatch is hollow and the primary one solid, in the same colour', async (chartType) => { |
| 106 | + const { container } = render( |
| 107 | + <AdvancedChartImpl chartType={chartType} data={rows} xAxisKey="stage" series={compared} isAnimationActive={false} />, |
| 108 | + ); |
| 109 | + await settle(); |
| 110 | + const [primary, comparison] = swatches(container); |
| 111 | + expect(primary.classList.contains('bg-(--color-bg)')).toBe(true); |
| 112 | + expect(primary.getAttribute('data-swatch')).toBeNull(); |
| 113 | + expect(comparison.getAttribute('data-swatch')).toBe('comparison'); |
| 114 | + expect(comparison.classList.contains('bg-transparent')).toBe(true); |
| 115 | + expect(comparison.classList.contains('border-(--color-bg)')).toBe(true); |
| 116 | + // The overlay mirrors its primary's colour; the swatch shape tells them apart. |
| 117 | + expect(comparison.style.getPropertyValue('--color-bg')).toBe(primary.style.getPropertyValue('--color-bg')); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('horizontal bar category labels on a phone (row 18)', () => { |
| 122 | + const asPhone = () => { |
| 123 | + (window as any).innerWidth = 390; |
| 124 | + window.dispatchEvent(new Event('resize')); |
| 125 | + }; |
| 126 | + const statuses = [ |
| 127 | + { status: 'In Progress', n: 4 }, |
| 128 | + { status: 'Backlog', n: 2 }, |
| 129 | + { status: 'Waiting on the customer to confirm the delivery window', n: 1 }, |
| 130 | + ]; |
| 131 | + |
| 132 | + it('draws "In Progress" whole, not cut to "In Progr…"', () => { |
| 133 | + asPhone(); |
| 134 | + const { container } = render( |
| 135 | + <AdvancedChartImpl chartType="horizontal-bar" data={statuses} xAxisKey="status" series={[{ dataKey: 'n', label: 'Tasks' }]} isAnimationActive={false} />, |
| 136 | + ); |
| 137 | + const texts = categoryTicks(container).map((t) => Array.from(t.querySelectorAll('tspan')).map((s) => s.textContent).join(' ')); |
| 138 | + expect(texts).toContain('In Progress'); |
| 139 | + expect(container.textContent).not.toContain('In Progr…'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('wraps a long label to at most two lines and carries the full label as its tooltip', () => { |
| 143 | + asPhone(); |
| 144 | + const { container } = render( |
| 145 | + <AdvancedChartImpl chartType="horizontal-bar" data={statuses} xAxisKey="status" series={[{ dataKey: 'n', label: 'Tasks' }]} isAnimationActive={false} />, |
| 146 | + ); |
| 147 | + const long = categoryTicks(container).find((t) => t.querySelector('title')); |
| 148 | + expect(long).toBeDefined(); |
| 149 | + expect(long!.querySelector('title')!.textContent).toBe('Waiting on the customer to confirm the delivery window'); |
| 150 | + const lines = Array.from(long!.querySelectorAll('tspan')).map((s) => s.textContent ?? ''); |
| 151 | + expect(lines).toHaveLength(2); |
| 152 | + expect(lines[1].endsWith('…')).toBe(true); |
| 153 | + // A label that fits carries no tooltip. |
| 154 | + const backlog = categoryTicks(container).find((t) => t.textContent === 'Backlog'); |
| 155 | + expect(backlog?.querySelector('title')).toBeNull(); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +describe('vertical bar category labels on a phone (row 18)', () => { |
| 160 | + const asPhone = () => { |
| 161 | + (window as any).innerWidth = 390; |
| 162 | + window.dispatchEvent(new Event('resize')); |
| 163 | + }; |
| 164 | + const xTicks = (c: HTMLElement) => |
| 165 | + Array.from(c.querySelectorAll<SVGTextElement>('.recharts-xAxis-tick-labels .recharts-cartesian-axis-tick-value')); |
| 166 | + |
| 167 | + it('a few buckets rotate and draw "In Progress" whole, where the phone cut drew "In Progr…"', () => { |
| 168 | + asPhone(); |
| 169 | + const { container } = render( |
| 170 | + <AdvancedChartImpl |
| 171 | + chartType="bar" |
| 172 | + data={[{ s: 'Backlog', n: 2 }, { s: 'To Do', n: 2 }, { s: 'In Progress', n: 2 }, { s: 'In Review', n: 2 }, { s: 'Done', n: 2 }]} |
| 173 | + xAxisKey="s" |
| 174 | + series={[{ dataKey: 'n', label: 'Tasks' }]} |
| 175 | + isAnimationActive={false} |
| 176 | + />, |
| 177 | + ); |
| 178 | + const labels = xTicks(container).map((t) => t.textContent); |
| 179 | + expect(labels).toContain('In Progress'); |
| 180 | + expect(container.textContent).not.toContain('In Progr…'); |
| 181 | + }); |
| 182 | + |
| 183 | + it('a label still cut carries the full label as its tooltip; a whole one carries none', () => { |
| 184 | + const long = 'Waiting on the customer to confirm'; |
| 185 | + const { container } = render( |
| 186 | + <AdvancedChartImpl |
| 187 | + chartType="bar" |
| 188 | + data={[{ s: long, n: 2 }, { s: 'Done', n: 3 }]} |
| 189 | + xAxisKey="s" |
| 190 | + series={[{ dataKey: 'n', label: 'Tasks' }]} |
| 191 | + isAnimationActive={false} |
| 192 | + />, |
| 193 | + ); |
| 194 | + const ticks = Array.from(container.querySelectorAll('.recharts-xAxis-tick-labels .recharts-cartesian-axis-tick-label')); |
| 195 | + const cut = ticks.find((t) => (t.textContent ?? '').includes('…')); |
| 196 | + expect(cut, 'the long label was not cut, so this measures nothing').toBeDefined(); |
| 197 | + expect(cut!.querySelector('title')?.textContent).toBe(long); |
| 198 | + const whole = ticks.find((t) => t.querySelector('.recharts-cartesian-axis-tick-value')?.textContent === 'Done'); |
| 199 | + expect(whole?.querySelector('title')).toBeNull(); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
| 203 | +describe('a lone bar series painted per category (row 15 control)', () => { |
| 204 | + it('draws its legend label with no swatch, since no one colour names it', async () => { |
| 205 | + const { container } = render( |
| 206 | + <AdvancedChartImpl |
| 207 | + chartType="bar" |
| 208 | + data={[{ h: 'green', n: 3 }, { h: 'red', n: 1 }]} |
| 209 | + xAxisKey="h" |
| 210 | + series={[{ dataKey: 'n', label: 'Projects' }]} |
| 211 | + config={{ n: { label: 'Projects' } }} |
| 212 | + categoryColors={{ green: '#22c55e', red: '#ef4444' }} |
| 213 | + isAnimationActive={false} |
| 214 | + />, |
| 215 | + ); |
| 216 | + await settle(); |
| 217 | + expect(container.querySelector('.recharts-legend-wrapper')?.textContent).toContain('Projects'); |
| 218 | + expect(swatches(container)).toHaveLength(0); |
| 219 | + }); |
| 220 | +}); |
| 221 | + |
| 222 | + |
| 223 | +describe('the tooltip header names a month bucket the way the axis does (objectstack#27520 row 15)', () => { |
| 224 | + const header = (label: string) => { |
| 225 | + const { container } = render( |
| 226 | + <ChartContainer config={{ amount: { label: 'Amount' } }}> |
| 227 | + <ChartTooltipContent active label={label} payload={[{ value: 5, name: 'amount', dataKey: 'amount', payload: {} }]} /> |
| 228 | + </ChartContainer>, |
| 229 | + ); |
| 230 | + return container.querySelector('.font-medium')?.textContent; |
| 231 | + }; |
| 232 | + |
| 233 | + it('reads `Oct 2025` for the bucket key `2025-10`', () => { |
| 234 | + expect(header('2025-10')).toBe('Oct 2025'); |
| 235 | + }); |
| 236 | + |
| 237 | + it('CONTROL: any other category reads as written', () => { |
| 238 | + expect(header('2025-Q4')).toBe('2025-Q4'); |
| 239 | + expect(header('In Progress')).toBe('In Progress'); |
| 240 | + }); |
| 241 | +}); |
0 commit comments