@@ -12,7 +12,7 @@ import { CommentDto } from '../shared/dto/comment.dto';
1212import { TestVariationsService } from '../test-variations/test-variations.service' ;
1313import { TestRunDto } from '../test-runs/dto/testRun.dto' ;
1414import { BuildsService } from '../builds/builds.service' ;
15- import { TEST_PROJECT } from '../_data_' ;
15+ import { generateBaseline , generateTestRun , generateTestVariation , TEST_PROJECT } from '../_data_' ;
1616import { getTestVariationUniqueData } from '../utils' ;
1717import { BaselineDataDto } from '../shared/dto/baseline-data.dto' ;
1818import { CompareService } from '../compare/compare.service' ;
@@ -39,6 +39,7 @@ const initService = async ({
3939 testVariationFindManyMock = jest . fn ( ) ,
4040 baselineCreateMock = jest . fn ( ) ,
4141 testVariationFindOrCreateMock = jest . fn ( ) ,
42+ testVariationGetDetailsMock = jest . fn ( ) ,
4243 testVariationFindUniqueMock = jest . fn ( ) ,
4344 projectFindUniqueMock = jest . fn ( ) ,
4445 compareGetDiffMock = jest . fn ( ) ,
@@ -92,6 +93,7 @@ const initService = async ({
9293 provide : TestVariationsService ,
9394 useValue : {
9495 findOrCreate : testVariationFindOrCreateMock ,
96+ getDetails : testVariationGetDetailsMock ,
9597 } ,
9698 } ,
9799 {
@@ -114,31 +116,6 @@ describe('TestRunsService', () => {
114116 const imageBuffer = Buffer . from ( 'Image' ) ;
115117 const ignoreAreas = [ { x : 1 , y : 2 , width : 10 , height : 20 } ] ;
116118 const tempIgnoreAreas = [ { x : 3 , y : 4 , width : 30 , height : 40 } ] ;
117- const baseTestRun : TestRun = {
118- id : 'id' ,
119- imageName : 'imageName' ,
120- diffName : 'diffName' ,
121- baselineName : 'baselineName' ,
122- diffPercent : 1 ,
123- pixelMisMatchCount : 10 ,
124- diffTollerancePercent : 12 ,
125- status : TestStatus . new ,
126- buildId : 'buildId' ,
127- testVariationId : 'testVariationId' ,
128- updatedAt : new Date ( ) ,
129- createdAt : new Date ( ) ,
130- name : 'test run name' ,
131- ignoreAreas : '[]' ,
132- tempIgnoreAreas : '[]' ,
133- browser : 'browser' ,
134- device : 'device' ,
135- os : 'os' ,
136- viewport : 'viewport' ,
137- branchName : 'develop' ,
138- baselineBranchName : 'master' ,
139- comment : 'some comment' ,
140- merge : false ,
141- } ;
142119
143120 it ( 'findOne' , async ( ) => {
144121 const id = 'some id' ;
@@ -657,4 +634,126 @@ describe('TestRunsService', () => {
657634 expect ( service [ 'tryAutoApproveByNewBaselines' ] ) . toHaveBeenCalledWith ( { testVariation, testRun } ) ;
658635 expect ( mocked ( TestRunResultDto ) ) . toHaveBeenCalledWith ( testRun , testVariation ) ;
659636 } ) ;
637+
638+ describe ( 'tryAutoApproveByNewBaselines' , ( ) => {
639+ it ( 'skip if ok' , async ( ) => {
640+ const testRun = generateTestRun ( { status : TestStatus . ok } ) ;
641+ service = await initService ( { } ) ;
642+
643+ const result = await service [ 'tryAutoApproveByNewBaselines' ] ( {
644+ testVariation : generateTestVariation ( ) ,
645+ testRun,
646+ } ) ;
647+
648+ expect ( result ) . toBe ( testRun ) ;
649+ } ) ;
650+
651+ it ( 'should not auto approve' , async ( ) => {
652+ const testRun = generateTestRun ( { status : TestStatus . unresolved } ) ;
653+ const baseline = generateBaseline ( ) ;
654+ const testRunFindManyMock = jest . fn ( ) . mockResolvedValueOnce ( [ generateTestRun ( { status : TestStatus . approved } ) ] ) ;
655+ const testVariationGetDetailsMock = jest . fn ( ) . mockResolvedValueOnce ( generateTestVariation ( { } , [ baseline ] ) ) ;
656+ service = await initService ( { testRunFindManyMock, testVariationGetDetailsMock } ) ;
657+ service [ 'shouldAutoApprove' ] = jest . fn ( ) . mockResolvedValueOnce ( false ) ;
658+
659+ const result = await service [ 'tryAutoApproveByNewBaselines' ] ( {
660+ testVariation : generateTestVariation ( ) ,
661+ testRun,
662+ } ) ;
663+
664+ expect ( service [ 'shouldAutoApprove' ] ) . toHaveBeenCalled ( ) ;
665+ expect ( result ) . toBe ( testRun ) ;
666+ } ) ;
667+
668+ it ( 'should auto approve' , async ( ) => {
669+ const testRun = generateTestRun ( { status : TestStatus . unresolved } ) ;
670+ const baseline = generateBaseline ( ) ;
671+ const testRunFindManyMock = jest . fn ( ) . mockResolvedValueOnce ( [ generateTestRun ( { status : TestStatus . approved } ) ] ) ;
672+ const testVariationGetDetailsMock = jest . fn ( ) . mockResolvedValueOnce ( generateTestVariation ( { } , [ baseline ] ) ) ;
673+ service = await initService ( { testRunFindManyMock, testVariationGetDetailsMock } ) ;
674+ service [ 'shouldAutoApprove' ] = jest . fn ( ) . mockResolvedValueOnce ( true ) ;
675+ service . approve = jest . fn ( ) . mockResolvedValueOnce ( {
676+ ...testRun ,
677+ status : TestStatus . autoApproved ,
678+ } ) ;
679+
680+ const result = await service [ 'tryAutoApproveByNewBaselines' ] ( {
681+ testVariation : generateTestVariation ( ) ,
682+ testRun,
683+ } ) ;
684+
685+ expect ( result ) . toStrictEqual ( {
686+ ...testRun ,
687+ status : TestStatus . autoApproved ,
688+ } ) ;
689+ } ) ;
690+ } ) ;
691+
692+ describe ( 'tryAutoApproveByPastBaselines' , ( ) => {
693+ it ( 'skip if ok' , async ( ) => {
694+ const testRun = generateTestRun ( { status : TestStatus . ok } ) ;
695+ service = await initService ( { } ) ;
696+
697+ const result = await service [ 'tryAutoApproveByPastBaselines' ] ( {
698+ testVariation : generateTestVariation ( ) ,
699+ testRun,
700+ } ) ;
701+
702+ expect ( result ) . toBe ( testRun ) ;
703+ } ) ;
704+
705+ it ( 'skip if the branch the same as baseline' , async ( ) => {
706+ const testRun = generateTestRun ( { status : TestStatus . unresolved , branchName : 'a' , baselineBranchName : 'a' } ) ;
707+ service = await initService ( { } ) ;
708+
709+ const result = await service [ 'tryAutoApproveByPastBaselines' ] ( {
710+ testVariation : generateTestVariation ( ) ,
711+ testRun,
712+ } ) ;
713+
714+ expect ( result ) . toBe ( testRun ) ;
715+ } ) ;
716+
717+ it ( 'should not auto approve' , async ( ) => {
718+ const testRun = generateTestRun ( { status : TestStatus . unresolved } ) ;
719+ const baseline = generateBaseline ( ) ;
720+ const testVariationGetDetailsMock = jest
721+ . fn ( )
722+ . mockResolvedValueOnce ( generateTestVariation ( { } , [ baseline , baseline ] ) ) ;
723+ service = await initService ( { testVariationGetDetailsMock } ) ;
724+ service [ 'shouldAutoApprove' ] = jest . fn ( ) . mockResolvedValueOnce ( false ) ;
725+
726+ const result = await service [ 'tryAutoApproveByPastBaselines' ] ( {
727+ testVariation : generateTestVariation ( ) ,
728+ testRun,
729+ } ) ;
730+
731+ expect ( service [ 'shouldAutoApprove' ] ) . toHaveBeenCalled ( ) ;
732+ expect ( result ) . toBe ( testRun ) ;
733+ } ) ;
734+
735+ it ( 'should auto approve' , async ( ) => {
736+ const testRun = generateTestRun ( { status : TestStatus . unresolved } ) ;
737+ const baseline = generateBaseline ( ) ;
738+ const testVariationGetDetailsMock = jest
739+ . fn ( )
740+ . mockResolvedValueOnce ( generateTestVariation ( { } , [ baseline , baseline ] ) ) ;
741+ service = await initService ( { testVariationGetDetailsMock } ) ;
742+ service [ 'shouldAutoApprove' ] = jest . fn ( ) . mockResolvedValueOnce ( true ) ;
743+ service . approve = jest . fn ( ) . mockResolvedValueOnce ( {
744+ ...testRun ,
745+ status : TestStatus . autoApproved ,
746+ } ) ;
747+
748+ const result = await service [ 'tryAutoApproveByPastBaselines' ] ( {
749+ testVariation : generateTestVariation ( ) ,
750+ testRun,
751+ } ) ;
752+
753+ expect ( result ) . toStrictEqual ( {
754+ ...testRun ,
755+ status : TestStatus . autoApproved ,
756+ } ) ;
757+ } ) ;
758+ } ) ;
660759} ) ;
0 commit comments