|
| 1 | +import { validateCreditCard } from '../ValidateCreditCard' |
| 2 | + |
| 3 | +describe('Validate credit card number', () => { |
| 4 | + it('should throw error if card number is boolean', () => { |
| 5 | + const invalidCC = true |
| 6 | + expect(() => validateCreditCard(invalidCC)).toThrow( |
| 7 | + 'The given value is not a string' |
| 8 | + ) |
| 9 | + }) |
| 10 | + it('returns true if the credit card number is valid', () => { |
| 11 | + const validCreditCard = '4111111111111111' |
| 12 | + const validationResult = validateCreditCard(validCreditCard) |
| 13 | + expect(validationResult).toBe(true) |
| 14 | + }) |
| 15 | + it('should throw an error on non-numeric character in given credit card number', () => { |
| 16 | + const nonNumericCCNumbers = ['123ABCDEF', 'ABCDKDKD', 'ADS232'] |
| 17 | + nonNumericCCNumbers.forEach(nonNumericCC => expect(() => validateCreditCard(nonNumericCC)).toThrow( |
| 18 | + `${nonNumericCC} is an invalid credit card number because ` + 'it has nonnumerical characters.' |
| 19 | + )) |
| 20 | + }) |
| 21 | + it('should throw an error on credit card with invalid length', () => { |
| 22 | + const ccWithInvalidLength = ['41111', '4111111111111111111111'] |
| 23 | + ccWithInvalidLength.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow( |
| 24 | + `${invalidCC} is an invalid credit card number because ` + 'of its length.' |
| 25 | + )) |
| 26 | + }) |
| 27 | + it('should throw an error on credit card with invalid start substring', () => { |
| 28 | + const ccWithInvalidStartSubstring = ['12345678912345', '23456789123456', '789123456789123', '891234567891234', '912345678912345', '31345678912345', '32345678912345', '33345678912345', '38345678912345'] |
| 29 | + ccWithInvalidStartSubstring.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow( |
| 30 | + `${invalidCC} is an invalid credit card number because ` + 'of its first two digits.' |
| 31 | + )) |
| 32 | + }) |
| 33 | + it('should throw an error on credit card with luhn check fail', () => { |
| 34 | + const invalidCCs = ['411111111111111', '371211111111111', '49999999999999'] |
| 35 | + invalidCCs.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow( |
| 36 | + `${invalidCC} is an invalid credit card number because ` + 'it fails the Luhn check.' |
| 37 | + )) |
| 38 | + }) |
| 39 | +}) |
0 commit comments