From 976c116d1593533966f49e90033fcc10ba95020c Mon Sep 17 00:00:00 2001 From: davidalves1 Date: Thu, 28 Jul 2022 01:22:33 +0100 Subject: [PATCH] fix: fixing isValid method when an invalid card number is passed with a valid checksum --- src/creditcard.js | 3 ++- src/creditcard.test.js | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/creditcard.js b/src/creditcard.js index 7505cd0..3a1870c 100644 --- a/src/creditcard.js +++ b/src/creditcard.js @@ -2,6 +2,7 @@ import CARDS from './cards'; const MILLENNIUM = 1000; const DEFAULT_CODE_LENGTH = 3; +const CARD_NUMBER_LENGTH = 16; export const getCreditCardNameByNumber = (number) => { return findCreditCardObjectByNumber(number).name || 'Credit card is invalid!'; @@ -47,7 +48,7 @@ function validateCards(number, cards) { } function hasCorrectLength(number) { - return number && number.length <= 19; + return number && number.length === CARD_NUMBER_LENGTH; } function removeNonNumbersCaracteres(number) { diff --git a/src/creditcard.test.js b/src/creditcard.test.js index cd27b18..3599794 100644 --- a/src/creditcard.test.js +++ b/src/creditcard.test.js @@ -240,6 +240,10 @@ describe('CreditCard', () => { expect(isValid('0000000')).toBeFalsy(); }); + it('should return false when its a INVALID credit card number with a valid checksum', () => { + expect(isValid('132423')).toBeFalsy(); + }); + it('should return false when its a INVALID credit card number and contains cards list', () => { expect(isValid('0000000', { cards: ['visa'] })).toBeFalsy(); });