1
1
import axios , { AxiosError , AxiosResponse } from "axios" ;
2
2
import { mocked } from "jest-mock" ;
3
3
import FormData from "form-data" ;
4
-
4
+ import { testRunOkResponse , testRunUnresolvedResponse } from "./__data__" ;
5
5
import { VisualRegressionTracker } from "./visualRegressionTracker" ;
6
6
import {
7
7
Config ,
@@ -164,17 +164,6 @@ const testRunBuffer: TestRunBuffer = {
164
164
comment : "comment" ,
165
165
} ;
166
166
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
-
178
167
describe ( "VisualRegressionTracker" , ( ) => {
179
168
let vrt : VisualRegressionTracker ;
180
169
@@ -249,34 +238,42 @@ describe("VisualRegressionTracker", () => {
249
238
) ;
250
239
} ) ;
251
240
252
- it ( "should track base64" , async ( ) => {
241
+ it ( "should track base64 without retry" , async ( ) => {
242
+ const responce = testRunOkResponse ;
253
243
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 ) ;
258
245
259
246
await vrt . track ( testRunBase64 ) ;
260
247
261
248
expect ( vrt [ "submitTestRunBase64" ] ) . toHaveBeenCalledWith ( testRunBase64 ) ;
262
- expect ( vrt [ "processTestRun " ] ) . toHaveBeenCalledWith ( testRunResponse ) ;
249
+ expect ( vrt [ "submitTestRunBase64 " ] ) . toHaveBeenCalledTimes ( 1 ) ;
263
250
expect ( mockedTestRunResult ) . toHaveBeenCalledWith (
264
- testRunResponse ,
251
+ responce ,
265
252
"http://localhost:4200"
266
253
) ;
267
254
} ) ;
268
255
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 ;
270
270
const data = new FormData ( ) ;
271
271
const buildId = "1312" ;
272
272
const projectId = "asd" ;
273
273
vrt [ "buildId" ] = buildId ;
274
274
vrt [ "projectId" ] = projectId ;
275
275
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 ) ;
280
277
mockedDtoHelper . multipartDtoToFormData . mockReturnValueOnce ( data ) ;
281
278
282
279
await vrt . track ( testRunMultipart ) ;
@@ -287,25 +284,47 @@ describe("VisualRegressionTracker", () => {
287
284
branchName : config . branchName ,
288
285
...testRunMultipart ,
289
286
} ) ;
287
+ expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledTimes ( 1 ) ;
290
288
expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledWith ( data ) ;
291
- expect ( vrt [ "processTestRun" ] ) . toHaveBeenCalledWith ( testRunResponse ) ;
292
289
expect ( mockedTestRunResult ) . toHaveBeenCalledWith (
293
- testRunResponse ,
290
+ responce ,
294
291
"http://localhost:4200"
295
292
) ;
296
293
} ) ;
297
294
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
+
298
319
it ( "should track buffer" , async ( ) => {
320
+ const responce = testRunOkResponse ;
299
321
const data = new FormData ( ) ;
300
322
const buildId = "1312" ;
301
323
const projectId = "asd" ;
302
324
vrt [ "buildId" ] = buildId ;
303
325
vrt [ "projectId" ] = projectId ;
304
326
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 ) ;
309
328
mockedDtoHelper . bufferDtoToFormData . mockReturnValueOnce ( data ) ;
310
329
311
330
await vrt . track ( testRunBuffer ) ;
@@ -317,9 +336,8 @@ describe("VisualRegressionTracker", () => {
317
336
...testRunBuffer ,
318
337
} ) ;
319
338
expect ( vrt [ "submitTestRunMultipart" ] ) . toHaveBeenCalledWith ( data ) ;
320
- expect ( vrt [ "processTestRun" ] ) . toHaveBeenCalledWith ( testRunResponse ) ;
321
339
expect ( mockedTestRunResult ) . toHaveBeenCalledWith (
322
- testRunResponse ,
340
+ responce ,
323
341
"http://localhost:4200"
324
342
) ;
325
343
} ) ;
@@ -415,7 +433,7 @@ describe("VisualRegressionTracker", () => {
415
433
416
434
describe ( "submitTestRunBase64" , ( ) => {
417
435
it ( "should submit test run" , async ( ) => {
418
- const testRunResponse : TestRunResponse = {
436
+ const testRunUnresolvedResponse : TestRunResponse = {
419
437
url : "url" ,
420
438
status : TestStatus . unresolved ,
421
439
pixelMisMatchCount : 12 ,
@@ -429,11 +447,13 @@ describe("VisualRegressionTracker", () => {
429
447
const projectId = "asd" ;
430
448
vrt [ "buildId" ] = buildId ;
431
449
vrt [ "projectId" ] = projectId ;
432
- mockedAxios . post . mockResolvedValueOnce ( { data : testRunResponse } ) ;
450
+ mockedAxios . post . mockResolvedValueOnce ( {
451
+ data : testRunUnresolvedResponse ,
452
+ } ) ;
433
453
434
454
const result = await vrt [ "submitTestRunBase64" ] ( testRunBase64 ) ;
435
455
436
- expect ( result ) . toBe ( testRunResponse ) ;
456
+ expect ( result ) . toBe ( testRunUnresolvedResponse ) ;
437
457
expect ( mockedAxios . post ) . toHaveBeenCalledWith (
438
458
`${ config . apiUrl } /test-runs` ,
439
459
{
@@ -478,11 +498,13 @@ describe("VisualRegressionTracker", () => {
478
498
describe ( "submitTestRunMultipart" , ( ) => {
479
499
it ( "should submit test run" , async ( ) => {
480
500
const data = new FormData ( ) ;
481
- mockedAxios . post . mockResolvedValueOnce ( { data : testRunResponse } ) ;
501
+ mockedAxios . post . mockResolvedValueOnce ( {
502
+ data : testRunUnresolvedResponse ,
503
+ } ) ;
482
504
483
505
const result = await vrt [ "submitTestRunMultipart" ] ( data ) ;
484
506
485
- expect ( result ) . toBe ( testRunResponse ) ;
507
+ expect ( result ) . toBe ( testRunUnresolvedResponse ) ;
486
508
expect ( mockedAxios . post ) . toHaveBeenCalledWith (
487
509
`${ config . apiUrl } /test-runs/multipart` ,
488
510
data ,
@@ -546,32 +568,4 @@ describe("VisualRegressionTracker", () => {
546
568
) ;
547
569
} ) ;
548
570
} ) ;
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
- } ) ;
577
571
} ) ;
0 commit comments