Skip to content

Commit 984fffb

Browse files
authored
fix(payment-stripe): card expiration date validator (#168)
1 parent 3a13baa commit 984fffb

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from 'chai';
2+
import sinon from 'sinon';
3+
import isCardExpirationDateValid from '@helpers/is-card-expiration-date-valid';
4+
5+
describe('helpers/is-card-expiration-date-valid', () => {
6+
let clock: sinon.SinonFakeTimers;
7+
8+
beforeEach(() => {
9+
const currentDate = new Date('2024-01-30');
10+
11+
clock = sinon.useFakeTimers(currentDate);
12+
});
13+
14+
afterEach(() => {
15+
clock.restore();
16+
});
17+
18+
it('should be valid: expiration date is valid', () => {
19+
expect(isCardExpirationDateValid('02/25')).to.true;
20+
});
21+
22+
it('should be valid: current month - last card month', () => {
23+
expect(isCardExpirationDateValid('01/24')).to.true;
24+
});
25+
26+
it('should not be valid: expiration month is missing', () => {
27+
expect(isCardExpirationDateValid('/25')).to.false;
28+
});
29+
30+
it('should not be valid: expiration year is missing', () => {
31+
expect(isCardExpirationDateValid('02/')).to.false;
32+
});
33+
34+
it('should not be valid: expiration date is in the past', () => {
35+
expect(isCardExpirationDateValid('12/23')).to.false;
36+
});
37+
});

microservices/payment-stripe/src/helpers/is-card-expiration-date-valid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Check if card expiration date valid
33
*/
4-
const isCardExpirationDateValid = (date: string) => {
4+
const isCardExpirationDateValid = (date: string): boolean => {
55
const currentDate = new Date();
66
const [expirationMonth, expirationYear] = date.split('/');
77

@@ -11,7 +11,7 @@ const isCardExpirationDateValid = (date: string) => {
1111

1212
const expirationDate = new Date(
1313
getExpirationYear(expirationYear, currentDate),
14-
Number(expirationMonth) - 1,
14+
Number(expirationMonth),
1515
1,
1616
);
1717

0 commit comments

Comments
 (0)