Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: src/utils/timezoneUtils/dateTimeMiddleware.test.ts from Jest to Vitest #2775

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading