@@ -11,11 +11,13 @@ import {
1111import { mocked } from "ts-jest/utils" ;
1212import { MaybeMocked } from "ts-jest/dist/utils/testing" ;
1313
14+ jest . mock ( "@visual-regression-tracker/sdk-js" ) ;
15+
1416let browserType = chromium ;
1517let browser : Browser ;
1618let context : BrowserContext ;
1719let page : Page ;
18- let vrt : PlaywrightVisualRegressionTracker ;
20+ let playwrightVrt : PlaywrightVisualRegressionTracker ;
1921
2022const config : Config = {
2123 apiUrl : "http://localhost:4200" ,
@@ -34,41 +36,40 @@ beforeAll(async () => {
3436 } ,
3537 } ) ;
3638 page = await context . newPage ( ) ;
37- vrt = new PlaywrightVisualRegressionTracker ( browserType , config ) ;
3839} ) ;
3940
4041afterAll ( async ( ) => {
4142 await browser . close ( ) ;
4243} ) ;
4344
4445describe ( "playwright" , ( ) => {
45- it ( "start" , async ( ) => {
46- const startMock = jest . fn ( ) ;
47- VisualRegressionTracker . prototype . start = startMock ;
46+ beforeEach ( ( ) => {
47+ playwrightVrt = new PlaywrightVisualRegressionTracker ( browserType , config ) ;
48+ } ) ;
49+
50+ it ( "constructor" , async ( ) => {
51+ expect ( playwrightVrt [ "browser" ] ) . toBe ( browserType . name ( ) ) ;
52+ expect ( VisualRegressionTracker ) . toHaveBeenCalledWith ( config ) ;
53+ } ) ;
4854
49- await vrt . start ( ) ;
55+ it ( "start" , async ( ) => {
56+ await playwrightVrt . start ( ) ;
5057
51- expect ( startMock ) . toHaveBeenCalled ( ) ;
58+ expect ( VisualRegressionTracker . prototype . start ) . toHaveBeenCalled ( ) ;
5259 } ) ;
5360
5461 it ( "stop" , async ( ) => {
55- const stopMock = jest . fn ( ) ;
56- VisualRegressionTracker . prototype . stop = stopMock ;
62+ await playwrightVrt . stop ( ) ;
5763
58- await vrt . stop ( ) ;
59-
60- expect ( stopMock ) . toHaveBeenCalled ( ) ;
64+ expect ( VisualRegressionTracker . prototype . stop ) . toHaveBeenCalled ( ) ;
6165 } ) ;
6266
6367 describe ( "track" , ( ) => {
64- let trackMock : jest . Mock < any , any > ;
6568 let pageMocked : MaybeMocked < Page > ;
6669 const screenshot : Buffer = Buffer . from ( "image mocked" ) ;
6770
6871 beforeEach ( ( ) => {
69- vrt [ "vrt" ] [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
70- trackMock = jest . fn ( ) ;
71- VisualRegressionTracker . prototype . track = trackMock ;
72+ playwrightVrt [ "vrt" ] [ "isStarted" ] = jest . fn ( ) . mockReturnValueOnce ( true ) ;
7273 pageMocked = mocked ( page ) ;
7374 pageMocked . screenshot = jest . fn ( ) . mockResolvedValueOnce ( screenshot ) ;
7475 } ) ;
@@ -102,12 +103,12 @@ describe("playwright", () => {
102103 } ,
103104 } ;
104105
105- await vrt . trackPage ( page , imageName , trackOptions ) ;
106+ await playwrightVrt . trackPage ( page , imageName , trackOptions ) ;
106107
107108 expect ( pageMocked . screenshot ) . toHaveBeenCalledWith (
108109 trackOptions . screenshotOptions
109110 ) ;
110- expect ( trackMock ) . toHaveBeenCalledWith ( {
111+ expect ( VisualRegressionTracker . prototype . track ) . toHaveBeenCalledWith ( {
111112 name : imageName ,
112113 imageBase64 : screenshot . toString ( "base64" ) ,
113114 browser : browserType . name ( ) ,
@@ -123,10 +124,10 @@ describe("playwright", () => {
123124 const imageName = "test name" ;
124125 pageMocked . viewportSize = jest . fn ( ) . mockReturnValueOnce ( null ) ;
125126
126- await vrt . trackPage ( page , imageName ) ;
127+ await playwrightVrt . trackPage ( page , imageName ) ;
127128
128129 expect ( pageMocked . screenshot ) . toHaveBeenCalledWith ( undefined ) ;
129- expect ( trackMock ) . toHaveBeenCalledWith ( {
130+ expect ( VisualRegressionTracker . prototype . track ) . toHaveBeenCalledWith ( {
130131 name : imageName ,
131132 imageBase64 : screenshot . toString ( "base64" ) ,
132133 browser : browserType . name ( ) ,
@@ -168,12 +169,16 @@ describe("playwright", () => {
168169 . fn ( )
169170 . mockResolvedValueOnce ( screenshot ) ;
170171
171- await vrt . trackElementHandle ( elementHandle , imageName , trackOptions ) ;
172+ await playwrightVrt . trackElementHandle (
173+ elementHandle ,
174+ imageName ,
175+ trackOptions
176+ ) ;
172177
173178 expect ( elementHandleMocked ! . screenshot ) . toHaveBeenCalledWith (
174179 trackOptions . screenshotOptions
175180 ) ;
176- expect ( trackMock ) . toHaveBeenCalledWith ( {
181+ expect ( VisualRegressionTracker . prototype . track ) . toHaveBeenCalledWith ( {
177182 name : imageName ,
178183 imageBase64 : screenshot . toString ( "base64" ) ,
179184 browser : browserType . name ( ) ,
@@ -194,10 +199,10 @@ describe("playwright", () => {
194199 . fn ( )
195200 . mockResolvedValueOnce ( screenshot ) ;
196201
197- await vrt . trackElementHandle ( elementHandle , imageName ) ;
202+ await playwrightVrt . trackElementHandle ( elementHandle , imageName ) ;
198203
199204 expect ( elementHandleMocked ! . screenshot ) . toHaveBeenCalledWith ( undefined ) ;
200- expect ( trackMock ) . toHaveBeenCalledWith ( {
205+ expect ( VisualRegressionTracker . prototype . track ) . toHaveBeenCalledWith ( {
201206 name : imageName ,
202207 imageBase64 : screenshot . toString ( "base64" ) ,
203208 browser : browserType . name ( ) ,
@@ -214,7 +219,7 @@ describe("playwright", () => {
214219 const elementHandle = await page . $ ( "#test" ) ;
215220
216221 await expect (
217- vrt . trackElementHandle ( elementHandle , imageName )
222+ playwrightVrt . trackElementHandle ( elementHandle , imageName )
218223 ) . rejects . toThrowError ( new Error ( "ElementHandle is null" ) ) ;
219224 } ) ;
220225 } ) ;
0 commit comments