|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { Temporal } from '@js-temporal/polyfill' |
| 3 | +import { withDateOperation } from './withDateOperation' |
| 4 | + |
| 5 | +describe('withDateOperation', () => { |
| 6 | + const mockOperation = withDateOperation((zdt) => zdt) |
| 7 | + |
| 8 | + describe('timezone handling', () => { |
| 9 | + test('should use default timezone when not provided', () => { |
| 10 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }) |
| 11 | + expect(result.options.timeZone).toBeDefined() |
| 12 | + expect(typeof result.options.timeZone).toBe('string') |
| 13 | + }) |
| 14 | + |
| 15 | + test('should use custom timezone', () => { |
| 16 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 17 | + timeZone: 'Asia/Tokyo', |
| 18 | + }) |
| 19 | + expect(result.options.timeZone).toBe('Asia/Tokyo') |
| 20 | + }) |
| 21 | + |
| 22 | + test('should return timezone in options', () => { |
| 23 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 24 | + timeZone: 'Europe/London', |
| 25 | + }) |
| 26 | + expect(result.options.timeZone).toBe('Europe/London') |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + describe('calendar handling', () => { |
| 31 | + test('should use default calendar when not provided', () => { |
| 32 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }) |
| 33 | + expect(result.options.calendar).toBeDefined() |
| 34 | + expect(typeof result.options.calendar).toBe('string') |
| 35 | + }) |
| 36 | + |
| 37 | + test('should use custom calendar', () => { |
| 38 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 39 | + calendar: 'japanese', |
| 40 | + }) |
| 41 | + expect(result.options.calendar).toBe('japanese') |
| 42 | + }) |
| 43 | + |
| 44 | + test('should return calendar in options', () => { |
| 45 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 46 | + calendar: 'islamic', |
| 47 | + }) |
| 48 | + expect(result.options.calendar).toBe('islamic') |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + describe('conversion methods', () => { |
| 53 | + test('should have asDate method', () => { |
| 54 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 55 | + timeZone: 'UTC', |
| 56 | + }) |
| 57 | + const date = result.asDate() |
| 58 | + expect(date).toBeInstanceOf(Date) |
| 59 | + expect(date.toISOString()).toContain('2024-03-15') |
| 60 | + }) |
| 61 | + |
| 62 | + test('should have asEpoch method', () => { |
| 63 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 64 | + timeZone: 'UTC', |
| 65 | + }) |
| 66 | + const epoch = result.asEpoch() |
| 67 | + expect(typeof epoch).toBe('number') |
| 68 | + expect(new Date(epoch).toISOString()).toContain('2024-03-15') |
| 69 | + }) |
| 70 | + |
| 71 | + test('should have asString method', () => { |
| 72 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 73 | + timeZone: 'UTC', |
| 74 | + }) |
| 75 | + const str = result.asString() |
| 76 | + expect(typeof str).toBe('string') |
| 77 | + expect(str).toContain('2024-03-15') |
| 78 | + }) |
| 79 | + |
| 80 | + test('should have asLong method', () => { |
| 81 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 82 | + timeZone: 'America/New_York', |
| 83 | + calendar: 'gregory', |
| 84 | + }) |
| 85 | + const long = result.asLong() |
| 86 | + expect(typeof long).toBe('string') |
| 87 | + expect(long).toContain('2024-03-15') |
| 88 | + expect(long).toContain('[America/New_York]') |
| 89 | + expect(long).toContain('[u-ca=gregory]') |
| 90 | + }) |
| 91 | + |
| 92 | + test('should have asZonedDateTime method', () => { |
| 93 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 94 | + timeZone: 'UTC', |
| 95 | + }) |
| 96 | + const zdt = result.asZonedDateTime() |
| 97 | + expect(zdt).toBeInstanceOf(Temporal.ZonedDateTime) |
| 98 | + expect(zdt.toInstant().toString()).toContain('2024-03-15') |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('result properties', () => { |
| 103 | + test('should have value property as string', () => { |
| 104 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 105 | + timeZone: 'UTC', |
| 106 | + }) |
| 107 | + expect(typeof result.value).toBe('string') |
| 108 | + expect(result.value).toContain('2024-03-15') |
| 109 | + }) |
| 110 | + |
| 111 | + test('should have options property', () => { |
| 112 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }) |
| 113 | + expect(result.options).toHaveProperty('timeZone') |
| 114 | + expect(result.options).toHaveProperty('calendar') |
| 115 | + expect(typeof result.options.timeZone).toBe('string') |
| 116 | + expect(typeof result.options.calendar).toBe('string') |
| 117 | + }) |
| 118 | + |
| 119 | + test('should have timeZone property', () => { |
| 120 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 121 | + timeZone: 'Asia/Tokyo', |
| 122 | + }) |
| 123 | + expect(result.timeZone).toBe('Asia/Tokyo') |
| 124 | + }) |
| 125 | + |
| 126 | + test('should have calendar property', () => { |
| 127 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 128 | + calendar: 'japanese', |
| 129 | + }) |
| 130 | + expect(result.calendar).toBe('japanese') |
| 131 | + }) |
| 132 | + |
| 133 | + test('should support destructuring', () => { |
| 134 | + const result = mockOperation('2024-03-15T14:42:12.789Z', { unit: 'test' }, { |
| 135 | + timeZone: 'UTC', |
| 136 | + calendar: 'gregory', |
| 137 | + }) |
| 138 | + const { value, timeZone, calendar } = result |
| 139 | + expect(typeof value).toBe('string') |
| 140 | + expect(timeZone).toBe('UTC') |
| 141 | + expect(calendar).toBe('gregory') |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + describe('operation execution', () => { |
| 146 | + test('should execute the provided operation function', () => { |
| 147 | + const addDayOperation = withDateOperation<{ days: number }>((zdt, { days }) => { |
| 148 | + return zdt.add({ days }) |
| 149 | + }) |
| 150 | + const result = addDayOperation('2024-03-15T00:00:00Z', { days: 1 }, { |
| 151 | + timeZone: 'UTC', |
| 152 | + }) |
| 153 | + expect(result.value).toBe('2024-03-16T00:00:00Z') |
| 154 | + }) |
| 155 | + |
| 156 | + test('should pass args to the operation function', () => { |
| 157 | + const customOperation = withDateOperation<{ multiplier: number }>((zdt, { multiplier }) => { |
| 158 | + const days = zdt.day * multiplier |
| 159 | + return zdt.with({ day: days }) |
| 160 | + }) |
| 161 | + const result = customOperation('2024-03-15T00:00:00Z', { multiplier: 2 }, { |
| 162 | + timeZone: 'UTC', |
| 163 | + }) |
| 164 | + expect(result.value).toContain('2024-03-30') |
| 165 | + }) |
| 166 | + }) |
| 167 | +}) |
0 commit comments