Skip to content

Commit ecb4132

Browse files
committed
Add unit tests
1 parent 364ce21 commit ecb4132

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/components/LazyLoadComponent.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import Adapter from 'enzyme-adapter-react-16';
77
import LazyLoadComponent from './LazyLoadComponent.jsx';
88
import PlaceholderWithTracking from './PlaceholderWithTracking.jsx';
99
import PlaceholderWithoutTracking from './PlaceholderWithoutTracking.jsx';
10+
import isIntersectionObserverAvailable from '../utils/intersection-observer';
11+
12+
jest.mock('../utils/intersection-observer');
1013

1114
configure({ adapter: new Adapter() });
1215

@@ -16,6 +19,16 @@ const {
1619
} = ReactTestUtils;
1720

1821
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+
1932
it('renders a PlaceholderWithTracking when scrollPosition is undefined', function() {
2033
const lazyLoadComponent = mount(
2134
<LazyLoadComponent
@@ -30,6 +43,28 @@ describe('LazyLoadComponent', function() {
3043
expect(placeholderWithTracking.length).toEqual(1);
3144
});
3245

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+
3368
it('renders a PlaceholderWithoutTracking when scrollPosition is defined', function() {
3469
const lazyLoadComponent = mount(
3570
<LazyLoadComponent
@@ -39,9 +74,12 @@ describe('LazyLoadComponent', function() {
3974
</LazyLoadComponent>
4075
);
4176

77+
const placeholderWithTracking = scryRenderedComponentsWithType(
78+
lazyLoadComponent.instance(), PlaceholderWithTracking);
4279
const placeholderWithoutTracking = scryRenderedComponentsWithType(
4380
lazyLoadComponent.instance(), PlaceholderWithoutTracking);
4481

82+
expect(placeholderWithTracking.length).toEqual(0);
4583
expect(placeholderWithoutTracking.length).toEqual(1);
4684
});
4785

src/components/PlaceholderWithoutTracking.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class PlaceholderWithoutTracking extends React.Component {
1616
const { threshold } = props;
1717

1818
this.LAZY_LOAD_OBSERVER.observer = new IntersectionObserver(
19-
this.checkIntersections, { rootMargin: threshold + 'px' });
19+
this.checkIntersections, { rootMargin: threshold + 'px' }
20+
);
2021
}
2122
}
2223
}
@@ -120,7 +121,7 @@ PlaceholderWithoutTracking.propTypes = {
120121
scrollPosition: PropTypes.shape({
121122
x: PropTypes.number.isRequired,
122123
y: PropTypes.number.isRequired,
123-
}).isRequired,
124+
}),
124125
className: PropTypes.string,
125126
height: PropTypes.number,
126127
placeholder: PropTypes.element,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import isIntersectionObserverAvailable from './intersection-observer';
2+
3+
describe('isIntersectionObserverAvailable', function() {
4+
it('returns true if IntersectionObserver is available', function() {
5+
window.IntersectionObserver = {};
6+
window.IntersectionObserverEntry = {
7+
prototype: {
8+
isIntersecting: () => null,
9+
},
10+
};
11+
12+
expect(isIntersectionObserverAvailable()).toBe(true);
13+
});
14+
15+
it('returns false if IntersectionObserver is not available', function() {
16+
delete window.IntersectionObserver;
17+
delete window.IntersectionObserverEntry;
18+
19+
expect(isIntersectionObserverAvailable()).toBe(false);
20+
});
21+
});

0 commit comments

Comments
 (0)