Skip to content

Commit

Permalink
Merge pull request #122 from DockYard/fix-viewport-tolerance
Browse files Browse the repository at this point in the history
Fix viewportTolerance with defaults
  • Loading branch information
snewcomer authored Feb 8, 2018
2 parents 8306c39 + f3d771f commit 800d2c6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion addon/mixins/in-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default Mixin.create({
}

if (get(this, 'viewportUseIntersectionObserver')) {
const { top, left, bottom, right } = this.viewportTolerance;
const { top = 0, left = 0, bottom = 0, right = 0 } = this.viewportTolerance;
const options = {
root: scrollableArea,
rootMargin: `${top}px ${right}px ${bottom}px ${left}px`,
Expand Down
5 changes: 5 additions & 0 deletions tests/dummy/app/components/my-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export default Component.extend(InViewportMixin, {
viewportSpyOverride,
viewportEnabledOverride,
viewportIntersectionObserverOverride,
viewportToleranceOverride,
viewportRAFOverride,
scrollableAreaOverride
} = getProperties(this,
'viewportSpyOverride',
'viewportEnabledOverride',
'viewportIntersectionObserverOverride',
'viewportToleranceOverride',
'viewportRAFOverride',
'scrollableAreaOverride'
);
Expand All @@ -33,6 +35,9 @@ export default Component.extend(InViewportMixin, {
if (viewportIntersectionObserverOverride !== undefined) {
options.viewportUseIntersectionObserver = viewportIntersectionObserverOverride;
}
if (viewportToleranceOverride !== undefined) {
options.viewportTolerance = viewportToleranceOverride;
}
if (viewportRAFOverride !== undefined) {
options.viewportUseRAF = viewportRAFOverride;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/dummy/app/controllers/infinity-scrollable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ let line = '<line x1="10" x2="50" y1="110" y2="150" stroke="orange" stroke-width
const images = [rect, circle, line];

export default Controller.extend({
viewportToleranceOverride: {
top: 1
},

actions: {
infinityLoad() {
const newModels = [...Array(10).fill().map(() => `${images[(Math.random() * images.length) | 0]}`)];
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/templates/infinity-scrollable.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
{{my-component
class="infinity-scrollable"
scrollableAreaOverride=".list"
viewportToleranceOverride=viewportToleranceOverride
infinityLoad=(action "infinityLoad")}}
</div>
37 changes: 37 additions & 0 deletions tests/integration/components/my-component-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('my-component', 'Integration | Component | my component', {
integration: true
});

test('it renders with viewportTolerance partially set', function(assert) {
// viewportTolerance && viewportEnabled is usually setup in the initializer. Needs defaults
this.viewportToleranceOverride = {
top: 1
};
this.render(hbs`
{{#my-component viewportEnabled=true viewportToleranceOverride=viewportToleranceOverride}}
template block text
{{/my-component}}
`);

assert.equal(this.$().text().trim(), 'template block text');
});

test('it renders with intersectionThreshold set', function(assert) {
this.viewportTolerance = {
top: 0,
left: 0,
right: 0,
bottom: 0,
};
this.intersectionThreshold = 1.0;
this.render(hbs`
{{#my-component viewportEnabled=true viewportTolerance=viewportTolerance intersectionThreshold=intersectionThreshold}}
template block text
{{/my-component}}
`);

assert.equal(this.$().text().trim(), 'template block text');
});

0 comments on commit 800d2c6

Please sign in to comment.