Skip to content

Commit

Permalink
Use fake/fixed date in unit tests (#2091)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Resolves #2090

## Description of the changes
- Mock the current date to a fix value to avoid time of year variations
in behavior

---------

Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro authored Jan 5, 2024
1 parent 911ccf5 commit 868bb05
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions packages/jaeger-ui/src/utils/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,21 @@ describe('convertToTimeUnit', () => {
});

describe('formatRelativeDate', () => {
const currentTimestamp = Date.now();
const currentDate = new Date(currentTimestamp);
let currentTimestamp;
let currentDate;
beforeEach(() => {
// Set a fixed date to avoid issues like https://github.com/jaegertracing/jaeger-ui/issues/2090.
jest.useFakeTimers();
jest.setSystemTime(new Date(2023, 7, 19, 10, 30));

it('Displays Date MM-DD-YYY (Different Year) ', () => {
currentTimestamp = Date.now();
currentDate = new Date(currentTimestamp);
});

it('Displays Date MMM-DD-YYYY (Different Year) ', () => {
const input = new Date(currentTimestamp);
input.setFullYear(currentDate.getFullYear() - 2);
const output = input.toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
});
expect(formatRelativeDate(input)).toBe(output);
expect(formatRelativeDate(input)).toBe('Aug 19, 2021');
});
it('Displays Today (Todays date)', () => {
expect(formatRelativeDate(currentDate)).toBe('Today');
Expand All @@ -218,15 +221,10 @@ describe('formatRelativeDate', () => {
input.setDate(currentDate.getDate() - 1);
expect(formatRelativeDate(input)).toBe('Yesterday');
});
it('Displays MM-DAY (Same month different date)', () => {
it('Displays MMM-DD (Same month different date)', () => {
const input = new Date(currentTimestamp);
input.setDate(currentDate.getDate() - 4);
const output = input.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
});
expect(formatRelativeDate(input)).toBe(output);
expect(formatRelativeDate(input)).toBe('Aug 15');
});
});

Expand Down

0 comments on commit 868bb05

Please sign in to comment.