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

test: update notification date tests to be timezone agnostic #27925

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions test/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ process.env.PUSH_NOTIFICATIONS_SERVICE_URL =
process.env.PORTFOLIO_URL = 'https://portfolio.test';
process.env.METAMASK_VERSION = 'MOCK_VERSION';
process.env.ENABLE_CONFIRMATION_REDESIGN = 'true';
process.env.TZ = 'UTC';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if this actually has an impact in tests. Can I get other devs to test with/without this environment var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will keep this in the test environment for now. It does not impact any other tests.

Leaving a comment here for future devs: This can be taken out if necessary 😄

22 changes: 11 additions & 11 deletions ui/helpers/utils/notification.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
describe('formatMenuItemDate', () => {
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2024-06-07T09:40:00Z'));
jest.setSystemTime(new Date(Date.UTC(2024, 5, 7, 9, 40, 0))); // 2024-06-07T09:40:00Z
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main culprit. We were setting the system time to a TZ specific date.

We have now changed this to a UTC (timezone agnostic) date. Hopefully this resolves issues that devs may face when running this test locally.

});

afterAll(() => {
Expand All @@ -28,7 +28,7 @@ describe('formatMenuItemDate', () => {

// assert 1 hour ago
assertToday((testDate) => {
testDate.setHours(testDate.getHours() - 1);
testDate.setUTCHours(testDate.getUTCHours() - 1);
return testDate;
});
});
Expand All @@ -42,14 +42,14 @@ describe('formatMenuItemDate', () => {

// assert exactly 1 day ago
assertYesterday((testDate) => {
testDate.setDate(testDate.getDate() - 1);
testDate.setUTCDate(testDate.getUTCDate() - 1);
});

// assert almost a day ago, but was still yesterday
// E.g. if Today way 09:40AM, but date to test was 23 hours ago (yesterday at 10:40AM), we still want to to show yesterday
assertYesterday((testDate) => {
testDate.setDate(testDate.getDate() - 1);
testDate.setHours(testDate.getHours() + 1);
testDate.setUTCDate(testDate.getUTCDate() - 1);
testDate.setUTCHours(testDate.getUTCHours() + 1);
});
});

Expand All @@ -62,18 +62,18 @@ describe('formatMenuItemDate', () => {

// assert exactly 1 month ago
assertMonthsAgo((testDate) => {
testDate.setMonth(testDate.getMonth() - 1);
testDate.setUTCMonth(testDate.getUTCMonth() - 1);
});

// assert 2 months ago
assertMonthsAgo((testDate) => {
testDate.setMonth(testDate.getMonth() - 2);
testDate.setUTCMonth(testDate.getUTCMonth() - 2);
});

// assert almost a month ago (where it is a new month, but not 30 days)
assertMonthsAgo(() => {
// jest mock date is set in july, so we will test with month may
return new Date('2024-05-20T09:40:00Z');
return new Date(Date.UTC(2024, 4, 20, 9, 40, 0)); // 2024-05-20T09:40:00Z
});
});

Expand All @@ -86,18 +86,18 @@ describe('formatMenuItemDate', () => {

// assert exactly 1 year ago
assertYearsAgo((testDate) => {
testDate.setFullYear(testDate.getFullYear() - 1);
testDate.setUTCFullYear(testDate.getUTCFullYear() - 1);
});

// assert 2 years ago
assertYearsAgo((testDate) => {
testDate.setFullYear(testDate.getFullYear() - 2);
testDate.setUTCFullYear(testDate.getUTCFullYear() - 2);
});

// assert almost a year ago (where it is a new year, but not 365 days ago)
assertYearsAgo(() => {
// jest mock date is set in 2024, so we will test with year 2023
return new Date('2023-11-20T09:40:00Z');
return new Date(Date.UTC(2023, 10, 20, 9, 40, 0)); // 2023-11-20T09:40:00Z
});
});
});
Expand Down