@@ -16,7 +16,7 @@ const sdkAnalyticsTokens: BaseAnalyticsOptions = {
1616}
1717
1818const flushPromises = async ( ) => {
19- jest . advanceTimersByTime ( 100 ) ;
19+ jest . advanceTimersByTime ( 10 ) ;
2020 await Promise . resolve ( ) ;
2121}
2222
@@ -95,4 +95,36 @@ describe('HtmlImageLayer tests', function () {
9595 await flushPromises ( ) ;
9696 expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
9797 } ) ;
98+
99+ it ( 'should verfiy no unneeded request with placeholder plugin' , async function ( ) {
100+ const OriginalImage = Image ;
101+ // mocking Image constructor in order to simulate firing 'load' event
102+ jest . spyOn ( global , "Image" ) . mockImplementation ( ( ) => {
103+ const img = new OriginalImage ( ) ;
104+ setTimeout ( ( ) => {
105+ img . dispatchEvent ( new Event ( "load" ) ) ;
106+ } , 10 )
107+ return img ;
108+
109+ } )
110+ const img = document . createElement ( 'img' ) ;
111+ const imgSrcSpy = jest . spyOn ( img , 'src' , 'set' ) ;
112+ const imgSetAttributeSpy = jest . spyOn ( img , 'setAttribute' ) ;
113+
114+ new HtmlImageLayer ( img , cldImage , [ placeholder ( ) ] , sdkAnalyticsTokens ) ;
115+ expect ( imgSrcSpy ) . toHaveBeenCalledTimes ( 1 ) ;
116+ // test that the initial src is set to a token contains last character "B" which is the character of placeholder plugin
117+ const imgSrcSpyAnalyticsToken = imgSrcSpy . mock . calls [ 0 ] [ 0 ] ;
118+ expect ( imgSrcSpyAnalyticsToken ) . toEqualAnalyticsToken ( 'AXAABABB' ) ;
119+ // trigger load event in order to resolve the 1st promise of the placeholder plugin
120+ img . dispatchEvent ( new Event ( 'load' ) ) ;
121+ await flushPromises ( ) ;
122+ // resolve the 2nd promise of the placeholder plugin, which cause the placeholder plugin to create the pixelated Image
123+ await flushPromises ( ) ;
124+ // resolve the 3rd promise of the placeholder plugin, which cause HtmlLayer to set the image attribute
125+ await flushPromises ( ) ;
126+ expect ( imgSetAttributeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
127+ // test that the src which set by HtmlImageLayer contains last character "B" which is the character of placeholder plugin
128+ expect ( imgSetAttributeSpy . mock . calls [ 0 ] [ 1 ] ) . toEqualAnalyticsToken ( 'AXAABABB' ) ;
129+ } ) ;
98130} ) ;
0 commit comments