Skip to content

Commit 67f4872

Browse files
committed
Enabling intersection observer options.
1 parent 212f210 commit 67f4872

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

src/__tests__/loadable-components/intersection-observer.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const loadableVisiblity = require('../../loadable-components')
1515
const opts = {
1616
loading: () => null,
1717
loader: () => Promise.resolve(),
18+
observerOptions: { 'a': 'test' }
1819
}
1920

2021
const props = {'a': 1, 'b': 2}
@@ -104,4 +105,22 @@ describe('Loadable', () => {
104105

105106
expect(globallyTrackedElements.length).toEqual(1)
106107
})
108+
109+
110+
test('it should call setIntersectionObserver with observerOptions', () => {
111+
const mock = jest.fn().mockImplementation(() => {
112+
return {
113+
observe: () => {},
114+
}
115+
})
116+
117+
global.IntersectionObserver = mock
118+
119+
const Loader = loadableVisiblity(opts)
120+
121+
mount(<Loader {...props} />)
122+
123+
expect(mock).toHaveBeenCalledWith(expect.anything(), opts.observerOptions)
124+
global.IntersectionObserver = IntersectionObserver
125+
});
107126
})

src/__tests__/react-loadable/intersection-observer.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const LoadableVisibility = require('../../')
1111
const opts = {
1212
loading: () => null,
1313
loader: () => Promise.resolve(),
14+
observerOptions: { 'a': 'test' }
1415
}
1516

1617
const props = {'a': 1, 'b': 2}
@@ -77,6 +78,23 @@ describe('Loadable', () => {
7778
expect(wrapper.find('.loading-class-name')).toHaveLength(0)
7879
expect(wrapper.find('LoadableObject')).toHaveLength(1)
7980
});
81+
82+
test('it should call setIntersectionObserver with observerOptions', () => {
83+
const mock = jest.fn().mockImplementation(() => {
84+
return {
85+
observe: () => {},
86+
}
87+
})
88+
89+
global.IntersectionObserver = mock
90+
91+
const Loader = LoadableVisibility(opts)
92+
93+
mount(<Loader {...props} />)
94+
95+
expect(mock).toHaveBeenCalledWith(expect.anything(), opts.observerOptions)
96+
global.IntersectionObserver = IntersectionObserver
97+
});
8098
})
8199

82100
describe('Loadable.Map', () => {

src/createLoadableVisibilityComponent.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import { IntersectionObserver } from './capacities'
44
let intersectionObserver
55
const trackedElements = new Map()
66

7-
if (IntersectionObserver) {
8-
intersectionObserver = new window.IntersectionObserver((entries, observer) => {
9-
entries.forEach((entry) => {
10-
const trackedElement = trackedElements.get(entry.target)
11-
12-
if (trackedElement && entry.intersectionRatio > 0) {
13-
trackedElement.visibilityHandler()
14-
}
15-
})
16-
})
7+
const setIntersectionObserver = (options = {}) => {
8+
if (IntersectionObserver) {
9+
intersectionObserver = new window.IntersectionObserver((entries, observer) => {
10+
entries.forEach((entry) => {
11+
const trackedElement = trackedElements.get(entry.target)
12+
13+
if (trackedElement && entry.intersectionRatio > 0) {
14+
trackedElement.visibilityHandler()
15+
}
16+
})
17+
}, options)
18+
}
1719
}
1820

1921
function createLoadableVisibilityComponent (args, {
@@ -52,6 +54,7 @@ function createLoadableVisibilityComponent (args, {
5254
if (!preloaded) {
5355
const element = this.loadingRef
5456
trackedElements.set(element, this)
57+
setIntersectionObserver(args[0].observerOptions)
5558
intersectionObserver.observe(element)
5659
}
5760
}

0 commit comments

Comments
 (0)