Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling intersection observer options. #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/__tests__/loadable-components/intersection-observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const loadableVisiblity = require('../../loadable-components')
const opts = {
loading: () => null,
loader: () => Promise.resolve(),
observerOptions: { 'a': 'test' }
}

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

expect(globallyTrackedElements.length).toEqual(1)
})


test('it should call setIntersectionObserver with observerOptions', () => {
const mock = jest.fn().mockImplementation(() => {
return {
observe: () => {},
}
})

global.IntersectionObserver = mock

const Loader = loadableVisiblity(opts)

mount(<Loader {...props} />)

expect(mock).toHaveBeenCalledWith(expect.anything(), opts.observerOptions)
global.IntersectionObserver = IntersectionObserver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this and setting up the mock should be done in a beforeEach and afterEach for this test (perhaps wrap it in a describe), such that the mock is ensured to be cleaned up in the case that this test exits prematurely.

});
})
18 changes: 18 additions & 0 deletions src/__tests__/react-loadable/intersection-observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const LoadableVisibility = require('../../')
const opts = {
loading: () => null,
loader: () => Promise.resolve(),
observerOptions: { 'a': 'test' }
}

const props = {'a': 1, 'b': 2}
Expand Down Expand Up @@ -77,6 +78,23 @@ describe('Loadable', () => {
expect(wrapper.find('.loading-class-name')).toHaveLength(0)
expect(wrapper.find('LoadableObject')).toHaveLength(1)
});

test('it should call setIntersectionObserver with observerOptions', () => {
const mock = jest.fn().mockImplementation(() => {
return {
observe: () => {},
}
})

global.IntersectionObserver = mock

const Loader = LoadableVisibility(opts)

mount(<Loader {...props} />)

expect(mock).toHaveBeenCalledWith(expect.anything(), opts.observerOptions)
global.IntersectionObserver = IntersectionObserver
});
})

describe('Loadable.Map', () => {
Expand Down
23 changes: 13 additions & 10 deletions src/createLoadableVisibilityComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { IntersectionObserver } from './capacities'
let intersectionObserver
const trackedElements = new Map()

if (IntersectionObserver) {
intersectionObserver = new window.IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
const trackedElement = trackedElements.get(entry.target)

if (trackedElement && entry.intersectionRatio > 0) {
trackedElement.visibilityHandler()
}
})
})
const setIntersectionObserver = (options = {}) => {
if (IntersectionObserver) {
intersectionObserver = new window.IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
const trackedElement = trackedElements.get(entry.target)

if (trackedElement && entry.intersectionRatio > 0) {
trackedElement.visibilityHandler()
}
})
}, options)
}
}

function createLoadableVisibilityComponent (args, {
Expand Down Expand Up @@ -52,6 +54,7 @@ function createLoadableVisibilityComponent (args, {
if (!preloaded) {
const element = this.loadingRef
trackedElements.set(element, this)
setIntersectionObserver(args[0].observerOptions)
intersectionObserver.observe(element)
}
}
Expand Down