File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed
microservices/payment-stripe Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change 1
1
/**
2
2
* Check if card expiration date valid
3
3
*/
4
- const isCardExpirationDateValid = ( date : string ) => {
4
+ const isCardExpirationDateValid = ( date : string ) : boolean => {
5
5
const currentDate = new Date ( ) ;
6
6
const [ expirationMonth , expirationYear ] = date . split ( '/' ) ;
7
7
@@ -11,7 +11,7 @@ const isCardExpirationDateValid = (date: string) => {
11
11
12
12
const expirationDate = new Date (
13
13
getExpirationYear ( expirationYear , currentDate ) ,
14
- Number ( expirationMonth ) - 1 ,
14
+ Number ( expirationMonth ) ,
15
15
1 ,
16
16
) ;
17
17
You can’t perform that action at this time.
0 commit comments