|
| 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 | + |
| 36 | +const DESKTOP_WIDTH = window.innerWidth; |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + cleanup(); |
| 40 | + (window as any).innerWidth = DESKTOP_WIDTH; |
| 41 | +}); |
| 42 | + |
| 43 | +// The legend payload registers from a layout effect; read it after a tick. |
| 44 | +const settle = () => act(async () => { await new Promise((r) => setTimeout(r, 60)); }); |
| 45 | + |
| 46 | +const swatches = (c: HTMLElement) => |
| 47 | + Array.from(c.querySelectorAll<HTMLElement>('.recharts-legend-wrapper .rounded-\\[2px\\]')); |
| 48 | + |
| 49 | +const categoryTicks = (c: HTMLElement) => |
| 50 | + Array.from(c.querySelectorAll<SVGTextElement>('.recharts-yAxis .recharts-cartesian-axis-tick-value, .recharts-cartesian-axis-tick-labels text')); |
| 51 | + |
| 52 | +describe('month bucket keys on the category axis (row 15)', () => { |
| 53 | + it('reads `2025-10` as `Oct 2025`', () => { |
| 54 | + const { container } = render( |
| 55 | + <AdvancedChartImpl |
| 56 | + chartType="line" |
| 57 | + data={[{ month: '2025-10', n: 3 }, { month: '2025-11', n: 5 }, { month: '2025-12', n: 4 }]} |
| 58 | + xAxisKey="month" |
| 59 | + series={[{ dataKey: 'n', label: 'Count' }]} |
| 60 | + isAnimationActive={false} |
| 61 | + />, |
| 62 | + ); |
| 63 | + const text = container.textContent ?? ''; |
| 64 | + expect(text).toContain('Oct 2025'); |
| 65 | + expect(text).toContain('Dec 2025'); |
| 66 | + expect(text).not.toContain('2025-10'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('CONTROL: a category that only looks partly like a date is drawn as written', () => { |
| 70 | + const { container } = render( |
| 71 | + <AdvancedChartImpl |
| 72 | + chartType="bar" |
| 73 | + data={[{ q: '2025-Q4', n: 3 }, { q: '2026-Q1', n: 5 }]} |
| 74 | + xAxisKey="q" |
| 75 | + series={[{ dataKey: 'n', label: 'Count' }]} |
| 76 | + isAnimationActive={false} |
| 77 | + />, |
| 78 | + ); |
| 79 | + expect(container.textContent).toContain('2025-Q4'); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('legend swatches (row 15)', () => { |
| 84 | + const compared = [ |
| 85 | + { dataKey: 'n', label: 'Count', variant: 'current' }, |
| 86 | + { dataKey: 'n__compare', label: 'Count · vs last quarter', variant: 'comparison' }, |
| 87 | + ]; |
| 88 | + const rows = [{ stage: 'Prospecting', n: 3, n__compare: 2 }, { stage: 'Proposal', n: 5, n__compare: 4 }]; |
| 89 | + |
| 90 | + it('a bar chart draws a colour on every swatch, never a gradient reference', async () => { |
| 91 | + const { container } = render( |
| 92 | + <AdvancedChartImpl chartType="bar" data={rows} xAxisKey="stage" series={compared} isAnimationActive={false} />, |
| 93 | + ); |
| 94 | + await settle(); |
| 95 | + const drawn = swatches(container); |
| 96 | + expect(drawn).toHaveLength(2); |
| 97 | + for (const swatch of drawn) { |
| 98 | + const color = swatch.style.getPropertyValue('--color-bg'); |
| 99 | + expect(color).not.toBe(''); |
| 100 | + expect(color).not.toMatch(/^url\(/); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it.each(['bar', 'line', 'area'])('%s: the comparison swatch is hollow and the primary one solid, in the same colour', async (chartType) => { |
| 105 | + const { container } = render( |
| 106 | + <AdvancedChartImpl chartType={chartType} data={rows} xAxisKey="stage" series={compared} isAnimationActive={false} />, |
| 107 | + ); |
| 108 | + await settle(); |
| 109 | + const [primary, comparison] = swatches(container); |
| 110 | + expect(primary.classList.contains('bg-(--color-bg)')).toBe(true); |
| 111 | + expect(primary.getAttribute('data-swatch')).toBeNull(); |
| 112 | + expect(comparison.getAttribute('data-swatch')).toBe('comparison'); |
| 113 | + expect(comparison.classList.contains('bg-transparent')).toBe(true); |
| 114 | + expect(comparison.classList.contains('border-(--color-bg)')).toBe(true); |
| 115 | + // The overlay mirrors its primary's colour; the swatch shape tells them apart. |
| 116 | + expect(comparison.style.getPropertyValue('--color-bg')).toBe(primary.style.getPropertyValue('--color-bg')); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('horizontal bar category labels on a phone (row 18)', () => { |
| 121 | + const asPhone = () => { |
| 122 | + (window as any).innerWidth = 390; |
| 123 | + window.dispatchEvent(new Event('resize')); |
| 124 | + }; |
| 125 | + const statuses = [ |
| 126 | + { status: 'In Progress', n: 4 }, |
| 127 | + { status: 'Backlog', n: 2 }, |
| 128 | + { status: 'Waiting on the customer to confirm the delivery window', n: 1 }, |
| 129 | + ]; |
| 130 | + |
| 131 | + it('draws "In Progress" whole, not cut to "In Progr…"', () => { |
| 132 | + asPhone(); |
| 133 | + const { container } = render( |
| 134 | + <AdvancedChartImpl chartType="horizontal-bar" data={statuses} xAxisKey="status" series={[{ dataKey: 'n', label: 'Tasks' }]} isAnimationActive={false} />, |
| 135 | + ); |
| 136 | + const texts = categoryTicks(container).map((t) => Array.from(t.querySelectorAll('tspan')).map((s) => s.textContent).join(' ')); |
| 137 | + expect(texts).toContain('In Progress'); |
| 138 | + expect(container.textContent).not.toContain('In Progr…'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('wraps a long label to at most two lines and carries the full label as its tooltip', () => { |
| 142 | + asPhone(); |
| 143 | + const { container } = render( |
| 144 | + <AdvancedChartImpl chartType="horizontal-bar" data={statuses} xAxisKey="status" series={[{ dataKey: 'n', label: 'Tasks' }]} isAnimationActive={false} />, |
| 145 | + ); |
| 146 | + const long = categoryTicks(container).find((t) => t.querySelector('title')); |
| 147 | + expect(long).toBeDefined(); |
| 148 | + expect(long!.querySelector('title')!.textContent).toBe('Waiting on the customer to confirm the delivery window'); |
| 149 | + const lines = Array.from(long!.querySelectorAll('tspan')).map((s) => s.textContent ?? ''); |
| 150 | + expect(lines).toHaveLength(2); |
| 151 | + expect(lines[1].endsWith('…')).toBe(true); |
| 152 | + // A label that fits carries no tooltip. |
| 153 | + const backlog = categoryTicks(container).find((t) => t.textContent === 'Backlog'); |
| 154 | + expect(backlog?.querySelector('title')).toBeNull(); |
| 155 | + }); |
| 156 | +}); |
0 commit comments