@@ -257,195 +257,101 @@ describe('PaymasterWorker', () => {
257257 MockDate . reset ( ) ;
258258 } ) ;
259259
260- describe ( 'Blocked workspace reminder tests' , ( ) => {
261- test ( 'Should remind admins for blocked workspace if it has subscription and after payday passed 1 day' , async ( ) => {
262- /**
263- * Arrange
264- */
265- const currentDate = new Date ( '2005-12-23' ) ;
266- const plan = createPlanMock ( {
267- monthlyCharge : 100 ,
268- isDefault : true ,
269- } ) ;
270- const workspace = createWorkspaceMock ( {
271- plan,
272- subscriptionId : 'some-subscription-id' ,
273- lastChargeDate : new Date ( '2005-11-22' ) ,
274- isBlocked : true ,
275- billingPeriodEventsCount : 10 ,
276- } ) ;
260+ /**
261+ * Helper function to run blocked workspace reminder test
262+ *
263+ * @param lastChargeDate - date of last charge
264+ * @param currentDate - current date to test
265+ * @param shouldBeCalled - whether the reminder should be called
266+ * @param expectedDaysAfterPayday - expected days after payday in the call
267+ */
268+ const testBlockedWorkspaceReminder = async (
269+ lastChargeDate : Date ,
270+ currentDate : Date ,
271+ shouldBeCalled : boolean ,
272+ expectedDaysAfterPayday ?: number
273+ ) : Promise < jest . SpyInstance > => {
274+ const plan = createPlanMock ( {
275+ monthlyCharge : 100 ,
276+ isDefault : true ,
277+ } ) ;
278+ const workspace = createWorkspaceMock ( {
279+ plan,
280+ subscriptionId : 'some-subscription-id' ,
281+ lastChargeDate,
282+ isBlocked : true ,
283+ billingPeriodEventsCount : 10 ,
284+ } ) ;
277285
278- await fillDatabaseWithMockedData ( {
279- workspace,
280- plan,
281- } ) ;
286+ await fillDatabaseWithMockedData ( {
287+ workspace,
288+ plan,
289+ } ) ;
282290
283- MockDate . set ( currentDate ) ;
291+ MockDate . set ( currentDate ) ;
284292
285- /**
286- * Act
287- */
288- const worker = new PaymasterWorker ( ) ;
289- const blockWorkspaceSpy = jest . spyOn ( worker , 'addTask' ) ;
293+ const worker = new PaymasterWorker ( ) ;
294+ const addTaskSpy = jest . spyOn ( worker , 'addTask' ) ;
290295
291- await worker . start ( ) ;
292- await worker . handle ( WORKSPACE_SUBSCRIPTION_CHECK ) ;
293- await worker . finish ( ) ;
296+ await worker . start ( ) ;
297+ await worker . handle ( WORKSPACE_SUBSCRIPTION_CHECK ) ;
298+ await worker . finish ( ) ;
294299
295- /**
296- * Assert
297- */
298- expect ( blockWorkspaceSpy ) . toHaveBeenCalledWith ( 'sender/email' , {
300+ if ( shouldBeCalled ) {
301+ expect ( addTaskSpy ) . toHaveBeenCalledWith ( 'sender/email' , {
299302 type : 'blocked-workspace-reminder' ,
300303 payload : {
301304 workspaceId : workspace . _id . toString ( ) ,
302- daysAfterPayday : 1 ,
305+ daysAfterPayday : expectedDaysAfterPayday ,
303306 } ,
304307 } ) ;
305- MockDate . reset ( ) ;
306- } ) ;
307-
308- test ( 'Should remind admins for blocked workspace if it has subscription and after payday passed 5 days' , async ( ) => {
309- /**
310- * Arrange
311- */
312- const currentDate = new Date ( '2005-12-27' ) ;
313- const plan = createPlanMock ( {
314- monthlyCharge : 100 ,
315- isDefault : true ,
316- } ) ;
317- const workspace = createWorkspaceMock ( {
318- plan,
319- subscriptionId : 'some-subscription-id' ,
320- lastChargeDate : new Date ( '2005-11-22' ) ,
321- isBlocked : true ,
322- billingPeriodEventsCount : 10 ,
323- } ) ;
324-
325- await fillDatabaseWithMockedData ( {
326- workspace,
327- plan,
328- } ) ;
329-
330- MockDate . set ( currentDate ) ;
308+ } else {
309+ expect ( addTaskSpy ) . not . toHaveBeenCalledWith ( 'sender/email' , expect . objectContaining ( {
310+ type : 'blocked-workspace-reminder' ,
311+ } ) ) ;
312+ }
331313
332- /**
333- * Act
334- */
335- const worker = new PaymasterWorker ( ) ;
336- const blockWorkspaceSpy = jest . spyOn ( worker , 'addTask' ) ;
314+ MockDate . reset ( ) ;
315+ return addTaskSpy ;
316+ } ;
337317
338- await worker . start ( ) ;
339- await worker . handle ( WORKSPACE_SUBSCRIPTION_CHECK ) ;
340- await worker . finish ( ) ;
318+ describe ( 'Blocked workspace reminder tests' , ( ) => {
319+ test ( 'Should remind admins for blocked workspace if it has subscription and after payday passed 1 day' , async ( ) => {
320+ await testBlockedWorkspaceReminder (
321+ new Date ( '2005-11-22' ) ,
322+ new Date ( '2005-12-23' ) ,
323+ true ,
324+ 1
325+ ) ;
326+ } ) ;
341327
342- /**
343- * Assert
344- */
345- expect ( blockWorkspaceSpy ) . toHaveBeenCalledWith ( 'sender/email' , {
346- type : 'blocked-workspace-reminder' ,
347- payload : {
348- workspaceId : workspace . _id . toString ( ) ,
349- daysAfterPayday : 5 ,
350- } ,
351- } ) ;
352- MockDate . reset ( ) ;
328+ test ( 'Should remind admins for blocked workspace if it has subscription and after payday passed 5 days' , async ( ) => {
329+ await testBlockedWorkspaceReminder (
330+ new Date ( '2005-11-22' ) ,
331+ new Date ( '2005-12-27' ) ,
332+ true ,
333+ 5
334+ ) ;
353335 } ) ;
354336
355337 test ( 'Should remind admins for blocked workspace if it has subscription and after payday passed 30 days' , async ( ) => {
356- /**
357- * Arrange
358- */
359- const currentDate = new Date ( '2006-01-21' ) ;
360- const plan = createPlanMock ( {
361- monthlyCharge : 100 ,
362- isDefault : true ,
363- } ) ;
364- const workspace = createWorkspaceMock ( {
365- plan,
366- subscriptionId : 'some-subscription-id' ,
367- lastChargeDate : new Date ( '2005-11-22' ) ,
368- isBlocked : true ,
369- billingPeriodEventsCount : 10 ,
370- } ) ;
371-
372- await fillDatabaseWithMockedData ( {
373- workspace,
374- plan,
375- } ) ;
376-
377- MockDate . set ( currentDate ) ;
378-
379- /**
380- * Act
381- */
382- const worker = new PaymasterWorker ( ) ;
383- const blockWorkspaceSpy = jest . spyOn ( worker , 'addTask' ) ;
384-
385- await worker . start ( ) ;
386- await worker . handle ( WORKSPACE_SUBSCRIPTION_CHECK ) ;
387- await worker . finish ( ) ;
388-
389- /**
390- * Assert
391- */
392- expect ( blockWorkspaceSpy ) . toHaveBeenCalledWith ( 'sender/email' , {
393- type : 'blocked-workspace-reminder' ,
394- payload : {
395- workspaceId : workspace . _id . toString ( ) ,
396- daysAfterPayday : 30 ,
397- } ,
398- } ) ;
399- MockDate . reset ( ) ;
338+ await testBlockedWorkspaceReminder (
339+ new Date ( '2005-11-22' ) ,
340+ new Date ( '2006-01-21' ) ,
341+ true ,
342+ 30
343+ ) ;
400344 } ) ;
401345
402346 test ( 'Should not remind admins for blocked workspace on days not in reminder schedule (day 4)' , async ( ) => {
403- /**
404- * Arrange
405- */
406- const currentDate = new Date ( '2005-12-26' ) ;
407- const plan = createPlanMock ( {
408- monthlyCharge : 100 ,
409- isDefault : true ,
410- } ) ;
411- const workspace = createWorkspaceMock ( {
412- plan,
413- subscriptionId : 'some-subscription-id' ,
414- lastChargeDate : new Date ( '2005-11-22' ) ,
415- isBlocked : true ,
416- billingPeriodEventsCount : 10 ,
417- } ) ;
418-
419- await fillDatabaseWithMockedData ( {
420- workspace,
421- plan,
422- } ) ;
423-
424- MockDate . set ( currentDate ) ;
425-
426- /**
427- * Act
428- */
429- const worker = new PaymasterWorker ( ) ;
430- const blockWorkspaceSpy = jest . spyOn ( worker , 'addTask' ) ;
431-
432- await worker . start ( ) ;
433- await worker . handle ( WORKSPACE_SUBSCRIPTION_CHECK ) ;
434- await worker . finish ( ) ;
435-
436- /**
437- * Assert
438- */
439- expect ( blockWorkspaceSpy ) . not . toHaveBeenCalledWith ( 'sender/email' , {
440- type : 'blocked-workspace-reminder' ,
441- payload : {
442- workspaceId : workspace . _id . toString ( ) ,
443- daysAfterPayday : 4 ,
444- } ,
445- } ) ;
446- MockDate . reset ( ) ;
347+ await testBlockedWorkspaceReminder (
348+ new Date ( '2005-11-22' ) ,
349+ new Date ( '2005-12-26' ) ,
350+ false
351+ ) ;
447352 } ) ;
448353 } ) ;
354+ } ) ;
449355
450356 test ( 'Should update lastChargeDate and billingPeriodEventsCount if workspace has free tariff plan and it\'s time to pay' , async ( ) => {
451357 /**
0 commit comments