|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#7178 — a dataset measure over a date field renders as a date. |
| 5 | + * |
| 6 | + * `formatMeasure` opened with `if (typeof v !== 'number') return String(v)`, |
| 7 | + * placed BEFORE `format` was read. So a `min` / `max` over a date or datetime |
| 8 | + * field printed its stored value verbatim — a 24-character ISO string in the |
| 9 | + * KPI tile's `text-2xl font-semibold`, wrapped over two lines — and the |
| 10 | + * `format` that `DatasetMeasureSchema` accepts was unreachable for it. |
| 11 | + * |
| 12 | + * ── What these cases pin, and what they deliberately do NOT ───────────────── |
| 13 | + * The fix is a ROUTE, not a formatter: every byte of date rendering comes from |
| 14 | + * `../date-display.ts`, which is where `@object-ui/fields`' `formatDate` / |
| 15 | + * `formatDateTime` now live and what the `date` cell renderer, `ObjectGrid`'s |
| 16 | + * date cells and `ObjectGantt`'s tooltips call. So the cases below assert |
| 17 | + * AGREEMENT WITH THAT PATH rather than literal strings wherever the path's own |
| 18 | + * output is the contract — a literal would pin this file's opinion of a date, |
| 19 | + * which is exactly the second convention objectui#4576 says not to create. |
| 20 | + * (`DatasetWidget.dateMeasure.test.tsx` closes the loop on the rendered |
| 21 | + * surfaces, and `@object-ui/fields`' `date-display.reexport-identity.test.ts` |
| 22 | + * pins that the cell's `formatDate` and this one are the same function.) |
| 23 | + * |
| 24 | + * ── Directions, predicted in writing BEFORE the run ───────────────────────── |
| 25 | + * the two date cases RED pre-fix — they render the raw ISO string |
| 26 | + * the `format`-as-style cases RED pre-fix — `format` was never read here |
| 27 | + * ⭐ every must-not-move case GREEN on both sides. These are the guard, |
| 28 | + * not the fix: the new branch runs ahead of |
| 29 | + * the `String(v)` short-circuit, so it has to |
| 30 | + * be shown NOT to capture numbers, numeric |
| 31 | + * strings, the nullish em dash, or prose. |
| 32 | + * |
| 33 | + * The full before/after sweep is recorded in the PR: 33,696 argument forms |
| 34 | + * (value x format x currency x percentScale x locale) compared against a |
| 35 | + * verbatim copy of the pre-fix function, 4 distinct values moved — every one |
| 36 | + * of them ISO-shaped and parseable — and 0 expected movers missed. |
| 37 | + */ |
| 38 | + |
| 39 | +import { describe, it, expect } from 'vitest'; |
| 40 | + |
| 41 | +import { formatMeasure } from '../dataset-format.js'; |
| 42 | +import { formatDate, formatDateTime } from '../date-display.js'; |
| 43 | + |
| 44 | +/** A NON-current year, so `formatDate`'s "drop the year" branch is not in play. */ |
| 45 | +const ISO_DATE = '2024-07-04'; |
| 46 | +const ISO_DATETIME = '2024-07-04T07:00:00.000Z'; |
| 47 | +const EN = 'en-US'; |
| 48 | + |
| 49 | +describe('formatMeasure routes a date-shaped measure through the shared date path (objectui#7178)', () => { |
| 50 | + it('renders a datetime measure as a datetime, not as its raw ISO string', () => { |
| 51 | + const out = formatMeasure(ISO_DATETIME, undefined, undefined, undefined, EN); |
| 52 | + expect(out).not.toBe(ISO_DATETIME); |
| 53 | + expect(out).toBe(formatDateTime(ISO_DATETIME, { locale: EN })); |
| 54 | + }); |
| 55 | + |
| 56 | + it('renders a date-only measure as a date, not as its raw ISO string', () => { |
| 57 | + const out = formatMeasure(ISO_DATE, undefined, undefined, undefined, EN); |
| 58 | + expect(out).not.toBe(ISO_DATE); |
| 59 | + expect(out).toBe(formatDate(ISO_DATE, undefined, { locale: EN })); |
| 60 | + }); |
| 61 | + |
| 62 | + it('accepts the space-separated ISO spelling a backend may send', () => { |
| 63 | + const spaced = '2024-07-04 07:00:00'; |
| 64 | + expect(formatMeasure(spaced, undefined, undefined, undefined, EN)).toBe( |
| 65 | + formatDateTime(spaced, { locale: EN }), |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('follows the display locale, like every other measure', () => { |
| 70 | + const de = formatMeasure(ISO_DATETIME, undefined, undefined, undefined, 'de-DE'); |
| 71 | + const en = formatMeasure(ISO_DATETIME, undefined, undefined, undefined, EN); |
| 72 | + expect(de).not.toBe(en); |
| 73 | + expect(de).toBe(formatDateTime(ISO_DATETIME, { locale: 'de-DE' })); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +/** |
| 78 | + * The finding's reusable half: `DatasetMeasureSchema.format` is an open string |
| 79 | + * that parses and, on this path, could never be read. It is read now — as the |
| 80 | + * shared path's STYLE vocabulary, which is what that path actually accepts. |
| 81 | + * A date PATTERN is not part of that vocabulary, and these cases say so out |
| 82 | + * loud rather than leaving it silent, because "silent" is the whole card. |
| 83 | + */ |
| 84 | +describe('what `format` can and cannot say for a date measure (objectui#7178)', () => { |
| 85 | + it('honours `short` — the same word `DateCellRenderer` honours from `field.format`', () => { |
| 86 | + expect(formatMeasure(ISO_DATE, 'short', undefined, undefined, EN)).toBe( |
| 87 | + formatDate(ISO_DATE, 'short', { locale: EN }), |
| 88 | + ); |
| 89 | + expect(formatMeasure(ISO_DATE, 'short', undefined, undefined, EN)).not.toBe( |
| 90 | + formatMeasure(ISO_DATE, undefined, undefined, undefined, EN), |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('honours `relative`', () => { |
| 95 | + expect(formatMeasure(ISO_DATE, 'relative', undefined, undefined, EN)).toBe( |
| 96 | + formatDate(ISO_DATE, 'relative', { locale: EN }), |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('⚠️ does NOT interpret a date PATTERN — `YYYY-MM-DD` renders the locale default', () => { |
| 101 | + // Measured, not assumed, and stated in the PR body: the shared date path |
| 102 | + // takes a named style, not a pattern. This case exists so the limit is |
| 103 | + // pinned rather than rediscovered — and so that teaching the shared path a |
| 104 | + // pattern grammar later has a test that must be updated deliberately. |
| 105 | + const patterned = formatMeasure(ISO_DATE, 'YYYY-MM-DD', undefined, undefined, EN); |
| 106 | + expect(patterned).not.toBe('2024-07-04'); |
| 107 | + expect(patterned).toBe(formatMeasure(ISO_DATE, undefined, undefined, undefined, EN)); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +/** |
| 112 | + * ⭐ The regression guard. The date branch runs ahead of the `String(v)` |
| 113 | + * short-circuit, so every one of these has to be shown NOT to reach it. |
| 114 | + */ |
| 115 | +describe('formatMeasure leaves every non-date value exactly where it was (objectui#7178)', () => { |
| 116 | + it('formats numbers through the numeral path, untouched', () => { |
| 117 | + expect(formatMeasure(1234.5, '0.0', undefined, undefined, EN)).toBe('1,234.5'); |
| 118 | + expect(formatMeasure(1234.5, '0.00', 'EUR', undefined, EN)).toBe('€1,234.50'); |
| 119 | + expect(formatMeasure(0.75, '0.0%', undefined, 'fraction', EN)).toBe('75.0%'); |
| 120 | + expect(formatMeasure(42, undefined, undefined, undefined, EN)).toBe('42'); |
| 121 | + expect(formatMeasure(2026, undefined, undefined, undefined, EN)).toBe('2026'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('⭐ never reads a NUMERIC STRING as a date', () => { |
| 125 | + // `1751612400000` is epoch milliseconds for a real instant, and `2026` is a |
| 126 | + // year. Both are what a count or an id looks like coming back from a |
| 127 | + // measure, and `Date.parse` would take either if it were asked. |
| 128 | + for (const v of ['0', '42', '1234.5', '-1234.5', '1751612400000', '2026', '1e21']) { |
| 129 | + expect(formatMeasure(v, undefined, undefined, undefined, EN)).toBe(v); |
| 130 | + expect(formatMeasure(v, 'YYYY-MM-DD', undefined, undefined, EN)).toBe(v); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it('keeps the nullish em dash', () => { |
| 135 | + expect(formatMeasure(null)).toBe('—'); |
| 136 | + expect(formatMeasure(undefined)).toBe('—'); |
| 137 | + expect(formatMeasure(null, 'YYYY-MM-DD', undefined, undefined, EN)).toBe('—'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('falls through to `String(v)` for an arbitrary non-numeric, non-date value', () => { |
| 141 | + for (const v of ['Acme Corp', 'N/A', '', 'March 5, 2026', '2026-07', '2026/07/04', '04-07-2026']) { |
| 142 | + expect(formatMeasure(v, undefined, undefined, undefined, EN)).toBe(String(v)); |
| 143 | + } |
| 144 | + expect(formatMeasure(true, undefined, undefined, undefined, EN)).toBe('true'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('leaves an ISO-SHAPED but unparseable value alone rather than showing an em dash', () => { |
| 148 | + // `formatDate` answers `—` for an unparseable value. Routing one there |
| 149 | + // would REPLACE a raw string the author can still debug with a dash that |
| 150 | + // says nothing, so the parse guard keeps these on the old path. |
| 151 | + for (const v of ['2026-13-45', '2024-07-04T99:99']) { |
| 152 | + expect(formatMeasure(v, undefined, undefined, undefined, EN)).toBe(v); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + it('agrees with the list cell on a rolled-over date instead of second-guessing it', () => { |
| 157 | + // `Date.parse('2024-02-30')` is NOT NaN — V8 rolls it to March 1/2. Every |
| 158 | + // date surface in the repo builds its `Date` the same way, so this renders |
| 159 | + // as the same day a list cell shows for the same stored string. Pinned |
| 160 | + // because it is the one place agreement looks like a bug. |
| 161 | + const rolled = '2024-02-30'; |
| 162 | + expect(Number.isNaN(Date.parse(rolled))).toBe(false); |
| 163 | + expect(formatMeasure(rolled, undefined, undefined, undefined, EN)).toBe( |
| 164 | + formatDate(rolled, undefined, { locale: EN }), |
| 165 | + ); |
| 166 | + }); |
| 167 | +}); |
0 commit comments