@@ -14,18 +14,39 @@ describe('LazyLoadImage', function() {
1414 afterLoad = ( ) => null ,
1515 beforeLoad = ( ) => null ,
1616 placeholder = null ,
17- scrollPosition = { x : 0 , y : 0 }
17+ scrollPosition = { x : 0 , y : 0 } ,
18+ style = { }
1819 } = { } ) {
1920 return ReactTestUtils . renderIntoDocument (
2021 < LazyLoadImage
2122 afterLoad = { afterLoad }
2223 beforeLoad = { beforeLoad }
2324 placeholder = { placeholder }
2425 scrollPosition = { scrollPosition }
25- src = "" />
26+ src = ""
27+ style = { style } />
2628 ) ;
2729 }
2830
31+ function simulateScroll ( lazyLoadImage , offsetX = 0 , offsetY = 0 ) {
32+ const myMock = jest . fn ( ) ;
33+
34+ myMock . mockReturnValue ( {
35+ bottom : - offsetY ,
36+ height : 0 ,
37+ left : - offsetX ,
38+ right : - offsetX ,
39+ top : - offsetY ,
40+ width : 0
41+ } ) ;
42+
43+ lazyLoadImage . placeholder . getBoundingClientRect = myMock ;
44+
45+ lazyLoadImage . componentWillReceiveProps ( {
46+ scrollPosition : { x : offsetX , y : offsetY }
47+ } ) ;
48+ }
49+
2950 function expectImages ( wrapper , numberOfImages ) {
3051 const img = scryRenderedDOMComponentsWithTag ( wrapper , 'img' ) ;
3152
@@ -40,18 +61,21 @@ describe('LazyLoadImage', function() {
4061
4162 it ( 'renders the default placeholder when it\'s not in the viewport' , function ( ) {
4263 const lazyLoadImage = renderLazyLoadImage ( {
43- scrollPosition : { x : 0 , y : - 1000 }
64+ style : { marginTop : 100000 }
4465 } ) ;
4566
4667 expectImages ( lazyLoadImage , 0 ) ;
4768 expectPlaceholders ( lazyLoadImage , 1 ) ;
4869 } ) ;
4970
5071 it ( 'renders the prop placeholder when it\'s not in the viewport' , function ( ) {
51- const placeholder = < span className = "test-placeholder" > </ span > ;
72+ const style = { marginTop : 100000 } ;
73+ const placeholder = (
74+ < span className = "test-placeholder" style = { style } > </ span >
75+ ) ;
5276 const lazyLoadImage = renderLazyLoadImage ( {
53- scrollPosition : { x : 0 , y : - 1000 } ,
54- placeholder
77+ placeholder ,
78+ style
5579 } ) ;
5680
5781 expectImages ( lazyLoadImage , 0 ) ;
@@ -66,22 +90,24 @@ describe('LazyLoadImage', function() {
6690 } ) ;
6791
6892 it ( 'renders the image when it appears in the viewport' , function ( ) {
93+ const offset = 100000 ;
6994 const lazyLoadImage = renderLazyLoadImage ( {
70- scrollPosition : { x : 0 , y : - 1000 }
95+ style : { marginTop : offset }
7196 } ) ;
7297
73- lazyLoadImage . componentWillReceiveProps ( { scrollPosition : { x : 0 , y : 0 } } ) ;
98+ simulateScroll ( lazyLoadImage , 0 , offset ) ;
7499
75100 expectImages ( lazyLoadImage , 1 ) ;
76101 expectPlaceholders ( lazyLoadImage , 0 ) ;
77102 } ) ;
78103
79104 it ( 'renders the image when it appears in the viewport horizontally' , function ( ) {
105+ const offset = 100000 ;
80106 const lazyLoadImage = renderLazyLoadImage ( {
81- scrollPosition : { x : - 1000 , y : 0 }
107+ style : { marginLeft : offset }
82108 } ) ;
83109
84- lazyLoadImage . componentWillReceiveProps ( { scrollPosition : { x : 0 , y : 0 } } ) ;
110+ simulateScroll ( lazyLoadImage , offset , 0 ) ;
85111
86112 expectImages ( lazyLoadImage , 1 ) ;
87113 expectPlaceholders ( lazyLoadImage , 0 ) ;
@@ -91,10 +117,10 @@ describe('LazyLoadImage', function() {
91117 const beforeLoad = jest . fn ( ) ;
92118 const lazyLoadImage = renderLazyLoadImage ( {
93119 beforeLoad,
94- scrollPosition : { x : 0 , y : - 1000 }
120+ style : { marginTop : 100000 }
95121 } ) ;
96122
97- expect ( beforeLoad ) . toHaveBeenCalledTimes ( 0 ) ;
123+ expect ( beforeLoad ) . toHaveBeenCalledTimes ( 0 ) ;
98124 } ) ;
99125
100126 it ( 'triggers beforeLoad when the image is in the viewport' , function ( ) {
@@ -108,12 +134,13 @@ describe('LazyLoadImage', function() {
108134
109135 it ( 'triggers beforeLoad when the image appears in the viewport' , function ( ) {
110136 const beforeLoad = jest . fn ( ) ;
137+ const offset = 100000 ;
111138 const lazyLoadImage = renderLazyLoadImage ( {
112139 beforeLoad,
113- scrollPosition : { x : 0 , y : - 1000 }
140+ style : { marginTop : offset }
114141 } ) ;
115142
116- lazyLoadImage . componentWillReceiveProps ( { scrollPosition : { x : 0 , y : 0 } } ) ;
143+ simulateScroll ( lazyLoadImage , 0 , offset ) ;
117144
118145 expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
119146 } ) ;
@@ -122,7 +149,7 @@ describe('LazyLoadImage', function() {
122149 const afterLoad = jest . fn ( ) ;
123150 const lazyLoadImage = renderLazyLoadImage ( {
124151 afterLoad,
125- scrollPosition : { x : 0 , y : - 1000 }
152+ style : { marginTop : 100000 }
126153 } ) ;
127154
128155 expect ( afterLoad ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -139,12 +166,13 @@ describe('LazyLoadImage', function() {
139166
140167 it ( 'triggers afterLoad when the image appears in the viewport' , function ( ) {
141168 const afterLoad = jest . fn ( ) ;
169+ const offset = 100000 ;
142170 const lazyLoadImage = renderLazyLoadImage ( {
143171 afterLoad,
144- scrollPosition : { x : 0 , y : - 1000 }
172+ style : { marginTop : offset }
145173 } ) ;
146174
147- lazyLoadImage . componentWillReceiveProps ( { scrollPosition : { x : 0 , y : 0 } } ) ;
175+ simulateScroll ( lazyLoadImage , 0 , offset ) ;
148176
149177 expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
150178 } ) ;
0 commit comments