@@ -4,42 +4,106 @@ 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' ;
7+ import PlaceholderWithTracking from './PlaceholderWithTracking.jsx' ;
8+ import PlaceholderWithoutTracking from './PlaceholderWithoutTracking.jsx' ;
119
1210configure ( { adapter : new Adapter ( ) } ) ;
1311
1412const {
15- scryRenderedComponentsWithType
13+ scryRenderedComponentsWithType,
14+ scryRenderedDOMComponentsWithTag
1615} = ReactTestUtils ;
1716
1817describe ( 'LazyLoadComponent' , function ( ) {
19- it ( 'renders a LazyLoadComponentWithTracking when scrollPosition is undefined' , function ( ) {
18+ it ( 'renders a PlaceholderWithTracking when scrollPosition is undefined' , function ( ) {
19+ const lazyLoadComponent = mount (
20+ < LazyLoadComponent
21+ style = { { marginTop : 100000 } } > >
22+ < p > Lorem Ipsum</ p >
23+ </ LazyLoadComponent >
24+ ) ;
25+
26+ const placeholderWithTracking = scryRenderedComponentsWithType (
27+ lazyLoadComponent . instance ( ) , PlaceholderWithTracking ) ;
28+
29+ expect ( placeholderWithTracking . length ) . toEqual ( 1 ) ;
30+ } ) ;
31+
32+ it ( 'renders a PlaceholderWithoutTracking when scrollPosition is defined' , function ( ) {
33+ const lazyLoadComponent = mount (
34+ < LazyLoadComponent
35+ scrollPosition = { { x : 0 , y : 0 } }
36+ style = { { marginTop : 100000 } } > >
37+ < p > Lorem Ipsum</ p >
38+ </ LazyLoadComponent >
39+ ) ;
40+
41+ const placeholderWithoutTracking = scryRenderedComponentsWithType (
42+ lazyLoadComponent . instance ( ) , PlaceholderWithoutTracking ) ;
43+
44+ expect ( placeholderWithoutTracking . length ) . toEqual ( 1 ) ;
45+ } ) ;
46+
47+ it ( 'renders children when visible' , function ( ) {
2048 const lazyLoadComponent = mount (
2149 < LazyLoadComponent >
2250 < p > Lorem Ipsum</ p >
2351 </ LazyLoadComponent >
2452 ) ;
2553
26- const lazyLoadComponentWithTracking = scryRenderedComponentsWithType (
27- lazyLoadComponent . instance ( ) , LazyLoadComponentWithTracking ) ;
54+ lazyLoadComponent . instance ( ) . onVisible ( ) ;
55+
56+ const paragraphs = scryRenderedDOMComponentsWithTag (
57+ lazyLoadComponent . instance ( ) , 'p' ) ;
58+
59+ expect ( paragraphs . length ) . toEqual ( 1 ) ;
60+ } ) ;
61+
62+ it ( 'triggers beforeLoad when onVisible is triggered' , function ( ) {
63+ const beforeLoad = jest . fn ( ) ;
64+ const lazyLoadComponent = mount (
65+ < LazyLoadComponent
66+ beforeLoad = { beforeLoad }
67+ style = { { marginTop : 100000 } } >
68+ < p > Lorem Ipsum</ p >
69+ </ LazyLoadComponent >
70+ ) ;
71+
72+ lazyLoadComponent . instance ( ) . onVisible ( ) ;
73+
74+ expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
75+ } ) ;
76+
77+ it ( 'triggers afterLoad when onVisible is triggered' , function ( ) {
78+ const afterLoad = jest . fn ( ) ;
79+ const lazyLoadComponent = mount (
80+ < LazyLoadComponent
81+ afterLoad = { afterLoad }
82+ style = { { marginTop : 100000 } } >
83+ < p > Lorem Ipsum</ p >
84+ </ LazyLoadComponent >
85+ ) ;
86+
87+ lazyLoadComponent . instance ( ) . onVisible ( ) ;
2888
29- expect ( lazyLoadComponentWithTracking . length ) . toEqual ( 1 ) ;
89+ expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
3090 } ) ;
3191
32- it ( 'renders a LazyLoadComponentWithoutTracking when scrollPosition is defined' , function ( ) {
92+ it ( 'triggers beforeLoad and afterLoad when visibleByDefault is true' , function ( ) {
93+ const afterLoad = jest . fn ( ) ;
94+ const beforeLoad = jest . fn ( ) ;
3395 const lazyLoadComponent = mount (
3496 < LazyLoadComponent
35- scrollPosition = { { x : 0 , y : 0 } } >
97+ afterLoad = { afterLoad }
98+ beforeLoad = { beforeLoad }
99+ style = { { marginTop : 100000 } } >
36100 < p > Lorem Ipsum</ p >
37101 </ LazyLoadComponent >
38102 ) ;
39103
40- const lazyLoadComponentWithoutTracking = scryRenderedComponentsWithType (
41- lazyLoadComponent . instance ( ) , LazyLoadComponentWithoutTracking ) ;
104+ lazyLoadComponent . instance ( ) . onVisible ( ) ;
42105
43- expect ( lazyLoadComponentWithoutTracking . length ) . toEqual ( 1 ) ;
106+ expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
107+ expect ( beforeLoad ) . toHaveBeenCalledTimes ( 1 ) ;
44108 } ) ;
45109} ) ;
0 commit comments