-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathcards.test.ts
More file actions
59 lines (50 loc) · 1.7 KB
/
Copy pathcards.test.ts
File metadata and controls
59 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cardFormattingPatterns from '@helpers/cards/cardFormattingPatterns';
import formatValueByPattern from '@helpers/cards/formatValueByPattern';
import {validateCardExpiry, validateCardNumber} from '@helpers/cards/validateCard';
describe('Card number', () => {
test('Format', () => {
const data = [
['4242424242424242', '4242 4242 4242 4242'],
['371758885524003', '3717 588855 24003']
];
data.forEach(([plain, formatted]) => {
const result = formatValueByPattern(cardFormattingPatterns.cardNumber, plain);
expect(result.value).toEqual(formatted);
});
});
test('Validate', () => {
const data = [
['4242424242424242', null],
['4242424242424241', 'invalid'],
['424242424242424', 'incomplete']
];
data.forEach(([cardNumber, code]) => {
const result = validateCardNumber(cardNumber);
if(code) {
expect(result.code).toEqual(code);
} else {
expect(result).toEqual(null);
}
});
});
});
describe('Expiry date', () => {
const joiner = '/';
const getExpiryDate = (date: Date) => `${date.getMonth()}${joiner}${date.getFullYear() % 100}`;
test('Format', () => {
const month = 10;
const year = 20;
const {value} = formatValueByPattern(cardFormattingPatterns.cardExpiry, `${month}${year}`);
expect(value).toEqual(`${month}${joiner}${year}`);
});
test('Expired', () => {
const date = new Date();
date.setMonth(date.getMonth() - 1);
expect(validateCardExpiry(getExpiryDate(date))).toBeTruthy();
});
test('Nonexpired', () => {
const date = new Date();
date.setFullYear(date.getFullYear() + 1);
expect(validateCardExpiry(getExpiryDate(date))).toEqual(null);
});
});