Skip to content

Commit 1500406

Browse files
committed
fix tests for parseLocalDate()
1 parent 0440afc commit 1500406

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

packages/cubejs-backend-shared/test/time.test.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
timeSeries,
55
isPredefinedGranularity,
66
timeSeriesFromCustomInterval,
7-
extractDate,
7+
parseLocalDate,
88
utcToLocalTimeZone,
99
addSecondsToLocalTimestamp,
1010
reformatInIsoLocal,
@@ -196,33 +196,41 @@ describe('predefinedGranularity', () => {
196196
});
197197

198198
describe('extractDate', () => {
199+
const timezone = 'Europe/Kiev';
200+
199201
it('should return null if data is empty', () => {
200-
expect(extractDate(null)).toBeNull();
201-
expect(extractDate(undefined)).toBeNull();
202-
expect(extractDate([])).toBeNull();
203-
expect(extractDate('')).toBeNull();
202+
expect(parseLocalDate(null, timezone)).toBeNull();
203+
expect(parseLocalDate(undefined, timezone)).toBeNull();
204+
expect(parseLocalDate([], timezone)).toBeNull();
205+
expect(parseLocalDate('', timezone)).toBeNull();
204206
});
205207

206208
it('should return null if no valid date is found in data', () => {
207-
expect(extractDate([{}])).toBeNull();
208-
expect(extractDate([{ someKey: 'invalid date' }])).toBeNull();
209+
expect(parseLocalDate([{}], timezone)).toBeNull();
210+
expect(parseLocalDate([{ someKey: 'invalid date' }], timezone)).toBeNull();
211+
});
212+
213+
it('should throw an error for unknown timezone', () => {
214+
const input = [{ date: '2025-02-28T12:00:00+03:00' }];
215+
expect(() => parseLocalDate(input, 'Invalid/Timezone'))
216+
.toThrowError('Unknown timezone: Invalid/Timezone');
209217
});
210218

211219
it('should parse a date with UTC timezone', () => {
212220
const input = [{ date: '2025-02-28T12:00:00Z' }];
213-
const result = extractDate(input);
214-
expect(result).toBe('2025-02-28T12:00:00.000');
221+
const result = parseLocalDate(input, timezone);
222+
expect(result).toBe('2025-02-28T14:00:00.000');
215223
});
216224

217225
it('should parse a date with an offset timezone', () => {
218226
const input = [{ date: '2025-02-28T12:00:00+03:00' }];
219-
const result = extractDate(input);
220-
expect(result).toBe('2025-02-28T09:00:00.000');
227+
const result = parseLocalDate(input, timezone);
228+
expect(result).toBe('2025-02-28T11:00:00.000');
221229
});
222230

223231
it('should parse a date without timezone as UTC', () => {
224232
const input = [{ date: '2025-02-28 12:00:00' }];
225-
const result = extractDate(input);
233+
const result = parseLocalDate(input, timezone);
226234
expect(result).toBe('2025-02-28T12:00:00.000');
227235
});
228236

@@ -232,10 +240,10 @@ describe('extractDate', () => {
232240
const input3 = [{ date: '2025-02-28T12:00:00Z' }];
233241
const input4 = [{ date: '2025-02-28T12:00:00+03:00' }];
234242

235-
expect(extractDate(input1)).toBe('2025-02-28T12:00:00.000');
236-
expect(extractDate(input2)).toBe('2025-02-28T12:00:00.000');
237-
expect(extractDate(input3)).toBe('2025-02-28T12:00:00.000');
238-
expect(extractDate(input4)).toBe('2025-02-28T09:00:00.000');
243+
expect(parseLocalDate(input1, timezone)).toBe('2025-02-28T12:00:00.000');
244+
expect(parseLocalDate(input2, timezone)).toBe('2025-02-28T12:00:00.000');
245+
expect(parseLocalDate(input3, timezone)).toBe('2025-02-28T14:00:00.000');
246+
expect(parseLocalDate(input4, timezone)).toBe('2025-02-28T11:00:00.000');
239247
});
240248
});
241249

0 commit comments

Comments
 (0)