Skip to content

Commit 2a2bbd0

Browse files
authored
Merge pull request #93294 from Expensify/claude-fixSplitDistanceMileageRate
2 parents 5342cdf + efcc596 commit 2a2bbd0

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/libs/actions/IOU/MoneyRequest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ function startMoneyRequest(
407407
isFromFloatingActionButton?: boolean,
408408
) {
409409
const sourceRoute = Navigation.getActiveRoute();
410+
// 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.
411+
if (iouType === CONST.IOU.TYPE.SPLIT) {
412+
getDefaultP2PMileageRate();
413+
}
410414
startSpan(CONST.TELEMETRY.SPAN_OPEN_CREATE_EXPENSE, {
411415
name: '/money-request-create',
412416
op: CONST.TELEMETRY.SPAN_OPEN_CREATE_EXPENSE,

tests/unit/DefaultP2PMileageRateTest.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Onyx from 'react-native-onyx';
2+
import {startMoneyRequest} from '@libs/actions/IOU/MoneyRequest';
23
import {getDefaultP2PMileageRate} from '@libs/actions/Transaction';
34
import * as API from '@libs/API';
45
import {READ_COMMANDS} from '@libs/API/types';
@@ -9,6 +10,9 @@ import ONYXKEYS from '@src/ONYXKEYS';
910
import createRandomTransaction from '../utils/collections/transaction';
1011
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
1112

13+
jest.mock('@libs/Navigation/Navigation');
14+
jest.mock('@libs/telemetry/activeSpans');
15+
1216
describe('Default P2P mileage rate', () => {
1317
beforeAll(() => {
1418
Onyx.init({
@@ -34,6 +38,29 @@ describe('Default P2P mileage rate', () => {
3438
});
3539
});
3640

41+
describe('startMoneyRequest', () => {
42+
// Splitting a distance expense goes through startMoneyRequest, so it must prefetch the default P2P mileage rate
43+
// (the same way startDistanceRequest does) or the Distance tab loads with a malformed rate.
44+
it('prefetches the default P2P mileage rate when starting a split distance request', () => {
45+
// eslint-disable-next-line rulesdir/no-multiple-api-calls
46+
const readSpy = jest.spyOn(API, 'read').mockImplementation(() => {});
47+
48+
startMoneyRequest(CONST.IOU.TYPE.SPLIT, '1', [], CONST.IOU.REQUEST_TYPE.DISTANCE);
49+
50+
expect(readSpy).toHaveBeenCalledWith(READ_COMMANDS.GET_DEFAULT_P2P_MILEAGE_RATE, null);
51+
});
52+
53+
// Non-split flows never expose a Distance tab, so they must not issue the (uncached) prefetch read.
54+
it('does not prefetch the default P2P mileage rate for non-split requests', () => {
55+
// eslint-disable-next-line rulesdir/no-multiple-api-calls
56+
const readSpy = jest.spyOn(API, 'read').mockImplementation(() => {});
57+
58+
startMoneyRequest(CONST.IOU.TYPE.SUBMIT, '1', [], CONST.IOU.REQUEST_TYPE.MANUAL);
59+
60+
expect(readSpy).not.toHaveBeenCalledWith(READ_COMMANDS.GET_DEFAULT_P2P_MILEAGE_RATE, null);
61+
});
62+
});
63+
3764
describe('getRateForP2P', () => {
3865
it('falls back to USD 67¢/mile when no rate has been stored in Onyx', () => {
3966
const result = DistanceRequestUtils.getRateForP2P('EUR', undefined);

0 commit comments

Comments
 (0)