@@ -10,13 +10,14 @@ configure({ adapter: new Adapter() });
1010
1111const {
1212 findRenderedComponentWithType,
13- findRenderedDOMComponentWithTag
13+ findRenderedDOMComponentWithTag,
14+ scryRenderedDOMComponentsWithTag,
15+ Simulate
1416} = ReactTestUtils ;
1517
1618describe ( 'LazyLoadImage' , function ( ) {
1719 it ( 'renders a LazyLoadComponent with the correct props' , function ( ) {
1820 const props = {
19- afterLoad : ( ) => null ,
2021 beforeLoad : ( ) => null ,
2122 delayMethod : 'debounce' ,
2223 delayTime : 600 ,
@@ -28,7 +29,6 @@ describe('LazyLoadImage', function() {
2829 } ;
2930 const lazyLoadImage = mount (
3031 < LazyLoadImage
31- afterLoad = { props . afterLoad }
3232 beforeLoad = { props . beforeLoad }
3333 delayMethod = { props . delayMethod }
3434 delayTime = { props . delayTime }
@@ -42,7 +42,6 @@ describe('LazyLoadImage', function() {
4242 const lazyLoadComponent = findRenderedComponentWithType ( lazyLoadImage . instance ( ) , LazyLoadComponent ) ;
4343 const img = findRenderedDOMComponentWithTag ( lazyLoadImage . instance ( ) , 'img' ) ;
4444
45- expect ( lazyLoadComponent . props . afterLoad ) . toEqual ( props . afterLoad ) ;
4645 expect ( lazyLoadComponent . props . beforeLoad ) . toEqual ( props . beforeLoad ) ;
4746 expect ( lazyLoadComponent . props . delayMethod ) . toEqual ( props . delayMethod ) ;
4847 expect ( lazyLoadComponent . props . delayTime ) . toEqual ( props . delayTime ) ;
@@ -52,4 +51,52 @@ describe('LazyLoadImage', function() {
5251 expect ( lazyLoadComponent . props . visibleByDefault ) . toEqual ( props . visibleByDefault ) ;
5352 expect ( img . src ) . toEqual ( props . src ) ;
5453 } ) ;
54+
55+ it ( 'calls afterLoad when img triggers onLoad' , function ( ) {
56+ const afterLoad = jest . fn ( ) ;
57+ const lazyLoadImage = mount (
58+ < LazyLoadImage
59+ afterLoad = { afterLoad } />
60+ ) ;
61+
62+ const img = findRenderedDOMComponentWithTag ( lazyLoadImage . instance ( ) , 'img' ) ;
63+
64+ Simulate . load ( img ) ;
65+
66+ expect ( afterLoad ) . toHaveBeenCalledTimes ( 1 ) ;
67+ } ) ;
68+
69+ it ( 'doesn\'t render placeholder background when not defined' , function ( ) {
70+ const lazyLoadImage = mount (
71+ < LazyLoadImage />
72+ ) ;
73+
74+ const span = scryRenderedDOMComponentsWithTag ( lazyLoadImage . instance ( ) , 'span' ) ;
75+
76+ expect ( span . length ) . toEqual ( 0 ) ;
77+ } ) ;
78+
79+ it ( 'renders placeholder background when defined' , function ( ) {
80+ const lazyLoadImage = mount (
81+ < LazyLoadImage
82+ placeholderSrc = 'lorem-ipsum.jpg'
83+ visibleByDefault = { false } />
84+ ) ;
85+
86+ const span = scryRenderedDOMComponentsWithTag ( lazyLoadImage . instance ( ) , 'span' ) ;
87+
88+ expect ( span . length ) . toEqual ( 1 ) ;
89+ } ) ;
90+
91+ it ( 'doesn\'t render placeholder background when visibleByDefault is true' , function ( ) {
92+ const lazyLoadImage = mount (
93+ < LazyLoadImage
94+ placeholderSrc = 'lorem-ipsum.jpg'
95+ visibleByDefault = { true } />
96+ ) ;
97+
98+ const span = scryRenderedDOMComponentsWithTag ( lazyLoadImage . instance ( ) , 'span' ) ;
99+
100+ expect ( span . length ) . toEqual ( 0 ) ;
101+ } ) ;
55102} ) ;
0 commit comments