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
4 changes: 4 additions & 0 deletions src/libs/actions/IOU/MoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ function startMoneyRequest(
isFromFloatingActionButton?: boolean,
) {
const sourceRoute = Navigation.getActiveRoute();
// Only the split flow exposes a Distance tab from here, so prefetch the default P2P mileage rate solely for splits to avoid an unnecessary read on other flows.
if (iouType === CONST.IOU.TYPE.SPLIT) {
getDefaultP2PMileageRate();
}
startSpan(CONST.TELEMETRY.SPAN_OPEN_CREATE_EXPENSE, {
name: '/money-request-create',
op: CONST.TELEMETRY.SPAN_OPEN_CREATE_EXPENSE,
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/DefaultP2PMileageRateTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Onyx from 'react-native-onyx';
import {startMoneyRequest} from '@libs/actions/IOU/MoneyRequest';
import {getDefaultP2PMileageRate} from '@libs/actions/Transaction';
import * as API from '@libs/API';
import {READ_COMMANDS} from '@libs/API/types';
Expand All @@ -9,6 +10,9 @@ import ONYXKEYS from '@src/ONYXKEYS';
import createRandomTransaction from '../utils/collections/transaction';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

jest.mock('@libs/Navigation/Navigation');
jest.mock('@libs/telemetry/activeSpans');

describe('Default P2P mileage rate', () => {
beforeAll(() => {
Onyx.init({
Expand All @@ -34,6 +38,29 @@ describe('Default P2P mileage rate', () => {
});
});

describe('startMoneyRequest', () => {
// Splitting a distance expense goes through startMoneyRequest, so it must prefetch the default P2P mileage rate
// (the same way startDistanceRequest does) or the Distance tab loads with a malformed rate.
it('prefetches the default P2P mileage rate when starting a split distance request', () => {
// eslint-disable-next-line rulesdir/no-multiple-api-calls
const readSpy = jest.spyOn(API, 'read').mockImplementation(() => {});

startMoneyRequest(CONST.IOU.TYPE.SPLIT, '1', [], CONST.IOU.REQUEST_TYPE.DISTANCE);

expect(readSpy).toHaveBeenCalledWith(READ_COMMANDS.GET_DEFAULT_P2P_MILEAGE_RATE, null);
});

// Non-split flows never expose a Distance tab, so they must not issue the (uncached) prefetch read.
it('does not prefetch the default P2P mileage rate for non-split requests', () => {
// eslint-disable-next-line rulesdir/no-multiple-api-calls
const readSpy = jest.spyOn(API, 'read').mockImplementation(() => {});

startMoneyRequest(CONST.IOU.TYPE.SUBMIT, '1', [], CONST.IOU.REQUEST_TYPE.MANUAL);

expect(readSpy).not.toHaveBeenCalledWith(READ_COMMANDS.GET_DEFAULT_P2P_MILEAGE_RATE, null);
});
});

describe('getRateForP2P', () => {
it('falls back to USD 67¢/mile when no rate has been stored in Onyx', () => {
const result = DistanceRequestUtils.getRateForP2P('EUR', undefined);
Expand Down
Loading