@@ -36,6 +36,13 @@ describe('Class: DynamoDbPersistenceLayer', () => {
36
36
} ) ;
37
37
38
38
describe ( 'Method: _putRecord' , ( ) => {
39
+ const currentDateInMilliseconds = 1000 ;
40
+ const currentDateInSeconds = 1 ;
41
+
42
+ beforeEach ( ( ) => {
43
+ jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDateInMilliseconds ) ;
44
+ } ) ;
45
+
39
46
test ( 'when called with a record that succeeds condition, it puts record in dynamo table' , async ( ) => {
40
47
// Prepare
41
48
const tableName = 'tableName' ;
@@ -46,10 +53,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
46
53
const expiryTimestamp = 0 ;
47
54
const inProgressExpiryTimestamp = 0 ;
48
55
const record = new IdempotencyRecord ( key , status , expiryTimestamp , inProgressExpiryTimestamp , undefined , undefined ) ;
49
-
50
- const currentDate = 1 ;
51
- jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDate ) ;
52
-
53
56
const dynamoClient = mockClient ( DynamoDBDocument ) . on ( PutCommand ) . resolves ( { } ) ;
54
57
55
58
// Act
@@ -60,7 +63,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
60
63
TableName : tableName ,
61
64
Item : { 'id' : key , 'expiration' : expiryTimestamp , status : status } ,
62
65
ExpressionAttributeNames : { '#id' : 'id' , '#expiry' : 'expiration' , '#status' : 'status' } ,
63
- ExpressionAttributeValues : { ':now' : currentDate , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
66
+ ExpressionAttributeValues : { ':now' : currentDateInSeconds , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
64
67
ConditionExpression : 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
65
68
} ) ;
66
69
} ) ;
@@ -76,9 +79,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
76
79
const inProgressExpiryTimestamp = 0 ;
77
80
const record = new IdempotencyRecord ( key , status , expiryTimestamp , inProgressExpiryTimestamp , undefined , undefined ) ;
78
81
79
- const currentDate = 1 ;
80
- jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDate ) ;
81
-
82
82
const dynamoClient = mockClient ( DynamoDBDocument ) . on ( PutCommand ) . rejects ( { name : 'ConditionalCheckFailedException' } ) ;
83
83
84
84
// Act
@@ -94,7 +94,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
94
94
TableName : tableName ,
95
95
Item : { 'id' : key , 'expiration' : expiryTimestamp , status : status } ,
96
96
ExpressionAttributeNames : { '#id' : 'id' , '#expiry' : 'expiration' , '#status' : 'status' } ,
97
- ExpressionAttributeValues : { ':now' : currentDate , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
97
+ ExpressionAttributeValues : { ':now' : currentDateInSeconds , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
98
98
ConditionExpression : 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
99
99
} ) ;
100
100
expect ( error ) . toBeInstanceOf ( IdempotencyItemAlreadyExistsError ) ;
@@ -111,9 +111,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
111
111
const inProgressExpiryTimestamp = 0 ;
112
112
const record = new IdempotencyRecord ( key , status , expiryTimestamp , inProgressExpiryTimestamp , undefined , undefined ) ;
113
113
114
- const currentDate = 1 ;
115
- jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDate ) ;
116
-
117
114
const dynamoClient = mockClient ( DynamoDBDocument ) . on ( PutCommand ) . rejects ( new Error ( ) ) ;
118
115
119
116
// Act
@@ -129,7 +126,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
129
126
TableName : tableName ,
130
127
Item : { 'id' : key , 'expiration' : expiryTimestamp , status : status } ,
131
128
ExpressionAttributeNames : { '#id' : 'id' , '#expiry' : 'expiration' , '#status' : 'status' } ,
132
- ExpressionAttributeValues : { ':now' : currentDate , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
129
+ ExpressionAttributeValues : { ':now' : currentDateInSeconds , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
133
130
ConditionExpression : 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
134
131
} ) ;
135
132
expect ( error ) . toBe ( error ) ;
0 commit comments