@@ -7,6 +7,9 @@ import Adapter from 'enzyme-adapter-react-16';
7
7
import LazyLoadComponent from './LazyLoadComponent.jsx' ;
8
8
import PlaceholderWithTracking from './PlaceholderWithTracking.jsx' ;
9
9
import PlaceholderWithoutTracking from './PlaceholderWithoutTracking.jsx' ;
10
+ import isIntersectionObserverAvailable from '../utils/intersection-observer' ;
11
+
12
+ jest . mock ( '../utils/intersection-observer' ) ;
10
13
11
14
configure ( { adapter : new Adapter ( ) } ) ;
12
15
@@ -16,6 +19,16 @@ const {
16
19
} = ReactTestUtils ;
17
20
18
21
describe ( 'LazyLoadComponent' , function ( ) {
22
+ const windowIntersectionObserver = window . IntersectionObserver ;
23
+
24
+ beforeEach ( ( ) => {
25
+ isIntersectionObserverAvailable . mockImplementation ( ( ) => false ) ;
26
+ } ) ;
27
+
28
+ afterEach ( ( ) => {
29
+ window . IntersectionObserver = windowIntersectionObserver ;
30
+ } ) ;
31
+
19
32
it ( 'renders a PlaceholderWithTracking when scrollPosition is undefined' , function ( ) {
20
33
const lazyLoadComponent = mount (
21
34
< LazyLoadComponent
@@ -30,6 +43,28 @@ describe('LazyLoadComponent', function() {
30
43
expect ( placeholderWithTracking . length ) . toEqual ( 1 ) ;
31
44
} ) ;
32
45
46
+ it ( 'renders a PlaceholderWithoutTracking when scrollPosition is undefined but IntersectionObserver is available' , function ( ) {
47
+ isIntersectionObserverAvailable . mockImplementation ( ( ) => true ) ;
48
+ window . IntersectionObserver = jest . fn ( function ( ) {
49
+ this . observe = jest . fn ( ) ; // eslint-disable-line babel/no-invalid-this
50
+ } ) ;
51
+
52
+ const lazyLoadComponent = mount (
53
+ < LazyLoadComponent
54
+ style = { { marginTop : 100000 } } >
55
+ < p > Lorem Ipsum</ p >
56
+ </ LazyLoadComponent >
57
+ ) ;
58
+
59
+ const placeholderWithTracking = scryRenderedComponentsWithType (
60
+ lazyLoadComponent . instance ( ) , PlaceholderWithTracking ) ;
61
+ const placeholderWithoutTracking = scryRenderedComponentsWithType (
62
+ lazyLoadComponent . instance ( ) , PlaceholderWithoutTracking ) ;
63
+
64
+ expect ( placeholderWithTracking . length ) . toEqual ( 0 ) ;
65
+ expect ( placeholderWithoutTracking . length ) . toEqual ( 1 ) ;
66
+ } ) ;
67
+
33
68
it ( 'renders a PlaceholderWithoutTracking when scrollPosition is defined' , function ( ) {
34
69
const lazyLoadComponent = mount (
35
70
< LazyLoadComponent
@@ -39,9 +74,12 @@ describe('LazyLoadComponent', function() {
39
74
</ LazyLoadComponent >
40
75
) ;
41
76
77
+ const placeholderWithTracking = scryRenderedComponentsWithType (
78
+ lazyLoadComponent . instance ( ) , PlaceholderWithTracking ) ;
42
79
const placeholderWithoutTracking = scryRenderedComponentsWithType (
43
80
lazyLoadComponent . instance ( ) , PlaceholderWithoutTracking ) ;
44
81
82
+ expect ( placeholderWithTracking . length ) . toEqual ( 0 ) ;
45
83
expect ( placeholderWithoutTracking . length ) . toEqual ( 1 ) ;
46
84
} ) ;
47
85
0 commit comments