1+ import { TasksService } from "./tasks.service" ;
2+ import { generateTestVariation , TEST_PROJECT } from '../../_data_' ;
3+ import { Test , TestingModule } from "@nestjs/testing" ;
4+ import { PrismaService } from '../../prisma/prisma.service' ;
5+ import { TestVariationsService } from "../../test-variations/test-variations.service" ;
6+
7+ const initService = async ( {
8+ projectFindManyMock = jest . fn ( ) ,
9+ testVariationFindManyMock = jest . fn ( ) ,
10+ testVariationsDeleteMock = jest . fn ( ) ,
11+ } ) => {
12+ const module : TestingModule = await Test . createTestingModule ( {
13+ providers : [
14+ TasksService ,
15+ {
16+ provide : PrismaService ,
17+ useValue : {
18+ testVariation : {
19+ findMany : testVariationFindManyMock ,
20+ } ,
21+ project : {
22+ findMany : projectFindManyMock ,
23+ } ,
24+ } ,
25+ } ,
26+ {
27+ provide : TestVariationsService ,
28+ useValue : {
29+ delete : testVariationsDeleteMock ,
30+ } ,
31+ } ,
32+ ] ,
33+ } ) . compile ( ) ;
34+
35+ return module . get < TasksService > ( TasksService ) ;
36+ } ;
37+
38+ describe ( 'cleanOldTestVariations' , ( ) => {
39+ let service : TasksService ;
40+
41+ it ( 'findMany' , async ( ) => {
42+ // .Arrange
43+ const project = TEST_PROJECT
44+ const testVariation = generateTestVariation ( )
45+ const projectFindManyMock = jest . fn ( ) . mockResolvedValueOnce ( [ project ] ) ;
46+ const testVariationFindManyMock = jest . fn ( ) . mockResolvedValueOnce ( [ testVariation ] )
47+ const testVariationsDeleteMock = jest . fn ( )
48+ service = await initService ( {
49+ projectFindManyMock : projectFindManyMock ,
50+ testVariationFindManyMock : testVariationFindManyMock ,
51+ testVariationsDeleteMock : testVariationsDeleteMock
52+ } ) ;
53+ const dateNow = new Date ( '2022-10-23' )
54+ jest
55+ . useFakeTimers ( )
56+ . setSystemTime ( dateNow ) ;
57+ const dateRemoveAfter : Date = new Date ( dateNow ) ;
58+ dateRemoveAfter . setDate ( dateRemoveAfter . getDate ( ) - project . maxBranchLifetime ) ;
59+
60+ // .Act
61+ await service . cleanOldTestVariations ( )
62+
63+ // .Assert
64+ expect ( testVariationFindManyMock ) . toHaveBeenCalledWith ( {
65+ where : {
66+ updatedAt : { lte : dateRemoveAfter } ,
67+ branchName : { not : project . mainBranchName } ,
68+ projectId : project . id
69+ }
70+ } ) ;
71+ expect ( testVariationsDeleteMock ) . toBeCalledWith ( testVariation . id ) ;
72+ } ) ;
73+
74+ } )
0 commit comments