diff --git a/src/UIComponentHelper.js b/src/UIComponentHelper.js index b6af7bf..18a455a 100644 --- a/src/UIComponentHelper.js +++ b/src/UIComponentHelper.js @@ -273,9 +273,13 @@ export default class UIComponentHelper { payload.observer && payload.intersectionObserverEntry ) { + const { intersectionObserverEntry: entry, observer } = payload; + const isIntersectionBugged = + entry.intersectionRatio === 0 && entry.isIntersecting; + return writer( - payload.intersectionObserverEntry.intersectionRatio * 100, - payload.observer + !isIntersectionBugged ? entry.intersectionRatio * 100 : 100, + observer ); } else { return writer(payload); diff --git a/src/__tests__/UIComponentHelperSpec.js b/src/__tests__/UIComponentHelperSpec.js index d6da5f4..b1f2812 100644 --- a/src/__tests__/UIComponentHelperSpec.js +++ b/src/__tests__/UIComponentHelperSpec.js @@ -117,4 +117,61 @@ describe('UIComponentHelper', () => { ).toBeTruthy(); }); }); + + describe('wrapVisibilityWriter method', () => { + function writer(...args) { + return `writer value: ${args.join(',')}`; + } + + function wrapWriterTest(payload) { + const entry = { payload }; + const parse = uiComponentHelper.wrapVisibilityWriter(writer); + + return parse(entry); + } + + const OBSERVER = {}; + const PAYLOAD = 42; + const PAYLOAD_OBJECT = { + observer: OBSERVER, + intersectionObserverEntry: { + intersectionRatio: 0.3, + isIntersecting: true + } + }; + const NONINTERSECTED_PAYLOAD_OBJECT = { + observer: OBSERVER, + intersectionObserverEntry: { + intersectionRatio: 0, + isIntersecting: false + } + }; + const BUGGED_PAYLOAD_OBJECT = { + observer: OBSERVER, + intersectionObserverEntry: { + intersectionRatio: 0, + isIntersecting: true + } + }; + + it('should return a return value of writer(PAYLOAD)', () => { + const result = wrapWriterTest(PAYLOAD); + expect(result).toBe(writer(PAYLOAD)); + }); + + it('should return a return value of writer(30, OBSERVER)', () => { + const result = wrapWriterTest(PAYLOAD_OBJECT); + expect(result).toBe(writer(30, OBSERVER)); + }); + + it('should return a return value of writer(0, OBSERVER)', () => { + const result = wrapWriterTest(NONINTERSECTED_PAYLOAD_OBJECT); + expect(result).toBe(writer(0, OBSERVER)); + }); + + it('should return a return value of writer(100, OBSERVER)', () => { + const result = wrapWriterTest(BUGGED_PAYLOAD_OBJECT); + expect(result).toBe(writer(100, OBSERVER)); + }); + }); });