|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { dataTableDateFormatter, dataTableTimeFormatter } from '../data-table-formatters'; |
| 3 | + |
| 4 | +describe('dataTableDateFormatter', () => { |
| 5 | + test('returns null for empty values', () => { |
| 6 | + expect(dataTableDateFormatter(null)).toBeNull(); |
| 7 | + expect(dataTableDateFormatter(undefined)).toBeNull(); |
| 8 | + expect(dataTableDateFormatter('')).toBeNull(); |
| 9 | + }); |
| 10 | + |
| 11 | + test('formats a valid ISO date (length 10)', () => { |
| 12 | + expect(dataTableDateFormatter('2023-11-12')).toBe('2023-11-12'); |
| 13 | + }); |
| 14 | + |
| 15 | + test('formats a valid ISO datetime (length 28)', () => { |
| 16 | + const value = '2023-11-12T15:30:45.000+0000'; |
| 17 | + expect(value.length).toBe(28); |
| 18 | + // Exact time depends on the runner's timezone, so assert the formatted shape rather than the wall-clock time. |
| 19 | + expect(dataTableDateFormatter(value)).toMatch(/^\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2} (AM|PM)$/); |
| 20 | + }); |
| 21 | + |
| 22 | + test('formats a valid Date instance', () => { |
| 23 | + const result = dataTableDateFormatter(new Date(2023, 10, 12, 15, 30, 45)); |
| 24 | + expect(result).toBe('2023-11-12 3:30:45 PM'); |
| 25 | + }); |
| 26 | + |
| 27 | + // Regression: an invalid Date instance previously caused formatDate to throw "RangeError: Invalid time value". |
| 28 | + // It should fall back to the raw value (coerced to a string) rather than throwing or hiding data. |
| 29 | + test('returns the raw value for an invalid Date instance instead of throwing', () => { |
| 30 | + const value = new Date('invalid'); |
| 31 | + expect(() => dataTableDateFormatter(value)).not.toThrow(); |
| 32 | + expect(dataTableDateFormatter(value)).toBe('Invalid Date'); |
| 33 | + }); |
| 34 | + |
| 35 | + // Regression: a 10-char MM/DD/YYYY value (e.g. from a formula field) is not ISO and previously |
| 36 | + // caused parseISO -> Invalid Date -> formatDate to throw "RangeError: Invalid time value". |
| 37 | + test('returns the raw value for a 10-char non-ISO date instead of throwing', () => { |
| 38 | + expect(() => dataTableDateFormatter('12/11/2023')).not.toThrow(); |
| 39 | + expect(dataTableDateFormatter('12/11/2023')).toBe('12/11/2023'); |
| 40 | + expect(dataTableDateFormatter('10/15/2019')).toBe('10/15/2019'); |
| 41 | + }); |
| 42 | + |
| 43 | + test('returns the raw value for an unparseable 28-char value instead of throwing', () => { |
| 44 | + const value = 'x'.repeat(28); |
| 45 | + expect(() => dataTableDateFormatter(value)).not.toThrow(); |
| 46 | + expect(dataTableDateFormatter(value)).toBe(value); |
| 47 | + }); |
| 48 | + |
| 49 | + test('returns other-length strings unchanged', () => { |
| 50 | + expect(dataTableDateFormatter('some text')).toBe('some text'); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('dataTableTimeFormatter', () => { |
| 55 | + test('returns null for empty values', () => { |
| 56 | + expect(dataTableTimeFormatter(null)).toBeNull(); |
| 57 | + expect(dataTableTimeFormatter(undefined)).toBeNull(); |
| 58 | + expect(dataTableTimeFormatter('')).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + test('formats a valid 13-char Salesforce time value', () => { |
| 62 | + expect(dataTableTimeFormatter('15:30:45.000Z')).toBe('3:30:45 PM'); |
| 63 | + }); |
| 64 | + |
| 65 | + // Regression: a 13-char value that does not match the expected time format should not throw. |
| 66 | + test('returns the raw value for an unparseable 13-char time instead of throwing', () => { |
| 67 | + const value = 'not-valid-tim'; |
| 68 | + expect(value.length).toBe(13); |
| 69 | + expect(() => dataTableTimeFormatter(value)).not.toThrow(); |
| 70 | + expect(dataTableTimeFormatter(value)).toBe(value); |
| 71 | + }); |
| 72 | + |
| 73 | + test('returns other-length strings unchanged', () => { |
| 74 | + expect(dataTableTimeFormatter('15:30')).toBe('15:30'); |
| 75 | + }); |
| 76 | +}); |
0 commit comments