Skip to content

Commit

Permalink
Adding unit tests for isToday, isTomorrow and isYesterday
Browse files Browse the repository at this point in the history
  • Loading branch information
abzokhattab committed Sep 26, 2023
1 parent fd9f36e commit 114e047
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ const DateUtils = {
subtractMillisecondsFromDateTime,
getDateStringFromISOTimestamp,
getStatusUntilDate,
isToday,
isTomorrow,
isYesterday,
};

export default DateUtils;
30 changes: 29 additions & 1 deletion tests/unit/DateUtilsTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Onyx from 'react-native-onyx';
import {format as tzFormat} from 'date-fns-tz';
import {format as tzFormat, utcToZonedTime} from 'date-fns-tz';
import {addMinutes, subHours, subMinutes, subSeconds, format, setMinutes, setHours, subDays, addDays} from 'date-fns';
import CONST from '../../src/CONST';
import DateUtils from '../../src/libs/DateUtils';
Expand Down Expand Up @@ -130,6 +130,34 @@ describe('DateUtils', () => {
expect(result).toBe(expectedDateTime);
});

describe('Date Comparison Functions', () => {
const today = new Date();
const tomorrow = addDays(today, 1);
const yesterday = subDays(today, 1);

const todayInTimezone = utcToZonedTime(today, timezone);
const tomorrowInTimezone = utcToZonedTime(tomorrow, timezone);
const yesterdayInTimezone = utcToZonedTime(yesterday, timezone);

it('isToday should correctly identify today', () => {
expect(DateUtils.isToday(todayInTimezone, timezone)).toBe(true);
expect(DateUtils.isToday(tomorrowInTimezone, timezone)).toBe(false);
expect(DateUtils.isToday(yesterdayInTimezone, timezone)).toBe(false);
});

it('isTomorrow should correctly identify tomorrow', () => {
expect(DateUtils.isTomorrow(tomorrowInTimezone, timezone)).toBe(true);
expect(DateUtils.isTomorrow(todayInTimezone, timezone)).toBe(false);
expect(DateUtils.isTomorrow(yesterdayInTimezone, timezone)).toBe(false);
});

it('isYesterday should correctly identify yesterday', () => {
expect(DateUtils.isYesterday(yesterdayInTimezone, timezone)).toBe(true);
expect(DateUtils.isYesterday(todayInTimezone, timezone)).toBe(false);
expect(DateUtils.isYesterday(tomorrowInTimezone, timezone)).toBe(false);
});
});

describe('getDBTime', () => {
it('should return the date in the format expected by the database', () => {
const getDBTime = DateUtils.getDBTime();
Expand Down

0 comments on commit 114e047

Please sign in to comment.