@@ -10,9 +10,16 @@ const {
1010} = ReactTestUtils ;
1111
1212describe ( 'LazyLoadImage' , function ( ) {
13- function renderLazyLoadImage ( scrollPosition = 0 , placeholder = null ) {
13+ function renderLazyLoadImage ( {
14+ afterLoad = ( ) => null ,
15+ beforeLoad = ( ) => null ,
16+ placeholder = null ,
17+ scrollPosition = 0
18+ } = { } ) {
1419 return ReactTestUtils . renderIntoDocument (
1520 < LazyLoadImage
21+ afterLoad = { afterLoad }
22+ beforeLoad = { beforeLoad }
1623 placeholder = { placeholder }
1724 scrollPosition = { scrollPosition }
1825 src = "" />
@@ -32,15 +39,20 @@ describe('LazyLoadImage', function() {
3239 }
3340
3441 it ( 'renders the default placeholder when it\'s not in the viewport' , function ( ) {
35- const lazyLoadImage = renderLazyLoadImage ( - 1000 ) ;
42+ const lazyLoadImage = renderLazyLoadImage ( {
43+ scrollPosition : - 1000
44+ } ) ;
3645
3746 expectImages ( lazyLoadImage , 0 ) ;
3847 expectPlaceholders ( lazyLoadImage , 1 ) ;
3948 } ) ;
4049
4150 it ( 'renders the prop placeholder when it\'s not in the viewport' , function ( ) {
4251 const placeholder = < span className = "test-placeholder" > </ span > ;
43- const lazyLoadImage = renderLazyLoadImage ( - 1000 , placeholder ) ;
52+ const lazyLoadImage = renderLazyLoadImage ( {
53+ scrollPosition : - 1000 ,
54+ placeholder
55+ } ) ;
4456
4557 expectImages ( lazyLoadImage , 0 ) ;
4658 expectPlaceholders ( lazyLoadImage , 1 , 'test-placeholder' ) ;
@@ -54,11 +66,75 @@ describe('LazyLoadImage', function() {
5466 } ) ;
5567
5668 it ( 'renders the image when it appears in the viewport' , function ( ) {
57- const lazyLoadImage = renderLazyLoadImage ( - 1000 ) ;
69+ const lazyLoadImage = renderLazyLoadImage ( {
70+ scrollPosition : - 1000
71+ } ) ;
5872
5973 lazyLoadImage . componentWillReceiveProps ( { scrollPosition : 0 } ) ;
6074
6175 expectImages ( lazyLoadImage , 1 ) ;
6276 expectPlaceholders ( lazyLoadImage , 0 ) ;
6377 } ) ;
78+
79+ it ( 'doesn\'t trigger beforeLoad when the image is not the viewport' , function ( ) {
80+ const beforeLoad = jest . fn ( ) ;
81+ const lazyLoadImage = renderLazyLoadImage ( {
82+ beforeLoad,
83+ scrollPosition : - 1000
84+ } ) ;
85+
86+ expect ( beforeLoad ) . toHaveBeenCalledTimes ( 0 ) ;
87+ } ) ;
88+
89+ it ( 'triggers beforeLoad when the image is in the viewport' , function ( ) {
90+ const beforeLoad = jest . fn ( ) ;
91+ const lazyLoadImage = renderLazyLoadImage ( {
92+ beforeLoad
93+ } ) ;
94+
95+ expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
96+ } ) ;
97+
98+ it ( 'triggers beforeLoad when the image appears in the viewport' , function ( ) {
99+ const beforeLoad = jest . fn ( ) ;
100+ const lazyLoadImage = renderLazyLoadImage ( {
101+ beforeLoad,
102+ scrollPosition : - 1000
103+ } ) ;
104+
105+ lazyLoadImage . componentWillReceiveProps ( { scrollPosition : 0 } ) ;
106+
107+ expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
108+ } ) ;
109+
110+ it ( 'doesn\'t trigger afterLoad when the image is not the viewport' , function ( ) {
111+ const afterLoad = jest . fn ( ) ;
112+ const lazyLoadImage = renderLazyLoadImage ( {
113+ afterLoad,
114+ scrollPosition : - 1000
115+ } ) ;
116+
117+ expect ( afterLoad ) . toHaveBeenCalledTimes ( 0 ) ;
118+ } ) ;
119+
120+ it ( 'triggers afterLoad when the image is in the viewport' , function ( ) {
121+ const afterLoad = jest . fn ( ) ;
122+ const lazyLoadImage = renderLazyLoadImage ( {
123+ afterLoad
124+ } ) ;
125+
126+ expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
127+ } ) ;
128+
129+ it ( 'triggers afterLoad when the image appears in the viewport' , function ( ) {
130+ const afterLoad = jest . fn ( ) ;
131+ const lazyLoadImage = renderLazyLoadImage ( {
132+ afterLoad,
133+ scrollPosition : - 1000
134+ } ) ;
135+
136+ lazyLoadImage . componentWillReceiveProps ( { scrollPosition : 0 } ) ;
137+
138+ expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
139+ } ) ;
64140} ) ;
0 commit comments