@@ -10,13 +10,14 @@ configure({ adapter: new Adapter() });
10
10
11
11
const {
12
12
findRenderedComponentWithType,
13
- findRenderedDOMComponentWithTag
13
+ findRenderedDOMComponentWithTag,
14
+ scryRenderedDOMComponentsWithTag,
15
+ Simulate
14
16
} = ReactTestUtils ;
15
17
16
18
describe ( 'LazyLoadImage' , function ( ) {
17
19
it ( 'renders a LazyLoadComponent with the correct props' , function ( ) {
18
20
const props = {
19
- afterLoad : ( ) => null ,
20
21
beforeLoad : ( ) => null ,
21
22
delayMethod : 'debounce' ,
22
23
delayTime : 600 ,
@@ -28,7 +29,6 @@ describe('LazyLoadImage', function() {
28
29
} ;
29
30
const lazyLoadImage = mount (
30
31
< LazyLoadImage
31
- afterLoad = { props . afterLoad }
32
32
beforeLoad = { props . beforeLoad }
33
33
delayMethod = { props . delayMethod }
34
34
delayTime = { props . delayTime }
@@ -42,7 +42,6 @@ describe('LazyLoadImage', function() {
42
42
const lazyLoadComponent = findRenderedComponentWithType ( lazyLoadImage . instance ( ) , LazyLoadComponent ) ;
43
43
const img = findRenderedDOMComponentWithTag ( lazyLoadImage . instance ( ) , 'img' ) ;
44
44
45
- expect ( lazyLoadComponent . props . afterLoad ) . toEqual ( props . afterLoad ) ;
46
45
expect ( lazyLoadComponent . props . beforeLoad ) . toEqual ( props . beforeLoad ) ;
47
46
expect ( lazyLoadComponent . props . delayMethod ) . toEqual ( props . delayMethod ) ;
48
47
expect ( lazyLoadComponent . props . delayTime ) . toEqual ( props . delayTime ) ;
@@ -52,4 +51,52 @@ describe('LazyLoadImage', function() {
52
51
expect ( lazyLoadComponent . props . visibleByDefault ) . toEqual ( props . visibleByDefault ) ;
53
52
expect ( img . src ) . toEqual ( props . src ) ;
54
53
} ) ;
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
+ } ) ;
55
102
} ) ;
0 commit comments