Skip to content

Commit

Permalink
Fix the timezone issue in test again
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 committed Jul 21, 2024
1 parent 7ead7d3 commit ebd3a6a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 44 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import React from 'react';
import { shallow } from 'enzyme';
import {} from '@elastic/datemath';
import { AccelerationDetailsTab } from './acceleration_details_tab';
import { ApplicationStart } from 'opensearch-dashboards/public';

Expand Down Expand Up @@ -74,24 +73,11 @@ describe('AccelerationDetailsTab', () => {

beforeAll(() => {
// Mock the Date.now() method to always return a specific timestamp
jest.spyOn(Date, 'now').mockImplementation(() => 1627819985000); // 2021-08-01T11:53:05.000Z

// Mock the Intl.DateTimeFormat to use a specific timezone (e.g., UTC)
jest.spyOn(Intl, 'DateTimeFormat').mockImplementation(() => {
return {
format: (date) =>
new Date(date).toLocaleString('en-US', {
timeZone: 'UTC',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true,
}),
} as any;
});
jest.spyOn(Date, 'now').mockImplementation(() => 1627843985000); // 2021-08-01T18:53:05.000Z

// Mock toLocaleString to return a fixed string
// eslint-disable-next-line no-extend-native
Date.prototype.toLocaleString = jest.fn(() => '8/1/2021, 2:53:05 PM');
});

afterAll(() => {
Expand All @@ -113,11 +99,7 @@ describe('AccelerationDetailsTab', () => {
const wrapper = shallowComponent();
const creationDateNode = wrapper.find('DetailComponent[title="Creation Date"]');
const displayedDate = creationDateNode.prop('description') as string;

// Convert the displayed local time back to UTC
const displayedUTC = new Date(displayedDate).toUTCString();

expect(displayedUTC).toBe('Sun, 01 Aug 2021 18:53:05 GMT');
expect(displayedDate).toBe('8/1/2021, 2:53:05 PM');
});

test('displays the correct refresh type', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { ApplicationStart, HttpStart, NotificationsStart } from 'opensearch-dash
import { CatalogCacheManager } from '../../../../framework/catalog_cache/cache_manager';
import { DirectQueryLoadingStatus } from '../../../../framework/types';

// Mock the Date object to return a fixed date in UTC
const fixedDate = new Date(Date.UTC(2024, 6, 21, 12, 0, 0));
// Mock the Date object to return a fixed date
const fixedDate = new Date('2024-07-21T12:00:00Z');
const OriginalDate = Date;

global.Date = jest.fn((...args) => {
Expand All @@ -24,6 +24,10 @@ global.Date = jest.fn((...args) => {

global.Date.now = OriginalDate.now;

// Mock toLocaleString to return a fixed string
// eslint-disable-next-line no-extend-native
Date.prototype.toLocaleString = jest.fn(() => '7/21/2024, 12:00:00 PM');

jest.mock('../../../plugin', () => ({
getRenderCreateAccelerationFlyout: jest.fn(() => jest.fn()),
getRenderAccelerationDetailsFlyout: jest.fn(() => jest.fn()),
Expand Down Expand Up @@ -99,23 +103,6 @@ describe('AssociatedObjectsTab', () => {
global.Date = OriginalDate;
});

// Mock the Intl.DateTimeFormat to use UTC for consistency
jest.spyOn(Intl, 'DateTimeFormat').mockImplementation(() => {
return {
format: (date) =>
new Date(date).toLocaleString('en-US', {
timeZone: 'UTC',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true,
}),
} as any;
});

test('renders without crashing', () => {
renderComponent();
expect(
Expand Down

0 comments on commit ebd3a6a

Please sign in to comment.