Skip to content

Commit

Permalink
test: update notification date tests to be timezone agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
Prithpal-Sooriya committed Oct 17, 2024
1 parent 935ad43 commit f52f2a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
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';
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
});

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

0 comments on commit f52f2a8

Please sign in to comment.