Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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