Skip to content

Commit

Permalink
fix: today/yesterday keywords failed on last/first day of month (#1336)
Browse files Browse the repository at this point in the history
- Wasn't correctly calculating the yesterday/tomorrow dates
- Unit tests previously were failing when run on the last day of the
month
- Mock the system time for consistency in tests
- Add tests explicitly testing first and last day of month, year
- Fixes #1335
  • Loading branch information
mofojed authored May 31, 2023
1 parent f771d59 commit 4c3fe24
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
65 changes: 59 additions & 6 deletions packages/jsapi-utils/src/DateUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,68 @@ describe('parseDateRange', () => {
]);
});

it('should return a range from today to tomorrow if text is "today"', () => {
const range = DateUtils.parseDateRange(dh, 'today', 'America/New_York');
const start = range[0];
const end = range[1];
if (start && end) {
describe('a range from today to tomorrow if text is keyword "today"', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

test.each([
[new Date(2023, 4, 15)],
[new Date(2023, 4, 1)],
[new Date(2023, 4, 31)],
[new Date(2023, 0, 1)],
[new Date(2023, 11, 31)],
])('should work with keyword "today" for date %s', date => {
jest.setSystemTime(date);
const range = DateUtils.parseDateRange(dh, 'today', 'America/New_York');
const start = range[0];
const end = range[1];
const startDate = start?.asDate();
const endDate = end?.asDate();
expect(startDate).toEqual(date);
expect(endDate).toEqual(
new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1)
);
expect(dateDiffInMillisseconds(startDate, endDate)).toBe(MS_PER_DAY);
}
});
});

describe('a range from yesterday to today if text is keyword "yesterday"', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

test.each([
[new Date(2023, 4, 15)],
[new Date(2023, 4, 1)],
[new Date(2023, 4, 31)],
[new Date(2023, 0, 1)],
[new Date(2023, 11, 31)],
])('should work with keyword "yesterday" for date %s', date => {
jest.setSystemTime(date);
const range = DateUtils.parseDateRange(
dh,
'yesterday',
'America/New_York'
);
const start = range[0];
const end = range[1];
const startDate = start?.asDate();
const endDate = end?.asDate();
expect(startDate).toEqual(
new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1)
);
expect(endDate).toEqual(date);
expect(dateDiffInMillisseconds(startDate, endDate)).toBe(MS_PER_DAY);
});
});

it('should return null as the end range if text is "now"', () => {
Expand Down
22 changes: 16 additions & 6 deletions packages/jsapi-utils/src/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,35 @@ export class DateUtils {
now.getMonth(),
now.getDate()
);
const endDate = DateUtils.makeDateWrapper(
dh,
timeZone,
const tomorrow = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1
);
const endDate = DateUtils.makeDateWrapper(
dh,
timeZone,
tomorrow.getFullYear(),
tomorrow.getMonth(),
tomorrow.getDate()
);
return [startDate, endDate];
}

if (cleanText === 'yesterday') {
const now = new Date(Date.now());
const startDate = DateUtils.makeDateWrapper(
dh,
timeZone,
const yesterday = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() - 1
);
const startDate = DateUtils.makeDateWrapper(
dh,
timeZone,
yesterday.getFullYear(),
yesterday.getMonth(),
yesterday.getDate()
);
const endDate = DateUtils.makeDateWrapper(
dh,
timeZone,
Expand Down

0 comments on commit 4c3fe24

Please sign in to comment.