Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VickyStash committed Nov 15, 2024
1 parent fb4b456 commit fc97fdc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function maskCard(lastFour = ''): string {
* @param bank - card bank.
* @returns - The masked card string.
*/
function maskCardNumber(cardName: string, bank: string): string {
if (!cardName || cardName === '' || bank === '') {
function maskCardNumber(cardName: string, bank: string | undefined): string {
if (!cardName || cardName === '' || !bank) {
return '';
}
const hasSpace = /\s/.test(cardName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function ConfirmationStep({policyID, backTo}: ConfirmationStepProps) {
/>
<MenuItemWithTopDescription
description={translate('workspace.companyCards.card')}
title={CardUtils.maskCardNumber(data?.cardNumber ?? '', data?.bankName ?? '')}
title={CardUtils.maskCardNumber(data?.cardNumber ?? '', data?.bankName)}
shouldShowRightIcon
onPress={() => editStep(CONST.COMPANY_CARD.STEP.CARD)}
/>
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/CardUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,41 @@ describe('CardUtils', () => {
expect(feedName).toBe(undefined);
});
});

describe('maskCardNumber', () => {
it("Should return the card number divided into chunks of 4, with 'X' replaced by '•' if it's provided in the '480801XXXXXX2554' format", () => {
const cardNumber = '480801XXXXXX2554';
const maskedCardNumber = CardUtils.maskCardNumber(cardNumber, CONST.COMPANY_CARD.FEED_BANK_NAME.MASTER_CARD);
expect(maskedCardNumber).toBe('4808 01•• •••• 2554');
});

it("Should return card number without changes if it's provided in the 'CREDIT CARD...6607' format", () => {
const cardNumber = 'CREDIT CARD...6607';
const maskedCardNumber = CardUtils.maskCardNumber(cardNumber, CONST.COMPANY_CARD.FEED_BANK_NAME.CHASE);
expect(maskedCardNumber).toBe(cardNumber);
});

it("Should return the Amex direct feed card number divided into 4/6/5 chunks, with 'X' replaced by '•'", () => {
const cardNumber = '211944XXXXX6557';
const maskedCardNumber = CardUtils.maskCardNumber(cardNumber, CONST.COMPANY_CARD.FEED_BANK_NAME.AMEX_DIRECT);
expect(maskedCardNumber).toBe('2119 44•••• •6557');
});

it("Should return the Amex custom feed card number divided into 4/6/5 chunks, with 'X' replaced by '•'", () => {
const cardNumber = '211944XXXXX6557';
const maskedCardNumber = CardUtils.maskCardNumber(cardNumber, CONST.COMPANY_CARD.FEED_BANK_NAME.AMEX);
expect(maskedCardNumber).toBe('2119 44•••• •6557');
});

it('Should return empty string if undefined bank was provided', () => {
const cardNumber = '480801XXXXXX2554';
const maskedCardNumber = CardUtils.maskCardNumber(cardNumber, undefined);
expect(maskedCardNumber).toBe('');
});

it('Should return empty string if invalid card name was provided', () => {
const maskedCardNumber = CardUtils.maskCardNumber('', CONST.COMPANY_CARD.FEED_BANK_NAME.MASTER_CARD);
expect(maskedCardNumber).toBe('');
});
});
});

0 comments on commit fc97fdc

Please sign in to comment.