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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import getTextInputAutocorrectProps from '@libs/getTextInputAutocorrectProps';
import type BankInfoSubStepProps from '@pages/ReimbursementAccount/NonUSD/BankInfo/types';
import {getBankInfoStepValues} from '@pages/ReimbursementAccount/NonUSD/utils/getBankInfoStepValues';
import getInputForValueSet from '@pages/ReimbursementAccount/NonUSD/utils/getInputForValueSet';
import putBankAddressBeforeBankCity from '@pages/ReimbursementAccount/NonUSD/utils/putBankAddressBeforeBankCity';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -39,7 +40,9 @@ function BankAccountDetails({onNext, isEditing, corpayFields}: BankInfoSubStepPr
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
const [reimbursementAccountDraft] = useOnyx(ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM_DRAFT);

const bankAccountDetailsFields = corpayFields?.formFields?.filter((field) => !field.id.includes(CONST.NON_USD_BANK_ACCOUNT.BANK_INFO_STEP_ACCOUNT_HOLDER_KEY_PREFIX));
const bankAccountDetailsFields = corpayFields?.formFields
? putBankAddressBeforeBankCity(corpayFields.formFields.filter((field) => !field.id.includes(CONST.NON_USD_BANK_ACCOUNT.BANK_INFO_STEP_ACCOUNT_HOLDER_KEY_PREFIX)))
: undefined;

const subStepKeys: Record<keyof ReimbursementAccountForm, keyof ReimbursementAccountForm> = {} as Record<keyof ReimbursementAccountForm, keyof ReimbursementAccountForm>;
for (const field of bankAccountDetailsFields ?? []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {getLatestErrorMessage} from '@libs/ErrorUtils';
import type BankInfoSubStepProps from '@pages/ReimbursementAccount/NonUSD/BankInfo/types';
import {getBankInfoStepValues} from '@pages/ReimbursementAccount/NonUSD/utils/getBankInfoStepValues';
import getInputKeysForBankInfoStep from '@pages/ReimbursementAccount/NonUSD/utils/getInputKeysForBankInfoStep';
import putBankAddressBeforeBankCity from '@pages/ReimbursementAccount/NonUSD/utils/putBankAddressBeforeBankCity';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -26,7 +27,7 @@ function Confirmation({onNext, onMove, isEditing, corpayFields}: BankInfoSubStep
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
const [reimbursementAccountDraft] = useOnyx(ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM_DRAFT);
const inputKeys = getInputKeysForBankInfoStep(corpayFields);
const formFields = corpayFields?.formFields;
const formFields = corpayFields?.formFields ? putBankAddressBeforeBankCity(corpayFields.formFields) : undefined;
const values = getBankInfoStepValues(inputKeys, reimbursementAccountDraft, reimbursementAccount);

const summaryItems = (formFields ?? []).map((field) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import INPUT_IDS from '@src/types/form/ReimbursementAccountForm';
import type {CorpayFormField} from '@src/types/onyx';

/**
* Swaps the Bank address and Bank city fields when the API returns the city first,
* so picking an address can't overwrite a city the user already typed. Every other field keeps its original position.
*/
function putBankAddressBeforeBankCity(fields: CorpayFormField[]): CorpayFormField[] {
const bankCityIndex = fields.findIndex((field) => field.id === INPUT_IDS.ADDITIONAL_DATA.CORPAY.BANK_CITY);
const bankAddressIndex = fields.findIndex((field) => field.id === INPUT_IDS.ADDITIONAL_DATA.CORPAY.BANK_ADDRESS_LINE_1);
const bankCityField = fields.at(bankCityIndex);
const bankAddressField = fields.at(bankAddressIndex);

if (bankCityIndex === -1 || bankAddressIndex === -1 || bankAddressIndex < bankCityIndex || !bankCityField || !bankAddressField) {
return fields;
}

const reorderedFields = [...fields];
reorderedFields[bankCityIndex] = bankAddressField;
reorderedFields[bankAddressIndex] = bankCityField;
return reorderedFields;
}

export default putBankAddressBeforeBankCity;
94 changes: 94 additions & 0 deletions tests/unit/putBankAddressBeforeBankCityTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import putBankAddressBeforeBankCity from '@pages/ReimbursementAccount/NonUSD/utils/putBankAddressBeforeBankCity';

import type {CorpayFormField} from '@src/types/onyx';

function createField(id: string): CorpayFormField {
return {
id,
label: id,
errorMessage: '',
isRequired: true,
isRequiredInValueSet: false,
regEx: '',
validationRules: [],
};
}

function getIDs(fields: CorpayFormField[]): string[] {
return fields.map((field) => field.id);
}

describe('putBankAddressBeforeBankCity', () => {
it('puts Bank address before Bank city when the API returns the city first', () => {
// Given the API returns bankCity before bankAddressLine1, as it does for Croatia/EUR
const fields = [createField('bankName'), createField('bankCity'), createField('bankAddressLine1')];

// When the fields are reordered
const reorderedFields = putBankAddressBeforeBankCity(fields);

// Then Bank address comes first, so picking an address can't overwrite a city the user already typed
expect(getIDs(reorderedFields)).toEqual(['bankName', 'bankAddressLine1', 'bankCity']);
});

it('only swaps Bank address and Bank city and leaves every other field where it was', () => {
// Given Bank city and Bank address mixed with other fields like IBAN, SWIFT and Bank postal
const fields = [createField('accountNumber'), createField('bankPostal'), createField('swiftBicCode'), createField('bankCity'), createField('bankAddressLine1'), createField('IBAN')];

// When the fields are reordered
const reorderedFields = putBankAddressBeforeBankCity(fields);

// Then only Bank city and Bank address trade places
expect(getIDs(reorderedFields)).toEqual(['accountNumber', 'bankPostal', 'swiftBicCode', 'bankAddressLine1', 'bankCity', 'IBAN']);
});

it('swaps Bank address and Bank city when other fields sit between them', () => {
// Given Bank city and Bank address are not next to each other
const fields = [createField('bankCity'), createField('bankPostal'), createField('bankAddressLine1')];

// When the fields are reordered
const reorderedFields = putBankAddressBeforeBankCity(fields);

// Then the two swap slots and the field between them stays put
expect(getIDs(reorderedFields)).toEqual(['bankAddressLine1', 'bankPostal', 'bankCity']);
});

it('returns the same order when Bank address already comes before Bank city', () => {
// Given fields that already have Bank address before Bank city
const fields = [createField('bankName'), createField('bankAddressLine1'), createField('bankCity'), createField('accountNumber')];

// When the fields are reordered
const reorderedFields = putBankAddressBeforeBankCity(fields);

// Then nothing moves
expect(getIDs(reorderedFields)).toEqual(['bankName', 'bankAddressLine1', 'bankCity', 'accountNumber']);
});

it('returns the same order when only one of the two fields is present', () => {
// Given fields with Bank city but no Bank address
const fields = [createField('bankName'), createField('bankCity'), createField('bankPostal')];

// When the fields are reordered
const reorderedFields = putBankAddressBeforeBankCity(fields);

// Then nothing moves
expect(getIDs(reorderedFields)).toEqual(['bankName', 'bankCity', 'bankPostal']);
});

it('does not mutate the original array', () => {
// Given fields in the wrong order
const fields = [createField('bankCity'), createField('bankAddressLine1')];

// When the fields are reordered
putBankAddressBeforeBankCity(fields);

// Then the input array is left untouched, since it comes straight from Onyx
expect(getIDs(fields)).toEqual(['bankCity', 'bankAddressLine1']);
});

it('returns an empty array when there are no fields', () => {
// Given no fields
// When the fields are reordered
// Then an empty array is returned
expect(putBankAddressBeforeBankCity([])).toEqual([]);
});
});
Loading