Skip to content

Commit

Permalink
custom store test
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Apr 17, 2017
1 parent a6e2720 commit 450a142
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 17 deletions.
7 changes: 0 additions & 7 deletions addon/components/infinity-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Ember from 'ember';
import emberVersionIs from 'ember-version-is';

const InfinityLoaderComponent = Ember.Component.extend({
classNames: ["infinity-loader"],
Expand Down Expand Up @@ -128,10 +127,4 @@ const InfinityLoaderComponent = Ember.Component.extend({
})
});

if (emberVersionIs('lessThan', '1.13.0')) {
InfinityLoaderComponent.reopen({
hasBlock: Ember.computed.alias('template')
});
}

export default InfinityLoaderComponent;
10 changes: 3 additions & 7 deletions addon/mixins/route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Ember from 'ember';
const { Mixin, computed } = Ember;
import { emberDataVersionIs } from 'ember-version-is';
import { objectAssign } from '../utils';

/**
The Ember Infinity Route Mixin enables an application route to load paginated
Expand Down Expand Up @@ -199,11 +199,7 @@ const RouteMixin = Mixin.create({
@return {Ember.RSVP.Promise}
*/
infinityModel(modelName, options, boundParams) {
if (emberDataVersionIs('lessThan', '1.13.0')) {
this.set('_storeFindMethod', 'find');
}

options = options ? Ember.assign({}, options) : {};
options = options ? objectAssign({}, options) : {};

this.set('_infinityModelName', modelName);

Expand Down Expand Up @@ -335,7 +331,7 @@ const RouteMixin = Mixin.create({
pageParams[this.get('pageParam')] = nextPage;
}

const params = Ember.assign(pageParams, this.get('_extraParams'));
const params = objectAssign(pageParams, this.get('_extraParams'));

const boundParams = this.get('_boundParams');
if (!Ember.isEmpty(boundParams)) {
Expand Down
19 changes: 19 additions & 0 deletions addon/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export let objectAssign = Object.assign || function objectAssign(target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}

target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
"ember-addon"
],
"dependencies": {
"ember-cli-babel": "^5.1.6",
"ember-cli-version-checker": "^1.0.2",
"ember-version-is": "0.0.3"
"ember-cli-babel": "^5.1.6"
},
"ember-addon": {
"configPath": "tests/dummy/config",
Expand Down
32 changes: 32 additions & 0 deletions tests/acceptance/infinity-custom-store-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
import buildServer from '../helpers/fake-album-server';
import assertDetails from '../helpers/assert-acceptance-details';

let server;

moduleForAcceptance('Acceptance: Infinity Route - custom store route', {
beforeEach() {
server = buildServer();
this.customStore = this.application.__container__.lookup('service:custom-store');
for (let x = 0; x <= 5; x++) {
this.customStore.push('custom-model', { id: x });
}
},
afterEach() {
server.shutdown();
delete this.customStore;
}
});

test('scott scott it works with custom store', function(assert) {
visit('/custom-store');

andThen(() => {
assertDetails(assert, {
title: 'Listing Posts',
listLength: 6,
reachedInfinity: true
});
});
});
6 changes: 6 additions & 0 deletions tests/dummy/app/models/custom-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Ember from 'ember';

export default Ember.Object.extend({
name: ''
});

1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Router = Ember.Router.extend({
});

Router.map(function() {
this.route('custom-store');
this.route('demo', { path: '/' });
this.route('demo-scrollable', { path: '/demo-scrollable' });
this.route('home', { path: 'test' });
Expand Down
10 changes: 10 additions & 0 deletions tests/dummy/app/routes/custom-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Ember from 'ember';
const { Route, inject: { service } } = Ember;
import InfinityRoute from 'ember-infinity/mixins/route';

export default Route.extend(InfinityRoute, {
customStore: service(),
model() {
return this.infinityModel('custom-model', { store: 'customStore', storeFindMethod: 'findAll' });
}
});
46 changes: 46 additions & 0 deletions tests/dummy/app/services/custom-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Ember from 'ember';

function factoryForType(type, store) {
return Ember.getOwner(store)._lookupFactory('model:' + type);
}

function persistData(type, obj, customStore) {
const persistentContainer = customStore.get('persistentContainer');
const Factory = factoryForType(type, customStore);
const record = Factory.create(obj);
const id = obj.id;
if (persistentContainer[type]) {
return persistentContainer[type].set(id, record);
}
persistentContainer[type] = new Map();
return persistentContainer[type].set(id, record);
}

export default Ember.Service.extend({
/**
* holds all models by type
* @property persistentContainer
*/
persistentContainer: {},
/**
* find objects by id
* find method must return a promise so patching in a thennable w/ resolve
* @method findAll
* @param {String} type
*/
findAll(type) {
const containerObjs = this.get('persistentContainer')[type];
const content = Array.from(containerObjs.values());
const arrProxy = Ember.ArrayProxy.create({ content: Ember.A(content) });
return Ember.RSVP.resolve(arrProxy);
},
/**
* @method push
* @param {String} type
* @param {Object} obj
*/
push(type, obj) {
const hydrated = persistData(type, obj, this);
return hydrated.get(obj.id);
},
});
9 changes: 9 additions & 0 deletions tests/dummy/app/templates/custom-store.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h2 id="posts-title">Listing Posts</h2>

<ul class="test-list">
{{#each model as |post|}}
<li>{{post.name}}</li>
{{/each}}
</ul>

{{infinity-loader infinityModel=model}}

0 comments on commit 450a142

Please sign in to comment.