|
10 | 10 | import React from 'react'; |
11 | 11 | import { describe, it, expect, afterEach, vi } from 'vitest'; |
12 | 12 | import { render, cleanup } from '@testing-library/react'; |
| 13 | +import { formatMeasure } from '@object-ui/core'; |
| 14 | +import { LocalizationProvider } from '@object-ui/i18n'; |
13 | 15 |
|
14 | 16 | vi.mock('recharts', async () => { |
15 | 17 | const actual = await vi.importActual<any>('recharts'); |
@@ -134,3 +136,199 @@ describe('AdvancedChartImpl — value-axis ticks on a small domain (objectstack# |
134 | 136 | expect(ticks).toEqual(['0%', '0.85%', '1.7%', '2.55%', '3.4%']); |
135 | 137 | }); |
136 | 138 | }); |
| 139 | + |
| 140 | +/** |
| 141 | + * objectstack#37053 — a value axis prints its dataset measure's UNITS: a |
| 142 | + * whole-scale percent and a currency amount, beside a table that prints them |
| 143 | + * through `formatMeasure`. |
| 144 | + * |
| 145 | + * The chart's value-axis `format` key is a d3-style pattern (`formatterFor`): |
| 146 | + * it reads a `%` pattern as a 0–1 fraction and has no slot for an ISO |
| 147 | + * currency, so a report or dashboard chart over a `percentScale: 'whole'` rate |
| 148 | + * or a `CNY` amount printed bare numbers (`0` … `34`, `0` … `12K`) beside a |
| 149 | + * table printing `33.3%` and `¥12,000.00`. The producer now hands each series |
| 150 | + * the measure's `{ format, currency, percentScale }` (`series[].measureFormat`, |
| 151 | + * renderer-internal like `series[].aggregate`), and an axis whose series all |
| 152 | + * carry the same one prints that measure's units: |
| 153 | + * |
| 154 | + * - a percent pattern through `formatMeasure` itself, at the column's scale; |
| 155 | + * - a currency as the compact default carrying the currency's symbol. |
| 156 | + * |
| 157 | + * A per-row currency column (`currencyField`) has no single currency, so its |
| 158 | + * producer hands no `currency` and the axis keeps distinct plain numbers — the |
| 159 | + * control. Every chart here is built the way `ChartRenderer` builds it: the |
| 160 | + * schema through `normalizeChartSchema`, whose series reader is where the |
| 161 | + * carrier is kept or dropped. |
| 162 | + * |
| 163 | + * A new top-level `describe` in this axis-tick harness, beside the |
| 164 | + * objectstack#36988 one above it. |
| 165 | + */ |
| 166 | +describe('AdvancedChartImpl — a value axis prints its dataset measure’s units (objectstack#37053)', () => { |
| 167 | + /** The display locale the chart resolves with no provider mounted. */ |
| 168 | + const LOCALE = 'en'; |
| 169 | + |
| 170 | + const CATEGORIES = new Set(['High', 'Medium', 'Low']); |
| 171 | + |
| 172 | + /** The value tick labels the left axis draws, in order. */ |
| 173 | + const valueTicks = (c: HTMLElement) => |
| 174 | + [...c.querySelectorAll('text.recharts-cartesian-axis-tick-value')] |
| 175 | + .filter((t) => t.getAttribute('orientation') === 'left') |
| 176 | + .map((t) => (t.textContent ?? '').trim()) |
| 177 | + .filter((t) => !CATEGORIES.has(t)); |
| 178 | + |
| 179 | + /** The data labels the bars print (`showDataLabels`). */ |
| 180 | + const dataLabels = (c: HTMLElement) => |
| 181 | + [...c.querySelectorAll('.recharts-label-list text')].map((t) => (t.textContent ?? '').trim()); |
| 182 | + |
| 183 | + /** |
| 184 | + * Draw one column chart over `rows`, its `value` series carrying `measureFormat` |
| 185 | + * exactly as a producer hands it, through the translation `ChartRenderer` |
| 186 | + * applies to every schema. |
| 187 | + */ |
| 188 | + function draw( |
| 189 | + rows: Array<{ priority: string; value: number }>, |
| 190 | + measureFormat: Record<string, unknown> | undefined, |
| 191 | + extra: Record<string, unknown> = {}, |
| 192 | + locale?: string, |
| 193 | + ) { |
| 194 | + const spec = normalizeChartSchema({ |
| 195 | + type: 'chart', |
| 196 | + chartType: 'column', |
| 197 | + xAxisKey: 'priority', |
| 198 | + series: [{ dataKey: 'value', label: 'Value', ...(measureFormat ? { measureFormat } : {}) }], |
| 199 | + ...extra, |
| 200 | + }); |
| 201 | + const { container } = render( |
| 202 | + <AdvancedChartImpl |
| 203 | + chartType="column" |
| 204 | + data={rows} |
| 205 | + xAxisKey="priority" |
| 206 | + series={spec.series} |
| 207 | + yAxes={spec.yAxes} |
| 208 | + showDataLabels={spec.showDataLabels} |
| 209 | + isAnimationActive={false} |
| 210 | + />, |
| 211 | + // The display locale the chart reads (`useDisplayLocale`), when a case names one. |
| 212 | + locale |
| 213 | + ? { wrapper: ({ children }: { children: React.ReactNode }) => <LocalizationProvider value={{ locale }}>{children}</LocalizationProvider> } |
| 214 | + : undefined, |
| 215 | + ); |
| 216 | + return container; |
| 217 | + } |
| 218 | + |
| 219 | + // A rate stored in percentage points (`percentScale: 'whole'`): one bucket at 33.3%. |
| 220 | + const WHOLE_RATE = [ |
| 221 | + { priority: 'High', value: 100 / 3 }, |
| 222 | + { priority: 'Medium', value: 0 }, |
| 223 | + { priority: 'Low', value: 0 }, |
| 224 | + ]; |
| 225 | + // An amount column, in thousands of yuan. |
| 226 | + const AMOUNTS = [ |
| 227 | + { priority: 'High', value: 12000 }, |
| 228 | + { priority: 'Medium', value: 4500 }, |
| 229 | + { priority: 'Low', value: 0 }, |
| 230 | + ]; |
| 231 | + |
| 232 | + it('SUBJECT: a whole-scale percent measure’s ticks read percents, in the table’s own format', () => { |
| 233 | + const measure = { format: '0.0%', percentScale: 'whole' }; |
| 234 | + const ticks = valueTicks(draw(WHOLE_RATE, measure)); |
| 235 | + expect(ticks).toEqual(['0.0%', '9.0%', '18.0%', '27.0%', '36.0%']); |
| 236 | + // The table prints the bucket's value through `formatMeasure` at the same |
| 237 | + // scale and pattern; each tick is that function's reading of its tick. |
| 238 | + expect(formatMeasure(100 / 3, '0.0%', undefined, 'whole', LOCALE)).toBe('33.3%'); |
| 239 | + expect(ticks).toEqual([0, 9, 18, 27, 36].map((t) => formatMeasure(t, '0.0%', undefined, 'whole', LOCALE))); |
| 240 | + }); |
| 241 | + |
| 242 | + it('SUBJECT: a fixed-currency measure’s ticks carry its symbol', () => { |
| 243 | + const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00', currency: 'CNY' })); |
| 244 | + expect(ticks).toEqual(['CN¥0', 'CN¥3K', 'CN¥6K', 'CN¥9K', 'CN¥12K']); |
| 245 | + // The symbol is the one the table prints the amount with. |
| 246 | + expect(formatMeasure(12000, '0,0.00', 'CNY', undefined, LOCALE)).toBe('CN¥12,000.00'); |
| 247 | + }); |
| 248 | + |
| 249 | + it('a second currency carries its own symbol (`USD` → `$12K`)', () => { |
| 250 | + const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00', currency: 'USD' })); |
| 251 | + expect(ticks).toEqual(['$0', '$3K', '$6K', '$9K', '$12K']); |
| 252 | + expect(formatMeasure(12000, '0,0.00', 'USD', undefined, LOCALE)).toBe('$12,000.00'); |
| 253 | + }); |
| 254 | + |
| 255 | + it('a zh-CN session reads the currency in its own compact units (`¥30万`)', () => { |
| 256 | + const large = AMOUNTS.map((r) => ({ ...r, value: r.value * 100 })); |
| 257 | + const ticks = valueTicks(draw(large, { format: '0,0.00', currency: 'CNY' }, {}, 'zh-CN')); |
| 258 | + expect(ticks).toEqual(['¥0', '¥30万', '¥60万', '¥90万', '¥120万']); |
| 259 | + expect(formatMeasure(1200000, '0,0.00', 'CNY', undefined, 'zh-CN')).toBe('¥1,200,000.00'); |
| 260 | + }); |
| 261 | + |
| 262 | + it('CONTROL: a per-row currency measure (no single currency) keeps distinct plain-number ticks', () => { |
| 263 | + // A `currencyField` column's producer hands its pattern and no currency. |
| 264 | + const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00' })); |
| 265 | + expect(ticks).toEqual(['0', '3K', '6K', '9K', '12K']); |
| 266 | + expect(new Set(ticks).size).toBe(ticks.length); |
| 267 | + expect(ticks.some((t) => /[¥$€£%]/.test(t))).toBe(false); |
| 268 | + }); |
| 269 | + |
| 270 | + it('a fraction-scale percent measure reads percents through the carrier alone (the dashboard’s case)', () => { |
| 271 | + const rate = WHOLE_RATE.map((r) => ({ ...r, value: r.value / 100 })); |
| 272 | + expect(valueTicks(draw(rate, { format: '0.0%' }))).toEqual(['0.0%', '8.5%', '17.0%', '25.5%', '34.0%']); |
| 273 | + }); |
| 274 | + |
| 275 | + it('a declared axis `format` wins over the carrier: a `0,0` axis over a `CNY` measure prints no symbol', () => { |
| 276 | + // The two readings differ here, so swapping their order in |
| 277 | + // `valueAxisTickFormatter` turns this red. |
| 278 | + const ticks = valueTicks(draw(AMOUNTS, { format: '0,0.00', currency: 'CNY' }, { yAxis: [{ format: '0,0' }] })); |
| 279 | + expect(ticks).toEqual(['0', '3,000', '6,000', '9,000', '12,000']); |
| 280 | + expect(ticks.some((t) => t.includes('CN¥'))).toBe(false); |
| 281 | + }); |
| 282 | + |
| 283 | + it('the report’s fraction-scale `yAxis[0].format` path still reads percents beside its carrier (objectstack#36988)', () => { |
| 284 | + const rate = [ |
| 285 | + { priority: 'High', value: 1 / 3 }, |
| 286 | + { priority: 'Medium', value: 0 }, |
| 287 | + { priority: 'Low', value: 0 }, |
| 288 | + ]; |
| 289 | + const ticks = valueTicks(draw(rate, { format: '0.0%' }, { yAxis: [{ format: '0.0%' }] })); |
| 290 | + expect(ticks).toEqual(['0.0%', '8.5%', '17.0%', '25.5%', '34.0%']); |
| 291 | + }); |
| 292 | + |
| 293 | + it('a pattern too coarse for the step gains the places the step needs, so no two ticks share a label', () => { |
| 294 | + const rate = [ |
| 295 | + { priority: 'High', value: 3.4 }, |
| 296 | + { priority: 'Medium', value: 0 }, |
| 297 | + { priority: 'Low', value: 0 }, |
| 298 | + ]; |
| 299 | + const ticks = valueTicks(draw(rate, { format: '0%', percentScale: 'whole' })); |
| 300 | + // `formatMeasure` prints a pattern's places as a fixed width, so every tick |
| 301 | + // carries the two the 0.85-point step needs. |
| 302 | + expect(ticks).toEqual(['0.00%', '0.85%', '1.70%', '2.55%', '3.40%']); |
| 303 | + expect(new Set(ticks).size).toBe(ticks.length); |
| 304 | + }); |
| 305 | + |
| 306 | + it('data labels print with the axis’ measure format', () => { |
| 307 | + const labels = dataLabels(draw(WHOLE_RATE, { format: '0.0%', percentScale: 'whole' }, { showDataLabels: true })); |
| 308 | + expect(labels).toContain('33.3%'); |
| 309 | + }); |
| 310 | + |
| 311 | + it('two series formatted differently on one axis keep the default axis', () => { |
| 312 | + const spec = normalizeChartSchema({ |
| 313 | + type: 'chart', |
| 314 | + chartType: 'column', |
| 315 | + xAxisKey: 'priority', |
| 316 | + series: [ |
| 317 | + { dataKey: 'value', measureFormat: { format: '0.0%', percentScale: 'whole' } }, |
| 318 | + { dataKey: 'other', measureFormat: { format: '0,0.00', currency: 'CNY' } }, |
| 319 | + ], |
| 320 | + }); |
| 321 | + const { container } = render( |
| 322 | + <AdvancedChartImpl |
| 323 | + chartType="column" |
| 324 | + data={WHOLE_RATE.map((r) => ({ ...r, other: r.value }))} |
| 325 | + xAxisKey="priority" |
| 326 | + series={spec.series} |
| 327 | + isAnimationActive={false} |
| 328 | + />, |
| 329 | + ); |
| 330 | + const ticks = valueTicks(container); |
| 331 | + expect(ticks.length).toBeGreaterThanOrEqual(3); |
| 332 | + expect(ticks.some((t) => /[¥%]/.test(t))).toBe(false); |
| 333 | + }); |
| 334 | +}); |
0 commit comments