11import axios , { AxiosError , AxiosResponse } from "axios" ;
22import { mocked } from "jest-mock" ;
33import FormData from "form-data" ;
4-
4+ import { testRunOkResponse , testRunUnresolvedResponse } from "./__data__" ;
55import { VisualRegressionTracker } from "./visualRegressionTracker" ;
66import {
77 Config ,
@@ -164,17 +164,6 @@ const testRunBuffer: TestRunBuffer = {
164164 comment : "comment" ,
165165} ;
166166
167- const testRunResponse : TestRunResponse = {
168- url : "url" ,
169- status : TestStatus . unresolved ,
170- pixelMisMatchCount : 12 ,
171- diffPercent : 0.12 ,
172- diffTollerancePercent : 0 ,
173- id : "some id" ,
174- imageName : "imageName" ,
175- merge : false ,
176- } ;
177-
178167describe ( "VisualRegressionTracker" , ( ) => {
179168 let vrt : VisualRegressionTracker ;
180169
@@ -249,34 +238,42 @@ describe("VisualRegressionTracker", () => {
249238 ) ;
250239 } ) ;
251240
252- it ( "should track base64" , async ( ) => {
241+ it ( "should track base64 without retry" , async ( ) => {
242+ const responce = testRunOkResponse ;
253243 vrt [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
254- vrt [ "submitTestRunBase64" ] = jest
255- . fn ( )
256- . mockResolvedValueOnce ( testRunResponse ) ;
257- vrt [ "processTestRun" ] = jest . fn ( ) ;
244+ vrt [ "submitTestRunBase64" ] = jest . fn ( ) . mockResolvedValue ( responce ) ;
258245
259246 await vrt . track ( testRunBase64 ) ;
260247
261248 expect ( vrt [ "submitTestRunBase64" ] ) . toHaveBeenCalledWith ( testRunBase64 ) ;
262- expect ( vrt [ "processTestRun " ] ) . toHaveBeenCalledWith ( testRunResponse ) ;
249+ expect ( vrt [ "submitTestRunBase64 " ] ) . toHaveBeenCalledTimes ( 1 ) ;
263250 expect ( mockedTestRunResult ) . toHaveBeenCalledWith (
264- testRunResponse ,
251+ responce ,
265252 "http://localhost:4200"
266253 ) ;
267254 } ) ;
268255
269- it ( "should track multipart" , async ( ) => {
256+ it ( "should track base64 with retry" , async ( ) => {
257+ const responce = testRunUnresolvedResponse ;
258+ vrt [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
259+ vrt [ "submitTestRunBase64" ] = jest . fn ( ) . mockResolvedValue ( responce ) ;
260+
261+ await expect ( vrt . track ( testRunBase64 , 3 ) ) . rejects . toThrowError (
262+ "Difference found: url"
263+ ) ;
264+ expect ( vrt [ "submitTestRunBase64" ] ) . toHaveBeenCalledTimes ( 4 ) ;
265+ expect ( vrt [ "submitTestRunBase64" ] ) . toHaveBeenCalledWith ( testRunBase64 ) ;
266+ } ) ;
267+
268+ it ( "should track multipart without retry" , async ( ) => {
269+ const responce = testRunOkResponse ;
270270 const data = new FormData ( ) ;
271271 const buildId = "1312" ;
272272 const projectId = "asd" ;
273273 vrt [ "buildId" ] = buildId ;
274274 vrt [ "projectId" ] = projectId ;
275275 vrt [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
276- vrt [ "submitTestRunMultipart" ] = jest
277- . fn ( )
278- . mockResolvedValueOnce ( testRunResponse ) ;
279- vrt [ "processTestRun" ] = jest . fn ( ) ;
276+ vrt [ "submitTestRunMultipart" ] = jest . fn ( ) . mockResolvedValueOnce ( responce ) ;
280277 mockedDtoHelper . multipartDtoToFormData . mockReturnValueOnce ( data ) ;
281278
282279 await vrt . track ( testRunMultipart ) ;
@@ -287,25 +284,47 @@ describe("VisualRegressionTracker", () => {
287284 branchName : config . branchName ,
288285 ...testRunMultipart ,
289286 } ) ;
287+ expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledTimes ( 1 ) ;
290288 expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledWith ( data ) ;
291- expect ( vrt [ "processTestRun" ] ) . toHaveBeenCalledWith ( testRunResponse ) ;
292289 expect ( mockedTestRunResult ) . toHaveBeenCalledWith (
293- testRunResponse ,
290+ responce ,
294291 "http://localhost:4200"
295292 ) ;
296293 } ) ;
297294
295+ it ( "should track multipart with retry" , async ( ) => {
296+ const responce = testRunUnresolvedResponse ;
297+ const data = new FormData ( ) ;
298+ const buildId = "1312" ;
299+ const projectId = "asd" ;
300+ vrt [ "buildId" ] = buildId ;
301+ vrt [ "projectId" ] = projectId ;
302+ vrt [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
303+ vrt [ "submitTestRunMultipart" ] = jest . fn ( ) . mockResolvedValue ( responce ) ;
304+ mockedDtoHelper . multipartDtoToFormData . mockReturnValueOnce ( data ) ;
305+
306+ await expect ( vrt . track ( testRunMultipart , 3 ) ) . rejects . toThrowError (
307+ "Difference found: url"
308+ ) ;
309+ expect ( mockedDtoHelper . multipartDtoToFormData ) . toHaveBeenCalledWith ( {
310+ buildId,
311+ projectId,
312+ branchName : config . branchName ,
313+ ...testRunMultipart ,
314+ } ) ;
315+ expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledTimes ( 4 ) ;
316+ expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledWith ( data ) ;
317+ } ) ;
318+
298319 it ( "should track buffer" , async ( ) => {
320+ const responce = testRunOkResponse ;
299321 const data = new FormData ( ) ;
300322 const buildId = "1312" ;
301323 const projectId = "asd" ;
302324 vrt [ "buildId" ] = buildId ;
303325 vrt [ "projectId" ] = projectId ;
304326 vrt [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
305- vrt [ "submitTestRunMultipart" ] = jest
306- . fn ( )
307- . mockResolvedValueOnce ( testRunResponse ) ;
308- vrt [ "processTestRun" ] = jest . fn ( ) ;
327+ vrt [ "submitTestRunMultipart" ] = jest . fn ( ) . mockResolvedValueOnce ( responce ) ;
309328 mockedDtoHelper . bufferDtoToFormData . mockReturnValueOnce ( data ) ;
310329
311330 await vrt . track ( testRunBuffer ) ;
@@ -317,9 +336,8 @@ describe("VisualRegressionTracker", () => {
317336 ...testRunBuffer ,
318337 } ) ;
319338 expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledWith ( data ) ;
320- expect ( vrt [ "processTestRun" ] ) . toHaveBeenCalledWith ( testRunResponse ) ;
321339 expect ( mockedTestRunResult ) . toHaveBeenCalledWith (
322- testRunResponse ,
340+ responce ,
323341 "http://localhost:4200"
324342 ) ;
325343 } ) ;
@@ -415,7 +433,7 @@ describe("VisualRegressionTracker", () => {
415433
416434 describe ( "submitTestRunBase64" , ( ) => {
417435 it ( "should submit test run" , async ( ) => {
418- const testRunResponse : TestRunResponse = {
436+ const testRunUnresolvedResponse : TestRunResponse = {
419437 url : "url" ,
420438 status : TestStatus . unresolved ,
421439 pixelMisMatchCount : 12 ,
@@ -429,11 +447,13 @@ describe("VisualRegressionTracker", () => {
429447 const projectId = "asd" ;
430448 vrt [ "buildId" ] = buildId ;
431449 vrt [ "projectId" ] = projectId ;
432- mockedAxios . post . mockResolvedValueOnce ( { data : testRunResponse } ) ;
450+ mockedAxios . post . mockResolvedValueOnce ( {
451+ data : testRunUnresolvedResponse ,
452+ } ) ;
433453
434454 const result = await vrt [ "submitTestRunBase64" ] ( testRunBase64 ) ;
435455
436- expect ( result ) . toBe ( testRunResponse ) ;
456+ expect ( result ) . toBe ( testRunUnresolvedResponse ) ;
437457 expect ( mockedAxios . post ) . toHaveBeenCalledWith (
438458 `${ config . apiUrl } /test-runs` ,
439459 {
@@ -478,11 +498,13 @@ describe("VisualRegressionTracker", () => {
478498 describe ( "submitTestRunMultipart" , ( ) => {
479499 it ( "should submit test run" , async ( ) => {
480500 const data = new FormData ( ) ;
481- mockedAxios . post . mockResolvedValueOnce ( { data : testRunResponse } ) ;
501+ mockedAxios . post . mockResolvedValueOnce ( {
502+ data : testRunUnresolvedResponse ,
503+ } ) ;
482504
483505 const result = await vrt [ "submitTestRunMultipart" ] ( data ) ;
484506
485- expect ( result ) . toBe ( testRunResponse ) ;
507+ expect ( result ) . toBe ( testRunUnresolvedResponse ) ;
486508 expect ( mockedAxios . post ) . toHaveBeenCalledWith (
487509 `${ config . apiUrl } /test-runs/multipart` ,
488510 data ,
@@ -546,32 +568,4 @@ describe("VisualRegressionTracker", () => {
546568 ) ;
547569 } ) ;
548570 } ) ;
549-
550- describe . each < [ TestStatus . new | TestStatus . unresolved , string ] > ( [
551- [ TestStatus . new , "No baseline: " ] ,
552- [ TestStatus . unresolved , "Difference found: " ] ,
553- ] ) ( "processTestRun" , ( status , expectedMessage ) => {
554- beforeEach ( ( ) => {
555- testRunResponse . status = status ;
556- } ) ;
557-
558- it ( `disabled soft assert should throw exception if status ${ status } ` , ( ) => {
559- vrt [ "config" ] . enableSoftAssert = false ;
560-
561- expect ( ( ) => vrt [ "processTestRun" ] ( testRunResponse ) ) . toThrowError (
562- new Error ( expectedMessage . concat ( testRunResponse . url ) )
563- ) ;
564- } ) ;
565-
566- it ( `enabled soft assert should log error if status ${ status } ` , ( ) => {
567- console . error = jest . fn ( ) ;
568- vrt [ "config" ] . enableSoftAssert = true ;
569-
570- vrt [ "processTestRun" ] ( testRunResponse ) ;
571-
572- expect ( console . error ) . toHaveBeenCalledWith (
573- expectedMessage . concat ( testRunResponse . url )
574- ) ;
575- } ) ;
576- } ) ;
577571} ) ;
0 commit comments