11import fs from 'fs' ;
22import Chromy from 'chromy' ;
3+ import getPort from 'get-port' ;
34import Differencify from './index' ;
45import logger from './logger' ;
6+ import run from './chromyRunner' ;
57
6- let chromyCloseCallsCounter = 0 ;
7- jest . mock ( 'chromy' , ( ) => jest . fn ( ( ) =>
8- ( {
9- goto : jest . fn ( ) ,
10- close : jest . fn ( ( ) => { chromyCloseCallsCounter += 1 ; } ) ,
11- screenshotDocument : jest . fn ( ( ) => 'png file' ) ,
12- screenshotSelector : jest . fn ( ( ) => 'png file' ) ,
13- } ) ,
14- ) ) ;
8+ jest . mock ( 'get-port' , ( ) => jest . fn ( ( ) => 3000 ) ) ;
9+ const mockClose = jest . fn ( ) ;
10+ jest . mock ( 'chromy' , ( ) => ( ) =>
11+ ( {
12+ close : mockClose ,
13+ options : { port : 3000 } ,
14+ } ) ) ;
15+ jest . mock ( './chromyRunner' , ( ) => jest . fn ( ( ) => true ) ) ;
1516
16- jest . mock ( './compareImage' , ( ) => jest . fn ( arg =>
17- new Promise ( ( resolve , reject ) => {
18- if ( arg . screenshots === 'screenshots' ) {
19- return resolve ( 'Saving the diff image to disk' ) ;
20- }
21- return reject ( 'error' ) ;
22- } ) ,
23- ) ) ;
17+ jest . mock ( 'fs' , ( ) => ( {
18+ mkdirSync : jest . fn ( ) ,
19+ existsSync : jest . fn ( ) ,
20+ } ) ) ;
2421
25- let writeFileSyncCalls = [ ] ;
26- fs . writeFileSync = ( ...args ) => {
27- writeFileSyncCalls . push ( ...args ) ;
28- } ;
29- fs . mkdirSync = ( ...args ) => {
30- writeFileSyncCalls . push ( ...args ) ;
31- } ;
32-
33- let loggerCalls = [ ] ;
34- logger . prefix = ( ) => logger ;
35- logger . log = ( ...args ) => {
36- loggerCalls . push ( ...args ) ;
37- } ;
22+ const mockLog = jest . fn ( ) ;
23+ jest . mock ( './logger' , ( ) => ( {
24+ prefix : jest . fn ( ( ) => ( {
25+ log : mockLog ,
26+ } ) ) ,
27+ log : jest . fn ( ) ,
28+ error : jest . fn ( ) ,
29+ enable : jest . fn ( ) ,
30+ } ) ) ;
3831
3932const globalConfig = {
4033 screenshots : 'screenshots' ,
@@ -59,35 +52,53 @@ const differencify = new Differencify(globalConfig);
5952
6053describe ( 'Differencify' , ( ) => {
6154 afterEach ( ( ) => {
62- loggerCalls = [ ] ;
63- writeFileSyncCalls = [ ] ;
64- chromyCloseCallsCounter = 0 ;
55+ mockLog . mockClear ( ) ;
56+ logger . log . mockClear ( ) ;
57+ run . mockClear ( ) ;
58+ mockClose . mockClear ( ) ;
59+ } ) ;
60+ it ( 'constructor fn' , async ( ) => {
61+ expect ( fs . mkdirSync ) . toHaveBeenCalledWith ( 'screenshots' ) ;
62+ expect ( fs . mkdirSync ) . toHaveBeenCalledWith ( './differencify_report' ) ;
6563 } ) ;
6664 it ( 'update fn' , async ( ) => {
6765 const result = await differencify . update ( testConfig ) ;
6866 expect ( result ) . toEqual ( true ) ;
69- expect ( differencify . chromeInstancesId ) . toEqual ( 9223 ) ;
70- expect ( loggerCalls [ 0 ] ) . toEqual ( 'goto -> www.example.com' ) ;
71- expect ( loggerCalls [ 1 ] ) . toEqual ( 'capturing screenshot of whole DOM' ) ;
72- expect ( loggerCalls [ 2 ] ) . toEqual ( 'screenshot saved in -> screenshots/default.png' ) ;
73- expect ( writeFileSyncCalls ) . toEqual ( [ 'screenshots' , './differencify_report' , 'screenshots/default.png' , 'png file' ] ) ;
67+ expect ( differencify . chromeInstances [ 3000 ] ) . toEqual ( undefined ) ;
68+ expect ( mockClose ) . toHaveBeenCalledTimes ( 1 ) ;
69+ expect ( mockLog ) . toHaveBeenCalledWith ( 'closing browser' ) ;
70+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
7471 } ) ;
7572 it ( 'test fn' , async ( ) => {
7673 const result = await differencify . test ( testConfig ) ;
7774 expect ( result ) . toEqual ( true ) ;
78- expect ( differencify . chromeInstancesId ) . toEqual ( 9224 ) ;
79- expect ( loggerCalls [ 0 ] ) . toEqual ( 'goto -> www.example.com' ) ;
80- expect ( loggerCalls [ 1 ] ) . toEqual ( 'capturing screenshot of whole DOM' ) ;
81- expect ( loggerCalls [ 2 ] ) . toEqual ( 'screenshot saved in -> ./differencify_report/default.png' ) ;
82- expect ( writeFileSyncCalls ) . toEqual ( [ './differencify_report/default.png' , 'png file' ] ) ;
75+ expect ( differencify . chromeInstances [ 3000 ] ) . toEqual ( undefined ) ;
76+ expect ( mockClose ) . toHaveBeenCalledTimes ( 1 ) ;
77+ expect ( mockLog ) . toHaveBeenCalledWith ( 'closing browser' ) ;
78+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
79+ } ) ;
80+ it ( 'chromyRunner will fail test fn' , async ( ) => {
81+ run . mockReturnValueOnce ( Promise . resolve ( false ) ) ;
82+ const result = await differencify . test ( testConfig ) ;
83+ expect ( result ) . toEqual ( false ) ;
84+ expect ( differencify . chromeInstances [ 3000 ] ) . toEqual ( undefined ) ;
85+ expect ( mockClose ) . toHaveBeenCalledTimes ( 1 ) ;
86+ expect ( mockLog ) . toHaveBeenCalledWith ( 'closing browser' ) ;
87+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
8388 } ) ;
8489 it ( 'cleanup fn' , async ( ) => {
8590 const chromy1 = new Chromy ( ) ;
86- differencify . chromeInstances [ 1 ] = chromy1 ;
8791 const chromy2 = new Chromy ( ) ;
88- differencify . chromeInstances [ 2 ] = chromy2 ;
92+ differencify . chromeInstances = [ chromy1 , chromy2 ] ;
8993 await differencify . cleanup ( ) ;
90- expect ( chromyCloseCallsCounter ) . toEqual ( 2 ) ;
91- expect ( loggerCalls [ 0 ] ) . toEqual ( 'All browsers been closed' ) ;
94+ expect ( mockClose ) . toHaveBeenCalledTimes ( 2 ) ;
95+ expect ( differencify . chromeInstances ) . toEqual ( { } ) ;
96+ expect ( logger . log ) . toHaveBeenCalledWith ( 'All browsers been closed' ) ;
97+ } ) ;
98+ it ( 'get-port fails test' , async ( ) => {
99+ getPort . mockReturnValueOnce ( Promise . reject ( ) ) ;
100+ const result = await differencify . test ( testConfig ) ;
101+ expect ( result ) . toEqual ( false ) ;
102+ expect ( logger . error ) . toHaveBeenCalledWith ( 'Failed to get a free port' , undefined ) ;
92103 } ) ;
93104} ) ;
0 commit comments