|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * auditHistoryDisplay — the History tab's Changes entry formats a currency, |
| 6 | + * number or percent value the way the record page formats that field. |
| 7 | + * |
| 8 | + * The record page's cells (`CurrencyCellRenderer`, `NumberCellRenderer`, |
| 9 | + * `PercentCellRenderer`) format through `formatCurrency`, |
| 10 | + * `formatNumberFieldValue` and `formatPercent` in `@object-ui/fields`; the |
| 11 | + * History tab printed the raw stored number instead (`4321000` where the |
| 12 | + * record header read `$4,321,000.00`). Each case below states the record |
| 13 | + * page's text as a literal and checks the shared formatter agrees with it, so |
| 14 | + * the literal is the cell's answer and not this helper's own. |
| 15 | + */ |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { |
| 18 | + formatCurrency, |
| 19 | + formatNumberFieldValue, |
| 20 | + formatPercent, |
| 21 | +} from '@object-ui/fields'; |
| 22 | +import { formatAuditValue, type AuditFieldDef } from '../auditHistoryDisplay'; |
| 23 | + |
| 24 | +const annualRevenue: AuditFieldDef = { |
| 25 | + type: 'currency', |
| 26 | + label: 'Annual Revenue', |
| 27 | + currencyConfig: { currencyMode: 'fixed', defaultCurrency: 'USD' }, |
| 28 | +}; |
| 29 | + |
| 30 | +describe('formatAuditValue — currency', () => { |
| 31 | + it('formats a fixed-currency amount as the record page does', () => { |
| 32 | + const out = formatAuditValue(annualRevenue, 4321000, { locale: 'en-US' }); |
| 33 | + expect(out).toBe('$4,321,000.00'); |
| 34 | + expect(out).toBe(formatCurrency(4321000, 'USD', 'en-US')); |
| 35 | + // Both sides of the change, the old value included. |
| 36 | + expect(formatAuditValue(annualRevenue, 8000000, { locale: 'en-US' })).toBe('$8,000,000.00'); |
| 37 | + }); |
| 38 | + |
| 39 | + it("reads the currency's own minor-unit width and the viewer's locale", () => { |
| 40 | + const yen: AuditFieldDef = { type: 'currency', currencyConfig: { currencyMode: 'fixed', defaultCurrency: 'JPY' } }; |
| 41 | + expect(formatAuditValue(yen, 4321000, { locale: 'en-US' })).toBe(formatCurrency(4321000, 'JPY', 'en-US')); |
| 42 | + expect(formatAuditValue(yen, 4321000, { locale: 'en-US' })).not.toContain('.'); |
| 43 | + const de = formatAuditValue(annualRevenue, 1234.5, { locale: 'de-DE' }); |
| 44 | + expect(de).toBe(formatCurrency(1234.5, 'USD', 'de-DE')); |
| 45 | + expect(de).toContain('1.234,50'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('reads a decimal column stored as its numeric string', () => { |
| 49 | + expect(formatAuditValue(annualRevenue, '4321000.5', { locale: 'en-US' })).toBe('$4,321,000.50'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('shows a dynamic-mode amount in the tenant default, and a plain number without one', () => { |
| 53 | + const dynamic: AuditFieldDef = { type: 'currency', currencyConfig: { currencyMode: 'dynamic' } }; |
| 54 | + expect(formatAuditValue(dynamic, 1500, { locale: 'en-US', tenantCurrency: 'EUR' })).toBe( |
| 55 | + formatCurrency(1500, 'EUR', 'en-US'), |
| 56 | + ); |
| 57 | + // No code known: the formatter's own no-code face, never a guessed symbol. |
| 58 | + expect(formatAuditValue(dynamic, 1500, { locale: 'en-US' })).toBe('1,500.00'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('shows a record-mode amount as a plain number: its code lives on the row, not in this call', () => { |
| 62 | + const recordMode: AuditFieldDef = { |
| 63 | + type: 'currency', |
| 64 | + currencyConfig: { currencyMode: 'record', currencyField: 'currency_code' }, |
| 65 | + }; |
| 66 | + // The tenant default is NOT a fallback for a record-mode amount. |
| 67 | + expect(formatAuditValue(recordMode, 1500, { locale: 'en-US', tenantCurrency: 'EUR' })).toBe( |
| 68 | + formatCurrency(1500, undefined, 'en-US'), |
| 69 | + ); |
| 70 | + expect(formatAuditValue(recordMode, 1500, { locale: 'en-US', tenantCurrency: 'EUR' })).toBe('1,500.00'); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('formatAuditValue — number', () => { |
| 75 | + it('pads to the declared scale and groups', () => { |
| 76 | + const amount: AuditFieldDef = { type: 'number', scale: 2 }; |
| 77 | + const out = formatAuditValue(amount, 1234.5, { locale: 'en-US' }); |
| 78 | + expect(out).toBe('1,234.50'); |
| 79 | + expect(out).toBe(formatNumberFieldValue(1234.5, { name: 'amount', type: 'number', scale: 2 }, 'en-US')); |
| 80 | + }); |
| 81 | + |
| 82 | + it('keeps a scale-0 number ungrouped, as the cell does for a year', () => { |
| 83 | + const year: AuditFieldDef = { type: 'number', scale: 0 }; |
| 84 | + expect(formatAuditValue(year, 2026, { locale: 'en-US' })).toBe('2026'); |
| 85 | + }); |
| 86 | + |
| 87 | + it("follows the viewer's locale", () => { |
| 88 | + const amount: AuditFieldDef = { type: 'number', scale: 2 }; |
| 89 | + expect(formatAuditValue(amount, 1234.5, { locale: 'de-DE' })).toBe('1.234,50'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('keeps a value that is not a number as it was stored', () => { |
| 93 | + expect(formatAuditValue({ type: 'number' }, 'n/a', { locale: 'en-US' })).toBe('n/a'); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('formatAuditValue — percent', () => { |
| 98 | + it('reads a fraction-stored percent at its declared scale', () => { |
| 99 | + const rate: AuditFieldDef = { type: 'percent', scale: 1 }; |
| 100 | + const out = formatAuditValue(rate, 0.25, { locale: 'en-US' }); |
| 101 | + expect(out).toBe('25.0%'); |
| 102 | + expect(out).toBe(formatPercent(0.25, 'fraction', 1, 'en-US')); |
| 103 | + }); |
| 104 | + |
| 105 | + it('reads a whole-stored percent when the field declares max above 1', () => { |
| 106 | + const points: AuditFieldDef = { type: 'percent', scale: 0, max: 100 }; |
| 107 | + expect(formatAuditValue(points, 25, { locale: 'en-US' })).toBe('25%'); |
| 108 | + }); |
| 109 | + |
| 110 | + it("follows the viewer's locale percent convention", () => { |
| 111 | + const rate: AuditFieldDef = { type: 'percent', scale: 1 }; |
| 112 | + expect(formatAuditValue(rate, 0.25, { locale: 'de-DE' })).toBe(formatPercent(0.25, 'fraction', 1, 'de-DE')); |
| 113 | + }); |
| 114 | +}); |
0 commit comments