|
6 | 6 | formatXAxisDate, |
7 | 7 | normalizeChartDataWithUnits, |
8 | 8 | getTooltipDateFormat, |
| 9 | + formatTooltipValueWithUnit, |
9 | 10 | } from './chartUtils'; |
10 | 11 | import { NAN_STRING } from '../../constants'; |
11 | 12 | import { UnitRange } from '../types'; |
@@ -414,4 +415,79 @@ describe('normalizeChartDataWithUnits', () => { |
414 | 415 | expect(result.rechartsData).toEqual(data); |
415 | 416 | }); |
416 | 417 | }); |
| 418 | + |
| 419 | + describe('valueBase', () => { |
| 420 | + it('should expose the value base used for normalization', () => { |
| 421 | + const data = [{ category: 'A', value: 2000 }]; |
| 422 | + const unitRange: UnitRange = [ |
| 423 | + { threshold: 1, label: 'op/s' }, |
| 424 | + { threshold: 1000, label: 'kop/s' }, |
| 425 | + ]; |
| 426 | + |
| 427 | + const result = normalizeChartDataWithUnits( |
| 428 | + data, |
| 429 | + 2000, |
| 430 | + unitRange, |
| 431 | + 'category', |
| 432 | + ); |
| 433 | + |
| 434 | + expect(result.unitLabel).toBe('kop/s'); |
| 435 | + expect(result.valueBase).toBe(1000); |
| 436 | + }); |
| 437 | + |
| 438 | + it('should default valueBase to 1 when no unit range is provided', () => { |
| 439 | + const result = normalizeChartDataWithUnits( |
| 440 | + [{ category: 'A', value: 100 }], |
| 441 | + 100, |
| 442 | + undefined, |
| 443 | + 'category', |
| 444 | + ); |
| 445 | + |
| 446 | + expect(result.valueBase).toBe(1); |
| 447 | + }); |
| 448 | + }); |
| 449 | +}); |
| 450 | + |
| 451 | +describe('formatTooltipValueWithUnit', () => { |
| 452 | + const unitRange: UnitRange = [ |
| 453 | + { threshold: 1, label: 'op/s' }, |
| 454 | + { threshold: 1000, label: 'kop/s' }, |
| 455 | + { threshold: 1000000, label: 'Mop/s' }, |
| 456 | + ]; |
| 457 | + |
| 458 | + it('re-derives a smaller unit for values that are small relative to the axis unit', () => { |
| 459 | + // Axis is in kop/s (valueBase 1000). A point of 5 op/s is stored as 0.005. |
| 460 | + // Without re-scaling it would read "0.01 kop/s"; instead it should read "5 op/s". |
| 461 | + expect(formatTooltipValueWithUnit(0.005, 1000, unitRange, 'kop/s')).toBe( |
| 462 | + '5.00 op/s', |
| 463 | + ); |
| 464 | + }); |
| 465 | + |
| 466 | + it('keeps the axis unit for values that match its magnitude', () => { |
| 467 | + // 2 (stored) * 1000 = 2000 op/s → 2 kop/s |
| 468 | + expect(formatTooltipValueWithUnit(2, 1000, unitRange, 'kop/s')).toBe( |
| 469 | + '2.00 kop/s', |
| 470 | + ); |
| 471 | + }); |
| 472 | + |
| 473 | + it('re-derives a larger unit for values that exceed the axis unit', () => { |
| 474 | + // 5000 (stored) * 1000 = 5,000,000 op/s → 5 Mop/s |
| 475 | + expect(formatTooltipValueWithUnit(5000, 1000, unitRange, 'kop/s')).toBe( |
| 476 | + '5.00 Mop/s', |
| 477 | + ); |
| 478 | + }); |
| 479 | + |
| 480 | + it('handles negative values (symmetrical charts) using the magnitude', () => { |
| 481 | + expect(formatTooltipValueWithUnit(-0.005, 1000, unitRange, 'kop/s')).toBe( |
| 482 | + '-5.00 op/s', |
| 483 | + ); |
| 484 | + }); |
| 485 | + |
| 486 | + it('falls back to the provided unit label when no unit range is given', () => { |
| 487 | + expect(formatTooltipValueWithUnit(42.5, 1, undefined, '%')).toBe('42.50 %'); |
| 488 | + }); |
| 489 | + |
| 490 | + it('returns "-" for non-finite values', () => { |
| 491 | + expect(formatTooltipValueWithUnit(NaN, 1000, unitRange, 'kop/s')).toBe('-'); |
| 492 | + }); |
417 | 493 | }); |
0 commit comments