Skip to content

Commit fcb8acf

Browse files
authored
Merge pull request Expensify#67520 from allgandalf/fixresetUSDBankAccount
Remove Onyx.connect() for the key: ONYXKEYS.BANK_ACCOUNT_LIST in src/libs/actions/ReimbursementAccount/resetUSDBankAccount.ts
2 parents 22614f3 + fe23d4e commit fcb8acf

3 files changed

Lines changed: 69 additions & 11 deletions

File tree

src/libs/actions/ReimbursementAccount/resetUSDBankAccount.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
1+
import type {OnyxEntry} from 'react-native-onyx';
22
import Onyx from 'react-native-onyx';
33
import * as API from '@libs/API';
44
import {WRITE_COMMANDS} from '@libs/API/types';
55
import CONST from '@src/CONST';
66
import ONYXKEYS from '@src/ONYXKEYS';
77
import INPUT_IDS from '@src/types/form/ReimbursementAccountForm';
88
import type * as OnyxTypes from '@src/types/onyx';
9+
import type {ACHAccount} from '@src/types/onyx/Policy';
910
import type {OnyxData} from '@src/types/onyx/Request';
1011

11-
let allPolicies: OnyxCollection<OnyxTypes.Policy>;
12-
Onyx.connect({
13-
key: ONYXKEYS.COLLECTION.POLICY,
14-
waitForCollectionCallback: true,
15-
callback: (value) => (allPolicies = value),
16-
});
17-
1812
/**
1913
* Reset user's USD reimbursement account. This will delete the bank account
2014
*/
2115
function resetUSDBankAccount(
2216
bankAccountID: number | undefined,
2317
session: OnyxEntry<OnyxTypes.Session>,
2418
policyID: string | undefined,
19+
achAccount: ACHAccount | undefined,
2520
lastUsedPaymentMethod?: OnyxTypes.LastPaymentMethodType,
2621
) {
2722
if (!bankAccountID) {
@@ -31,7 +26,6 @@ function resetUSDBankAccount(
3126
throw new Error('Missing credentials when attempting to reset free plan bank account');
3227
}
3328

34-
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? ({} as OnyxTypes.Policy);
3529
const isLastUsedPaymentMethodBBA = lastUsedPaymentMethod?.expense?.name === CONST.IOU.PAYMENT_TYPE.VBBA;
3630
const isPreviousLastUsedPaymentMethodBBA = lastUsedPaymentMethod?.lastUsed?.name === CONST.IOU.PAYMENT_TYPE.VBBA;
3731

@@ -136,7 +130,7 @@ function resetUSDBankAccount(
136130
onyxMethod: Onyx.METHOD.MERGE,
137131
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
138132
value: {
139-
achAccount: policy?.achAccount,
133+
achAccount,
140134
},
141135
},
142136
],

src/pages/workspace/WorkspaceResetBankAccountModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function WorkspaceResetBankAccountModal({
6969
setNonUSDBankAccountStep(null);
7070
}
7171
} else {
72-
resetUSDBankAccount(bankAccountID, session, policyID, lastPaymentMethod);
72+
resetUSDBankAccount(bankAccountID, session, policyID, policy?.achAccount, lastPaymentMethod);
7373

7474
if (setShouldShowContinueSetupButton) {
7575
setShouldShowContinueSetupButton(false);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Onyx from 'react-native-onyx';
2+
import CONST from '@src/CONST';
3+
import IntlStore from '@src/languages/IntlStore';
4+
import resetUSDBankAccount from '@src/libs/actions/ReimbursementAccount/resetUSDBankAccount';
5+
import ONYXKEYS from '@src/ONYXKEYS';
6+
import type {ACHAccount} from '@src/types/onyx/Policy';
7+
import type {MockFetch} from '../utils/TestHelper';
8+
import * as TestHelper from '../utils/TestHelper';
9+
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
10+
11+
const TEST_EMAIL = 'test@test.com';
12+
const TEST_ACCOUNT_ID = 1;
13+
const bankAccountID = 1;
14+
const policyID = '1234567890';
15+
const session = {email: TEST_EMAIL, accountID: TEST_ACCOUNT_ID};
16+
17+
describe('ReimbursementAccount', () => {
18+
beforeAll(() => {
19+
Onyx.init({
20+
keys: ONYXKEYS,
21+
});
22+
});
23+
24+
let mockFetch: MockFetch;
25+
beforeEach(() => {
26+
global.fetch = TestHelper.getGlobalFetchMock();
27+
mockFetch = fetch as MockFetch;
28+
IntlStore.load(CONST.LOCALES.EN);
29+
return Onyx.clear().then(waitForBatchedUpdates);
30+
});
31+
describe('resetUSDBankAccount', () => {
32+
afterEach(() => {
33+
mockFetch?.resume?.();
34+
});
35+
36+
it('should reset the USDBankAccount', async () => {
37+
(fetch as MockFetch)?.pause?.();
38+
const achAccount: ACHAccount = {
39+
bankAccountID,
40+
addressName: 'Test Address',
41+
bankName: 'Test Bank',
42+
reimburser: TEST_EMAIL,
43+
accountNumber: '1234567890',
44+
routingNumber: '123456789',
45+
};
46+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {achAccount});
47+
resetUSDBankAccount(bankAccountID, session, policyID, achAccount);
48+
49+
return waitForBatchedUpdates().then(
50+
() =>
51+
new Promise<void>((resolve) => {
52+
const connection = Onyx.connect({
53+
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
54+
callback: (policy) => {
55+
Onyx.disconnect(connection);
56+
expect(policy?.achAccount).toBeUndefined();
57+
resolve();
58+
},
59+
});
60+
}),
61+
);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)