Skip to content

Commit

Permalink
Refactor: src/utils/timezoneUtils/dateTimeMiddleware.test.ts from Jes…
Browse files Browse the repository at this point in the history
…t to Vitest (#2775)

* file name changed

* migrated to vitest from jest

* handled the split function correctly

* added comments that were missing
  • Loading branch information
Ramneet04 authored Dec 25, 2024
1 parent ad6ca7c commit 154588e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Operation, FetchResult } from '@apollo/client/core';
import { Observable } from '@apollo/client/core';
import { gql } from '@apollo/client';
import type { DocumentNode } from 'graphql';
import { describe, it, expect, vi } from 'vitest';

const DUMMY_QUERY: DocumentNode = gql`
query GetDummyData {
Expand All @@ -19,12 +20,12 @@ describe('Date Time Middleware Tests', () => {
query: DUMMY_QUERY,
operationName: 'GetDummyData',
variables: { startDate: '2023-09-01', startTime: '12:00:00' },
getContext: jest.fn(() => ({})),
setContext: jest.fn(),
getContext: vi.fn(() => ({})),
setContext: vi.fn(),
extensions: {},
};

const forward = jest.fn(
const forward = vi.fn(
(op) =>
new Observable<FetchResult>((observer) => {
expect(op.variables['startDate']).toBe('2023-09-01');
Expand Down Expand Up @@ -55,12 +56,12 @@ describe('Date Time Middleware Tests', () => {
query: DUMMY_QUERY,
operationName: 'GetDummyData',
variables: {},
getContext: jest.fn(() => ({})),
setContext: jest.fn(),
getContext: vi.fn(() => ({})),
setContext: vi.fn(),
extensions: {},
};

const forward = jest.fn(
const forward = vi.fn(
() =>
new Observable<FetchResult>((observer) => {
observer.next(testResponse);
Expand Down Expand Up @@ -98,12 +99,12 @@ describe('Date Time Middleware Tests', () => {
query: DUMMY_QUERY,
operationName: 'GetDummyData',
variables: { startDate: 'not-a-date', startTime: '25:99:99' },
getContext: jest.fn(() => ({})),
setContext: jest.fn(),
getContext: vi.fn(() => ({})),
setContext: vi.fn(),
extensions: {},
};

const forward = jest.fn(
const forward = vi.fn(
(op) =>
new Observable<FetchResult>((observer) => {
expect(op.variables['startDate']).toBe('not-a-date');
Expand All @@ -130,12 +131,12 @@ describe('Date Time Middleware Tests', () => {
query: DUMMY_QUERY,
operationName: 'GetDummyData',
variables: {},
getContext: jest.fn(() => ({})),
setContext: jest.fn(),
getContext: vi.fn(() => ({})),
setContext: vi.fn(),
extensions: {},
};

const forward = jest.fn(
const forward = vi.fn(
() =>
new Observable<FetchResult>((observer) => {
observer.next(testResponse);
Expand Down Expand Up @@ -188,12 +189,12 @@ describe('Date Time Middleware Tests', () => {
},
},
},
getContext: jest.fn(() => ({})),
setContext: jest.fn(),
getContext: vi.fn(() => ({})),
setContext: vi.fn(),
extensions: {},
};

const forward = jest.fn(
const forward = vi.fn(
(op) =>
new Observable<FetchResult>((observer) => {
expect(op.variables.event.startDate).toBe('2023-10-01');
Expand Down
20 changes: 13 additions & 7 deletions src/utils/timezoneUtils/dateTimeMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ const combineDateTime = (date: string, time: string): string => {

const splitDateTime = (dateTimeStr: string): { date: string; time: string } => {
const dateTime = dayjs.utc(dateTimeStr);
if (!dateTime.isValid()) {
const [date, time] = dateTimeStr.split('T');
return {
date: date,
time: time,
};
}
return {
date: dateTime.format('YYYY-MM-DD'),
time: dateTime.format('HH:mm:ss.SSS[Z]'),
};
};

const convertUTCToLocal = (dateStr: string): string => {
if (dayjs(dateStr).isValid()) {
return dayjs.utc(dateStr).local().format('YYYY-MM-DDTHH:mm:ss.SSS');
if (!dayjs(dateStr).isValid()) {
return dateStr;
}
return dateStr;
return dayjs.utc(dateStr).local().format('YYYY-MM-DDTHH:mm:ss.SSS');
};

const convertLocalToUTC = (dateStr: string): string => {
if (dayjs(dateStr).isValid()) {
const result = dayjs(dateStr).utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]');
return result;
if (!dayjs(dateStr).isValid()) {
return dateStr; // Leave the invalid value unchanged
}
return dateStr;
return dayjs(dateStr).utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]');
};

const traverseAndConvertDates = (
Expand Down

0 comments on commit 154588e

Please sign in to comment.