@@ -4,215 +4,42 @@ import { configure, mount } from 'enzyme';
44import Adapter from 'enzyme-adapter-react-16' ;
55
66import LazyLoadComponent from './LazyLoadComponent.jsx' ;
7+ import LazyLoadComponentWithTracking
8+ from './LazyLoadComponentWithTracking.jsx' ;
9+ import LazyLoadComponentWithoutTracking
10+ from './LazyLoadComponentWithoutTracking.jsx' ;
711
812configure ( { adapter : new Adapter ( ) } ) ;
913
1014const {
11- scryRenderedDOMComponentsWithTag
15+ scryRenderedComponentsWithType
1216} = ReactTestUtils ;
1317
1418describe ( 'LazyLoadComponent' , function ( ) {
15- function renderLazyLoadComponent ( {
16- afterLoad = ( ) => null ,
17- beforeLoad = ( ) => null ,
18- placeholder = null ,
19- scrollPosition = { x : 0 , y : 0 } ,
20- style = { } ,
21- visibleByDefault = false
22- } = { } ) {
23- return mount (
24- < LazyLoadComponent
25- afterLoad = { afterLoad }
26- beforeLoad = { beforeLoad }
27- placeholder = { placeholder }
28- scrollPosition = { scrollPosition }
29- style = { style }
30- visibleByDefault = { visibleByDefault } >
31- < p > Lorem ipsum</ p >
19+ it ( 'renders a LazyLoadComponentWithTracking when scrollPosition is undefined' , function ( ) {
20+ const lazyLoadComponent = mount (
21+ < LazyLoadComponent >
22+ < p > Lorem Ipsum</ p >
3223 </ LazyLoadComponent >
3324 ) ;
34- }
35-
36- function simulateScroll ( lazyLoadComponent , offsetX = 0 , offsetY = 0 ) {
37- const myMock = jest . fn ( ) ;
38-
39- myMock . mockReturnValue ( {
40- bottom : - offsetY ,
41- height : 0 ,
42- left : - offsetX ,
43- right : - offsetX ,
44- top : - offsetY ,
45- width : 0
46- } ) ;
47-
48- lazyLoadComponent . instance ( ) . placeholder . getBoundingClientRect = myMock ;
49-
50- lazyLoadComponent . setProps ( {
51- scrollPosition : { x : offsetX , y : offsetY }
52- } ) ;
53- }
54-
55- function expectParagraphs ( wrapper , numberOfParagraphs ) {
56- const p = scryRenderedDOMComponentsWithTag ( wrapper . instance ( ) , 'p' ) ;
57-
58- expect ( p . length ) . toEqual ( numberOfParagraphs ) ;
59- }
6025
61- function expectPlaceholders ( wrapper , numberOfPlaceholders , placeholderTag = 'span' ) {
62- const placeholder = scryRenderedDOMComponentsWithTag ( wrapper . instance ( ) , placeholderTag ) ;
26+ const lazyLoadComponentWithTracking = scryRenderedComponentsWithType (
27+ lazyLoadComponent . instance ( ) , LazyLoadComponentWithTracking ) ;
6328
64- expect ( placeholder . length ) . toEqual ( numberOfPlaceholders ) ;
65- }
66-
67- it ( 'renders the default placeholder when it\'s not in the viewport' , function ( ) {
68- const lazyLoadComponent = renderLazyLoadComponent ( {
69- style : { marginTop : 100000 }
70- } ) ;
71-
72- expectParagraphs ( lazyLoadComponent , 0 ) ;
73- expectPlaceholders ( lazyLoadComponent , 1 ) ;
29+ expect ( lazyLoadComponentWithTracking . length ) . toEqual ( 1 ) ;
7430 } ) ;
7531
76- it ( 'renders the prop placeholder when it\'s not in the viewport' , function ( ) {
77- const style = { marginTop : 100000 } ;
78- const placeholder = (
79- < strong style = { style } > </ strong >
32+ it ( 'renders a LazyLoadComponentWithoutTracking when scrollPosition is defined' , function ( ) {
33+ const lazyLoadComponent = mount (
34+ < LazyLoadComponent
35+ scrollPosition = { { x : 0 , y : 0 } } >
36+ < p > Lorem Ipsum</ p >
37+ </ LazyLoadComponent >
8038 ) ;
81- const lazyLoadComponent = renderLazyLoadComponent ( {
82- placeholder,
83- style
84- } ) ;
85-
86- expectParagraphs ( lazyLoadComponent , 0 ) ;
87- expectPlaceholders ( lazyLoadComponent , 1 , 'strong' ) ;
88- } ) ;
89-
90- it ( 'renders the image when it\'s in the viewport' , function ( ) {
91- const lazyLoadComponent = renderLazyLoadComponent ( ) ;
92-
93- expectParagraphs ( lazyLoadComponent , 1 ) ;
94- expectPlaceholders ( lazyLoadComponent , 0 ) ;
95- } ) ;
96-
97- it ( 'renders the image when it appears in the viewport' , function ( ) {
98- const offset = 100000 ;
99- const lazyLoadComponent = renderLazyLoadComponent ( {
100- style : { marginTop : offset }
101- } ) ;
102-
103- simulateScroll ( lazyLoadComponent , 0 , offset ) ;
104-
105- expectParagraphs ( lazyLoadComponent , 1 ) ;
106- expectPlaceholders ( lazyLoadComponent , 0 ) ;
107- } ) ;
108-
109- it ( 'renders the image when it appears in the viewport horizontally' , function ( ) {
110- const offset = 100000 ;
111- const lazyLoadComponent = renderLazyLoadComponent ( {
112- style : { marginLeft : offset }
113- } ) ;
114-
115- simulateScroll ( lazyLoadComponent , offset , 0 ) ;
116-
117- expectParagraphs ( lazyLoadComponent , 1 ) ;
118- expectPlaceholders ( lazyLoadComponent , 0 ) ;
119- } ) ;
120-
121- it ( 'renders the image when it\'s not in the viewport but visibleByDefault is true' , function ( ) {
122- const lazyLoadComponent = renderLazyLoadComponent ( {
123- style : { marginTop : 100000 } ,
124- visibleByDefault : true
125- } ) ;
126-
127- expectParagraphs ( lazyLoadComponent , 1 ) ;
128- expectPlaceholders ( lazyLoadComponent , 0 ) ;
129- } ) ;
130-
131- it ( 'doesn\'t trigger beforeLoad when the image is not the viewport' , function ( ) {
132- const beforeLoad = jest . fn ( ) ;
133- const lazyLoadComponent = renderLazyLoadComponent ( {
134- beforeLoad,
135- style : { marginTop : 100000 }
136- } ) ;
137-
138- expect ( beforeLoad ) . toHaveBeenCalledTimes ( 0 ) ;
139- } ) ;
140-
141- it ( 'triggers beforeLoad when the image is in the viewport' , function ( ) {
142- const beforeLoad = jest . fn ( ) ;
143- const lazyLoadComponent = renderLazyLoadComponent ( {
144- beforeLoad
145- } ) ;
146-
147- expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
148- } ) ;
149-
150- it ( 'triggers beforeLoad when the image appears in the viewport' , function ( ) {
151- const beforeLoad = jest . fn ( ) ;
152- const offset = 100000 ;
153- const lazyLoadComponent = renderLazyLoadComponent ( {
154- beforeLoad,
155- style : { marginTop : offset }
156- } ) ;
157-
158- simulateScroll ( lazyLoadComponent , 0 , offset ) ;
159-
160- expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
161- } ) ;
162-
163- it ( 'triggers beforeLoad when visibleByDefault is true' , function ( ) {
164- const beforeLoad = jest . fn ( ) ;
165- const offset = 100000 ;
166- const lazyLoadComponent = renderLazyLoadComponent ( {
167- beforeLoad,
168- style : { marginTop : offset } ,
169- visibleByDefault : true
170- } ) ;
171-
172- expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
173- } ) ;
174-
175- it ( 'doesn\'t trigger afterLoad when the image is not the viewport' , function ( ) {
176- const afterLoad = jest . fn ( ) ;
177- const lazyLoadComponent = renderLazyLoadComponent ( {
178- afterLoad,
179- style : { marginTop : 100000 }
180- } ) ;
181-
182- expect ( afterLoad ) . toHaveBeenCalledTimes ( 0 ) ;
183- } ) ;
184-
185- it ( 'triggers afterLoad when the image is in the viewport' , function ( ) {
186- const afterLoad = jest . fn ( ) ;
187- const lazyLoadComponent = renderLazyLoadComponent ( {
188- afterLoad
189- } ) ;
190-
191- expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
192- } ) ;
193-
194- it ( 'triggers afterLoad when the image appears in the viewport' , function ( ) {
195- const afterLoad = jest . fn ( ) ;
196- const offset = 100000 ;
197- const lazyLoadComponent = renderLazyLoadComponent ( {
198- afterLoad,
199- style : { marginTop : offset }
200- } ) ;
201-
202- simulateScroll ( lazyLoadComponent , 0 , offset ) ;
203-
204- expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
205- } ) ;
20639
207- it ( 'triggers afterLoad when visibleByDefault is true' , function ( ) {
208- const afterLoad = jest . fn ( ) ;
209- const offset = 100000 ;
210- const lazyLoadComponent = renderLazyLoadComponent ( {
211- afterLoad,
212- style : { marginTop : offset } ,
213- visibleByDefault : true
214- } ) ;
40+ const lazyLoadComponentWithoutTracking = scryRenderedComponentsWithType (
41+ lazyLoadComponent . instance ( ) , LazyLoadComponentWithoutTracking ) ;
21542
216- expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
43+ expect ( lazyLoadComponentWithoutTracking . length ) . toEqual ( 1 ) ;
21744 } ) ;
21845} ) ;
0 commit comments