Skip to content

Commit

Permalink
fix(uicomponenthelper): ratio of 0 fixed
Browse files Browse the repository at this point in the history
bugged entry with intersectionRatio: 0 and isIntersecting: true fixed

#50
  • Loading branch information
Ondrej Base committed Aug 21, 2018
1 parent c0384d5 commit 1dfa870
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/UIComponentHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
57 changes: 57 additions & 0 deletions src/__tests__/UIComponentHelperSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
});

0 comments on commit 1dfa870

Please sign in to comment.