1- const path = require ( 'path' ) ;
21const fs = require ( 'fs' ) ;
3- const mkdirp = require ( 'mkdirp' ) ;
42const jimp = require ( 'jimp' ) ;
53const utils = require ( '../../../../src/plugins/screenshoter/utils' ) ;
64
@@ -15,18 +13,6 @@ describe('Screenshoter Utils', () => {
1513 sandbox . restore ( ) ;
1614 } ) ;
1715
18- describe ( 'fileExists' , ( ) => {
19- it ( 'should return true if file exists' , ( ) => {
20- sandbox . stub ( fs , 'lstat' ) . callsArgWith ( 1 , false ) ;
21- return expect ( utils . fileExists ( 'foo' ) ) . to . eventually . equal ( true ) ;
22- } ) ;
23-
24- it ( "should return false if file doesn't exist" , ( ) => {
25- sandbox . stub ( fs , 'lstat' ) . callsArgWith ( 1 , true ) ;
26- return expect ( utils . fileExists ( 'foo' ) ) . to . eventually . equal ( false ) ;
27- } ) ;
28- } ) ;
29-
3016 describe ( 'removeFile' , ( ) => {
3117 it ( 'should resolve if the file could be removed' , ( ) => {
3218 sandbox . stub ( fs , 'unlink' ) . callsArgWith ( 1 , false ) ;
@@ -52,86 +38,6 @@ describe('Screenshoter Utils', () => {
5238 } ) ;
5339 } ) ;
5440
55- describe ( 'writeImage' , ( ) => {
56- let img ;
57-
58- beforeEach ( ( ) => {
59- img = { write : sandbox . stub ( ) } ;
60- } ) ;
61-
62- it ( 'should write an image to a file' , ( ) => {
63- img . write . callsArgWith ( 1 ) ;
64- return expect ( utils . writeImage ( img , 'foo' ) ) . to . eventually . be . fulfilled . and . be . an ( 'undefined' ) ;
65- } ) ;
66-
67- it ( "should reject if image file couldn't be created" , ( ) => {
68- img . write . callsArgWith ( 1 , 'error' ) ;
69- return expect ( utils . writeImage ( img , 'foo' ) ) . to . eventually . be . rejectedWith ( 'error' ) ;
70- } ) ;
71- } ) ;
72-
73- describe ( 'compare' , ( ) => {
74- let jimpRead ;
75- let jimpDistance ;
76- let jimpDiff ;
77-
78- beforeEach ( ( ) => {
79- jimpRead = sandbox . stub ( jimp , 'read' ) ;
80- jimpDistance = sandbox . stub ( jimp , 'distance' ) ;
81- jimpDiff = sandbox . stub ( jimp , 'diff' ) ;
82- } ) ;
83-
84- it ( 'should be equal if the tolerance is met' , ( ) => {
85- const distance = 0 ;
86- const diffImg = { } ;
87- const percent = 0 ;
88- jimpRead . returns ( Promise . resolve ( { } ) ) ;
89- jimpDistance . returns ( distance ) ;
90- jimpDiff . returns ( { image : diffImg , percent } ) ;
91-
92- return expect ( utils . compare ( 'baseline' , 'regression' , 0 ) ) . to . eventually . be . fulfilled . and . deep . equal ( {
93- diffImg,
94- isEqual : true ,
95- equality : `distance: ${ distance } , percent: ${ percent } ` ,
96- } ) ;
97- } ) ;
98-
99- it ( "should not be equal if the tolerance isn't met" , ( ) => {
100- const distance = 0.1 ;
101- const diffImg = { } ;
102- const percent = 0 ;
103- jimpRead . returns ( Promise . resolve ( { } ) ) ;
104- jimpDistance . returns ( distance ) ;
105- jimpDiff . returns ( { image : diffImg , percent } ) ;
106-
107- return expect ( utils . compare ( 'baseline' , 'regression' , 0 ) ) . to . eventually . be . fulfilled . and . deep . equal ( {
108- diffImg,
109- isEqual : false ,
110- equality : `distance: ${ distance } , percent: ${ percent } ` ,
111- } ) ;
112- } ) ;
113-
114- it ( "should not be equal if the tolerance isn't met" , ( ) => {
115- const distance = 0.1 ;
116- const diffImg = { } ;
117- const percent = 0 ;
118- jimpRead . returns ( Promise . resolve ( { } ) ) ;
119- jimpDistance . returns ( distance ) ;
120- jimpDiff . returns ( { image : diffImg , percent } ) ;
121-
122- return expect ( utils . compare ( 'baseline' , 'regression' , 0 ) ) . to . eventually . be . fulfilled . and . deep . equal ( {
123- diffImg,
124- isEqual : false ,
125- equality : `distance: ${ distance } , percent: ${ percent } ` ,
126- } ) ;
127- } ) ;
128-
129- it ( 'should reject with error' , ( ) => {
130- jimpRead . returns ( Promise . reject ( new Error ( 'error' ) ) ) ;
131- expect ( utils . compare ( 'baseline' , 'regression' , 0 ) ) . to . eventually . be . rejected . and . have . property ( 'message' , 'error' ) ;
132- } ) ;
133- } ) ;
134-
13541 describe ( 'getBoundingClientRect' , ( ) => {
13642 let querySelector ;
13743 let getBoundingClientRect ;
@@ -221,88 +127,4 @@ describe('Screenshoter Utils', () => {
221127 expect ( result . browserName ) . to . equal ( 'chrome' ) ;
222128 } ) ) ;
223129 } ) ;
224-
225- describe ( 'matchImageOf' , ( ) => {
226- let chaiCtx ;
227- let matchImageOf ;
228- let fileExists ;
229- let writeImage ;
230- let compare ;
231- let mkdir ;
232- const baselinePath = 'artifacts/baseline/' ;
233- const baseline = `${ baselinePath } id-windows-nt-chrome.png` ;
234- const regressionPath = 'artifacts/regression/' ;
235- const regression = `${ regressionPath } id-windows-nt-chrome.png` ;
236- const diffPath = 'artifacts/diff/' ;
237- const diff = `${ diffPath } id-windows-nt-chrome.png` ;
238- const img = { } ;
239-
240- beforeEach ( ( ) => {
241- sandbox . stub ( path , 'resolve' ) . callsFake ( ( ...args ) => args . join ( '/' ) . replace ( '//' , '/' ) ) ;
242- fileExists = sandbox . stub ( utils , 'fileExists' ) ;
243- writeImage = sandbox . stub ( utils , 'writeImage' ) ;
244- compare = sandbox . stub ( utils , 'compare' ) ;
245- chaiCtx = {
246- _obj : Promise . resolve ( {
247- img, browserName : 'chrome' , artifactsPath : 'artifacts' , platform : 'windows-nt' ,
248- } ) ,
249- assert : sinon . stub ( ) ,
250- } ;
251- matchImageOf = utils . matchImageOf . bind ( chaiCtx ) ;
252- sandbox . stub ( process , 'cwd' ) . returns ( 'foo' ) ;
253- mkdir = sandbox . stub ( mkdirp , 'sync' ) ;
254- } ) ;
255-
256- it ( "should write baseline if it's not existing" , ( ) => {
257- fileExists . returns ( Promise . resolve ( false ) ) ;
258- writeImage . returns ( Promise . resolve ( ) ) ;
259- return matchImageOf ( 'id' ) . then ( ( ) => {
260- expect ( writeImage ) . to . have . been . calledWith ( { } , baseline ) ;
261- } ) ;
262- } ) ;
263-
264- it ( 'should compare and resolve if considered equal to baseline' , ( ) => {
265- fileExists . returns ( Promise . resolve ( true ) ) ;
266- writeImage . returns ( Promise . resolve ( ) ) ;
267- compare . returns ( Promise . resolve ( { equality : 0 , isEqual : true } ) ) ;
268- return matchImageOf ( 'id' ) . then ( ( comparison ) => {
269- expect ( comparison ) . to . deep . equal ( { equality : 0 , isEqual : true } ) ;
270- } ) ;
271- } ) ;
272-
273- it ( 'should reject if not considered equal to baseline' , ( ) => {
274- const diffImg = { } ;
275- fileExists . returns ( Promise . resolve ( true ) ) ;
276- writeImage . returns ( Promise . resolve ( ) ) ;
277- compare . returns ( Promise . resolve ( { isEqual : false , diffImg } ) ) ;
278- return matchImageOf ( 'id' ) . then ( ( ) => {
279- expect ( writeImage . callCount ) . to . equal ( 2 ) ;
280- expect ( writeImage . firstCall ) . to . have . been . calledWithExactly ( img , regression ) ;
281- expect ( writeImage . secondCall ) . to . have . been . calledWithExactly ( diffImg , diff ) ;
282- } ) ;
283- } ) ;
284-
285- it ( 'should create the default artifacts folders' , ( ) => {
286- fileExists . returns ( Promise . resolve ( false ) ) ;
287- writeImage . returns ( Promise . resolve ( ) ) ;
288- return matchImageOf ( 'id' ) . then ( ( ) => {
289- expect ( mkdir . callCount ) . to . equal ( 3 ) ;
290- expect ( mkdir . firstCall ) . to . have . been . calledWithExactly ( baselinePath ) ;
291- expect ( mkdir . secondCall ) . to . have . been . calledWithExactly ( regressionPath ) ;
292- expect ( mkdir . thirdCall ) . to . have . been . calledWithExactly ( diffPath ) ;
293- } ) ;
294- } ) ;
295-
296- it ( 'should create the artifacts folders with subfolders' , ( ) => {
297- const folders = 'foo/bar' ;
298- fileExists . returns ( Promise . resolve ( false ) ) ;
299- writeImage . returns ( Promise . resolve ( ) ) ;
300- return matchImageOf ( 'id' , folders ) . then ( ( ) => {
301- expect ( mkdir . callCount ) . to . equal ( 3 ) ;
302- expect ( mkdir . firstCall ) . to . have . been . calledWithExactly ( baselinePath + folders ) ;
303- expect ( mkdir . secondCall ) . to . have . been . calledWithExactly ( regressionPath + folders ) ;
304- expect ( mkdir . thirdCall ) . to . have . been . calledWithExactly ( diffPath + folders ) ;
305- } ) ;
306- } ) ;
307- } ) ;
308130} ) ;
0 commit comments