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

Replace Mixin with inViewport service #410

Merged
merged 5 commits into from
Jan 18, 2020
Merged
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
21 changes: 18 additions & 3 deletions addon/components/infinity-loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import InViewportMixin from 'ember-in-viewport';
import { run } from '@ember/runloop';
import { get, set, computed, defineProperty } from '@ember/object';
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { resolve } from 'rsvp';

const InfinityLoaderComponent = Component.extend(InViewportMixin, {
const InfinityLoaderComponent = Component.extend({
infinity: service(),
inViewport: service(),

classNames: ['infinity-loader'],
classNameBindings: ['isDoneLoading:reached-infinity', 'viewportEntered:in-viewport'],
Expand Down Expand Up @@ -115,6 +115,21 @@ const InfinityLoaderComponent = Component.extend(InViewportMixin, {

this.addObserver('hideOnInfinity', this, this._loadStatusDidChange);
this.addObserver('reachedInfinity', this, this._loadStatusDidChange);

let options = {
viewportSpy: true,
viewportTolerance: {
top: 0,
right: 0,
bottom: this.triggerOffset,
left: 0
},
scrollableArea: this.scrollable
};
const { onEnter, onExit } = this.inViewport.watchElement(this.element, options);

onEnter(this.didEnterViewport.bind(this));
onExit(this.didExitViewport.bind(this));
},

willDestroyElement() {
Expand All @@ -139,7 +154,7 @@ const InfinityLoaderComponent = Component.extend(InViewportMixin, {
*/
didEnterViewport() {
if (
get(this, 'developmentMode') ||
this.developmentMode ||
typeof FastBoot !== 'undefined' ||
this.isDestroying ||
this.isDestroyed
Expand Down
17 changes: 10 additions & 7 deletions tests/acceptance/infinity-load-previous-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { module, test } from 'qunit';
import { visit, find, settled, triggerEvent } from '@ember/test-helpers';
import { visit, find, settled, triggerEvent, waitUntil } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import defaultScenario from '../../mirage/scenarios/default';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
Expand Down Expand Up @@ -30,7 +30,11 @@ module('Acceptance: Infinity Route - load previous', function(hooks) {
document.getElementsByClassName('infinity-loader-bottom')[0].scrollIntoView(false);
}

function shouldBeItemsOnTheList(assert, amount) {
async function shouldBeItemsOnTheList(assert, amount) {
await waitUntil(() => {
return postList().querySelectorAll('p').length === amount;
});

assert.equal(postList().querySelectorAll('p').length, amount, `${amount} items should be in the list`);
}

Expand All @@ -48,27 +52,26 @@ module('Acceptance: Infinity Route - load previous', function(hooks) {
defaultScenario(this.server);
await visit('/load-previous');

await settled();
shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
infinityShouldNotBeReached(assert);
scrollTo(triggerOffset() - 100);

await triggerEvent(window, 'scroll');

shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
scrollIntoView();

await triggerEvent(window, 'scroll');

shouldBeItemsOnTheList(assert, 50);
await shouldBeItemsOnTheList(assert, 50);
});

test('it should load previous elements when start on page two', async function(assert) {
defaultScenario(this.server);
await visit('/load-previous?page=2');

await settled();
shouldBeItemsOnTheList(assert, 50);
await shouldBeItemsOnTheList(assert, 50);
// This is difficult b/c of #ember-testing-container
// assert.equal(document.querySelectorAll('.posts p')[25].offsetTop, 12500, 'scrollable list has elements above (each 250px high * 25)');
});
Expand Down
44 changes: 27 additions & 17 deletions tests/acceptance/infinity-route-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { module, test } from 'qunit';
import { currentRouteName, visit, find, settled, triggerEvent, currentURL, waitUntil } from '@ember/test-helpers';
import { currentRouteName, visit, find, triggerEvent, currentURL, waitUntil } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

Expand Down Expand Up @@ -29,7 +29,10 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
document.getElementsByClassName('infinity-loader-bottom')[0].scrollIntoView(false);
}

function shouldBeItemsOnTheList(assert, amount) {
async function shouldBeItemsOnTheList(assert, amount) {
await waitUntil(() => {
return postList().querySelectorAll('li').length === amount;
});
assert.equal(postList().querySelectorAll('li').length, amount, `${amount} items should be in the list`);
}

Expand All @@ -53,18 +56,18 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
this.server.createList('post', 50);
await visit('/test-scrollable');

shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
infinityShouldNotBeReached(assert);
scrollTo(triggerOffset() - 100);

await triggerEvent('ul', 'scroll');

shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
scrollIntoView();

await triggerEvent('ul', 'scroll');

shouldBeItemsOnTheList(assert, 50);
await shouldBeItemsOnTheList(assert, 50);
infinityShouldBeReached(assert);

const global = this.owner.lookup('service:global');
Expand All @@ -76,32 +79,31 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
this.server.createList('post', 50);
await visit('/test-scrollable?triggerOffset=200');

shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
infinityShouldNotBeReached(assert);
scrollTo(triggerOffset() - 200 - 100);

await triggerEvent('ul', 'scroll');

shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
scrollTo(triggerOffset() - 200);

await triggerEvent('ul', 'scroll');

shouldBeItemsOnTheList(assert, 25);
await shouldBeItemsOnTheList(assert, 25);
scrollIntoView();

await triggerEvent('ul', 'scroll');

shouldBeItemsOnTheList(assert, 50);
await shouldBeItemsOnTheList(assert, 50);
infinityShouldBeReached(assert);
});

test('it should load previous elements when start on page two', async function(assert) {
this.server.createList('post', 50);
await visit('/test-scrollable?page=2');

await settled();
shouldBeItemsOnTheList(assert, 50);
await shouldBeItemsOnTheList(assert, 50);
assert.equal(document.querySelectorAll('ul.test-list-scrollable li')[25].offsetTop, 1250, 'scrollable list has elements above (each 250px high * 25)');
});

Expand All @@ -113,15 +115,19 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
return postList().querySelectorAll('li').length === 12;
});

shouldBeItemsOnTheList(assert, 12);
await shouldBeItemsOnTheList(assert, 12);
});

module('Acceptance: Infinity Route - multiple pages fetched', function(/*hooks*/) {
test('it should load previous elements when start on page three and scroll up', async function(assert) {
this.server.createList('post', 75);
await visit('/test-scrollable?page=3');

shouldBeItemsOnTheList(assert, 50);
await waitUntil(() => {
return postList().querySelectorAll('li').length === 50;
});

await shouldBeItemsOnTheList(assert, 50);
assert.equal(currentURL(), '/test-scrollable?page=3');

await triggerEvent('ul', 'scroll');
Expand All @@ -132,7 +138,7 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
return postList().querySelectorAll('li').length === 75;
});

shouldBeItemsOnTheList(assert, 75);
await shouldBeItemsOnTheList(assert, 75);
});
});

Expand All @@ -141,7 +147,7 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
this.server.createList('post', 15);
await visit('/extended');

shouldBeItemsOnTheList(assert, 6);
await shouldBeItemsOnTheList(assert, 6);
assert.equal(currentURL(), '/extended');

await triggerEvent('ul', 'scroll');
Expand All @@ -151,7 +157,7 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
return postList().querySelectorAll('li').length === 12;
});

shouldBeItemsOnTheList(assert, 12);
await shouldBeItemsOnTheList(assert, 12);

await triggerEvent('ul', 'scroll');
document.querySelector('.infinity-loader').scrollIntoView(false);
Expand All @@ -160,7 +166,7 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {
return postList().querySelectorAll('li').length === 15;
});

shouldBeItemsOnTheList(assert, 15);
await shouldBeItemsOnTheList(assert, 15);
});
});

Expand Down Expand Up @@ -193,6 +199,10 @@ module('Acceptance: Infinity Route - infinity routes', function(hooks) {

await triggerEvent('ul', 'scroll');

await waitUntil(() => {
return find('ul').querySelectorAll('li').length === 50;
});

assert.equal(find('ul').querySelectorAll('li').length, 50, `${50} items should be in the list`);
assert.equal(find('.infinity-loader').classList.contains('reached-infinity'), true, 'Infinity should have been reached');
});
Expand Down