@@ -36,6 +36,13 @@ describe('Class: DynamoDbPersistenceLayer', () => {
3636 } ) ;
3737
3838 describe ( 'Method: _putRecord' , ( ) => {
39+ const currentDateInMilliseconds = 1000 ;
40+ const currentDateInSeconds = 1 ;
41+
42+ beforeEach ( ( ) => {
43+ jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDateInMilliseconds ) ;
44+ } ) ;
45+
3946 test ( 'when called with a record that succeeds condition, it puts record in dynamo table' , async ( ) => {
4047 // Prepare
4148 const tableName = 'tableName' ;
@@ -46,10 +53,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
4653 const expiryTimestamp = 0 ;
4754 const inProgressExpiryTimestamp = 0 ;
4855 const record = new IdempotencyRecord ( key , status , expiryTimestamp , inProgressExpiryTimestamp , undefined , undefined ) ;
49-
50- const currentDate = 1 ;
51- jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDate ) ;
52-
5356 const dynamoClient = mockClient ( DynamoDBDocument ) . on ( PutCommand ) . resolves ( { } ) ;
5457
5558 // Act
@@ -60,7 +63,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
6063 TableName : tableName ,
6164 Item : { 'id' : key , 'expiration' : expiryTimestamp , status : status } ,
6265 ExpressionAttributeNames : { '#id' : 'id' , '#expiry' : 'expiration' , '#status' : 'status' } ,
63- ExpressionAttributeValues : { ':now' : currentDate , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
66+ ExpressionAttributeValues : { ':now' : currentDateInSeconds , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
6467 ConditionExpression : 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
6568 } ) ;
6669 } ) ;
@@ -76,9 +79,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
7679 const inProgressExpiryTimestamp = 0 ;
7780 const record = new IdempotencyRecord ( key , status , expiryTimestamp , inProgressExpiryTimestamp , undefined , undefined ) ;
7881
79- const currentDate = 1 ;
80- jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDate ) ;
81-
8282 const dynamoClient = mockClient ( DynamoDBDocument ) . on ( PutCommand ) . rejects ( { name : 'ConditionalCheckFailedException' } ) ;
8383
8484 // Act
@@ -94,7 +94,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
9494 TableName : tableName ,
9595 Item : { 'id' : key , 'expiration' : expiryTimestamp , status : status } ,
9696 ExpressionAttributeNames : { '#id' : 'id' , '#expiry' : 'expiration' , '#status' : 'status' } ,
97- ExpressionAttributeValues : { ':now' : currentDate , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
97+ ExpressionAttributeValues : { ':now' : currentDateInSeconds , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
9898 ConditionExpression : 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
9999 } ) ;
100100 expect ( error ) . toBeInstanceOf ( IdempotencyItemAlreadyExistsError ) ;
@@ -111,9 +111,6 @@ describe('Class: DynamoDbPersistenceLayer', () => {
111111 const inProgressExpiryTimestamp = 0 ;
112112 const record = new IdempotencyRecord ( key , status , expiryTimestamp , inProgressExpiryTimestamp , undefined , undefined ) ;
113113
114- const currentDate = 1 ;
115- jest . spyOn ( Date , 'now' ) . mockReturnValue ( currentDate ) ;
116-
117114 const dynamoClient = mockClient ( DynamoDBDocument ) . on ( PutCommand ) . rejects ( new Error ( ) ) ;
118115
119116 // Act
@@ -129,7 +126,7 @@ describe('Class: DynamoDbPersistenceLayer', () => {
129126 TableName : tableName ,
130127 Item : { 'id' : key , 'expiration' : expiryTimestamp , status : status } ,
131128 ExpressionAttributeNames : { '#id' : 'id' , '#expiry' : 'expiration' , '#status' : 'status' } ,
132- ExpressionAttributeValues : { ':now' : currentDate , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
129+ ExpressionAttributeValues : { ':now' : currentDateInSeconds , ':inprogress' : IdempotencyRecordStatus . INPROGRESS } ,
133130 ConditionExpression : 'attribute_not_exists(#id) OR #expiry < :now OR NOT #status = :inprogress'
134131 } ) ;
135132 expect ( error ) . toBe ( error ) ;
0 commit comments