Skip to content
Draft
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
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ codecov:
after_n_builds: 3

comment:
require_changes: "coverage_drop OR uncovered_patch"
layout: "condensed_header, diff, condensed_footer"
require_changes: false
layout: "condensed_header, files, condensed_footer"
behavior: default
hide_project_coverage: true

Expand Down
2 changes: 1 addition & 1 deletion src/libs/CurrencyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

let currencyList: OnyxValues[typeof ONYXKEYS.CURRENCY_LIST] = {};

Onyx.connect({

Check warning on line 11 in src/libs/CurrencyUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function

Check warning on line 11 in src/libs/CurrencyUtils.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.CURRENCY_LIST,
callback: (val) => {
if (!val || Object.keys(val).length === 0) {
Expand All @@ -24,7 +24,7 @@
* For currencies that have decimal places > 2, floor to 2 instead:
* https://github.com/Expensify/App/issues/15878#issuecomment-1496291464
*
* @param currency - IOU currency
* @param currency - IOU currency defaults to USD
*/
function getCurrencyDecimals(currency: string = CONST.CURRENCY.USD): number {
const decimals = currencyList?.[currency]?.decimals;
Expand Down
1 change: 1 addition & 0 deletions src/libs/DistanceRequestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export default {
getRateByCustomUnitRateID,
getDistanceForDisplayLabel,
convertDistanceUnit,
ensureRateDefined
};

export type {MileageRate};
137 changes: 0 additions & 137 deletions tests/unit/DateUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {addDays, addMinutes, format, setHours, setMinutes, subDays, subHours, subMinutes, subSeconds} from 'date-fns';

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'subSeconds' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'subMinutes' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'subHours' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'subDays' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'setMinutes' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'setHours' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'addMinutes' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'addDays' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'subSeconds' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'subMinutes' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'subHours' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'subDays' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'setMinutes' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'setHours' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'addMinutes' is defined but never used

Check failure on line 2 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'addDays' is defined but never used
import {toZonedTime, format as tzFormat} from 'date-fns-tz';

Check failure on line 3 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'tzFormat' is defined but never used

Check failure on line 3 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'toZonedTime' is defined but never used

Check failure on line 3 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'tzFormat' is defined but never used

Check failure on line 3 in tests/unit/DateUtilsTest.ts

View workflow job for this annotation

GitHub Actions / ESLint check

'toZonedTime' is defined but never used
import Onyx from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
Expand Down Expand Up @@ -56,143 +56,6 @@
expect(zoneAbbreviation).toBe('GMT-8');
});

it('formatToLongDateWithWeekday should return a long date with a weekday', () => {
const formattedDate = DateUtils.formatToLongDateWithWeekday(datetime);
expect(formattedDate).toBe('Monday, November 7, 2022');
});

it('formatToDayOfWeek should return a weekday', () => {
const weekDay = DateUtils.formatToDayOfWeek(new Date(datetime));
expect(weekDay).toBe('Monday');
});
it('formatToLocalTime should return a date in a local format', () => {
const localTime = DateUtils.formatToLocalTime(datetime);
expect(localTime).toBe('12:00 AM');
});

it('should return a date object with the formatted datetime when calling getLocalDateFromDatetime', () => {
const localDate = DateUtils.getLocalDateFromDatetime(LOCALE, timezone, datetime);
expect(tzFormat(localDate, CONST.DATE.FNS_TIMEZONE_FORMAT_STRING, {timeZone: timezone})).toEqual('2022-11-06T16:00:00-08:00');
});

it('should fallback to current date when getLocalDateFromDatetime is failing', () => {
const localDate = DateUtils.getLocalDateFromDatetime(LOCALE, 'InvalidTimezone' as SelectedTimezone, undefined);
expect(localDate.getTime()).not.toBeNaN();
});

it('should return the date in calendar time when calling datetimeToCalendarTime', () => {
const today = setMinutes(setHours(new Date(), 14), 32).toString();
expect(DateUtils.datetimeToCalendarTime(LOCALE, today, UTC as SelectedTimezone, false)).toBe('Today at 2:32 PM');

const tomorrow = addDays(setMinutes(setHours(new Date(), 14), 32), 1).toString();
expect(DateUtils.datetimeToCalendarTime(LOCALE, tomorrow, UTC as SelectedTimezone, false)).toBe('Tomorrow at 2:32 PM');

const yesterday = setMinutes(setHours(subDays(new Date(), 1), 7), 43).toString();
expect(DateUtils.datetimeToCalendarTime(LOCALE, yesterday, UTC as SelectedTimezone, false)).toBe('Yesterday at 7:43 AM');

const date = setMinutes(setHours(new Date('2022-11-05'), 10), 17).toString();
expect(DateUtils.datetimeToCalendarTime(LOCALE, date, UTC as SelectedTimezone, false)).toBe('Nov 5, 2022 at 10:17 AM');

const todayLowercaseDate = setMinutes(setHours(new Date(), 14), 32).toString();
expect(DateUtils.datetimeToCalendarTime(LOCALE, todayLowercaseDate, UTC as SelectedTimezone, false, true)).toBe('today at 2:32 PM');
});

it('should update timezone if automatic and selected timezone do not match', async () => {
jest.spyOn(Intl, 'DateTimeFormat').mockImplementation(
() =>
({
resolvedOptions: () => ({timeZone: 'America/Chicago'}),
}) as Intl.DateTimeFormat,
);
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, {'999': {accountID: 999, timezone: {selected: 'Europe/London', automatic: true}}});
await waitForBatchedUpdates();
const result = DateUtils.getCurrentTimezone({selected: 'Europe/London', automatic: true});
expect(result).toEqual({
selected: 'America/Chicago',
automatic: true,
});
});

it('should not update timezone if automatic and selected timezone match', async () => {
jest.spyOn(Intl, 'DateTimeFormat').mockImplementation(
() =>
({
resolvedOptions: () => ({timeZone: UTC}),
}) as Intl.DateTimeFormat,
);
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, {'999': {accountID: 999, timezone: {selected: 'Europe/London', automatic: true}}});
await waitForBatchedUpdates();
const result = DateUtils.getCurrentTimezone({selected: 'Europe/London', automatic: true});
expect(result).toEqual({
selected: UTC,
automatic: true,
});
});

it('canUpdateTimezone should return true when lastUpdatedTimezoneTime is more than 5 minutes ago', () => {
// Use fake timers to control the current time
jest.useFakeTimers();
jest.setSystemTime(addMinutes(new Date(), 6));
const isUpdateTimezoneAllowed = DateUtils.canUpdateTimezone();
expect(isUpdateTimezoneAllowed).toBe(true);
});

it('canUpdateTimezone should return false when lastUpdatedTimezoneTime is less than 5 minutes ago', () => {
// Use fake timers to control the current time
jest.useFakeTimers();
DateUtils.setTimezoneUpdated();
jest.setSystemTime(addMinutes(new Date(), 4));
const isUpdateTimezoneAllowed = DateUtils.canUpdateTimezone();
expect(isUpdateTimezoneAllowed).toBe(false);
});

it('should return the date in calendar time when calling datetimeToRelative', () => {
const aFewSecondsAgo = subSeconds(new Date(), 10).toString();
expect(DateUtils.datetimeToRelative(LOCALE, aFewSecondsAgo, UTC as SelectedTimezone)).toBe('less than a minute ago');

const aMinuteAgo = subMinutes(new Date(), 1).toString();
expect(DateUtils.datetimeToRelative(LOCALE, aMinuteAgo, UTC as SelectedTimezone)).toBe('1 minute ago');

const anHourAgo = subHours(new Date(), 1).toString();
expect(DateUtils.datetimeToRelative(LOCALE, anHourAgo, UTC as SelectedTimezone)).toBe('about 1 hour ago');
});

it('subtractMillisecondsFromDateTime should subtract milliseconds from a given date and time', () => {
const initialDateTime = '2023-07-18T10:30:00Z';
const millisecondsToSubtract = 5000; // 5 seconds
const expectedDateTime = '2023-07-18 10:29:55.000';
const result = DateUtils.subtractMillisecondsFromDateTime(initialDateTime, millisecondsToSubtract);
expect(result).toBe(expectedDateTime);
});

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

const todayInTimezone = toZonedTime(today, timezone);
const tomorrowInTimezone = toZonedTime(tomorrow, timezone);
const yesterdayInTimezone = toZonedTime(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
2 changes: 2 additions & 0 deletions tests/unit/DistanceRequestUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
const reportID = '1234';
const isPolicyExpenseChat = true;

DistanceRequestUtils.ensureRateDefined(2134);

Check failure on line 101 in tests/unit/DistanceRequestUtilsTest.ts

View workflow job for this annotation

GitHub Actions / typecheck

Assertions require every name in the call target to be declared with an explicit type annotation.

const result = DistanceRequestUtils.getCustomUnitRateID({
reportID,
isPolicyExpenseChat,
Expand Down
Loading