Skip to content

Commit

Permalink
Properly handle single results in LiveQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed May 24, 2019
1 parent 125b626 commit 4b5892d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 8 additions & 7 deletions addon/-private/live-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ export default ReadOnlyArrayProxy.extend({
content: computed('_content', {
get() {
if (get(this, '_content') === null) {
let results = this._sourceCache.query(this._query);

let content;
if (isArray(results)) {
content = results.map(r => this._identityMap.lookup(r))
} else if (typeof results === 'object') {
content = Object.keys(results).map(r => this._identityMap.lookup(results[r]))
let result = this._sourceCache.query(this._query);
let content = this._identityMap.lookupQueryResult(this._query, result);
if (content) {
if (!isArray(content)) {
content = [content];
}
} else {
content = [];
}
// eslint-disable-next-line ember/no-side-effects
set(this, '_content', content);
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/cache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ module('Integration - Cache', function(hooks) {
assert.notOk(planets.includes(jupiter));
});

test('liveQuery - findRelatedRecord updates when matching record is added', async function(assert) {
const callisto = await store.addRecord({ type: 'moon', name: 'Callisto' });
const planets = cache.liveQuery(q => q.findRelatedRecord(callisto, 'planet'));
assert.equal(planets.length, 0);

const jupiter = await store.addRecord({ id: 'jupiter', type: 'planet', name: 'Jupiter' });
callisto.set('planet', jupiter);
await store.requestQueue.process();

assert.equal(planets.length, 1);
assert.ok(planets.includes(jupiter));
});

test('#retrieveAttribute', async function(assert) {
const jupiter = await store.addRecord({type: 'planet', name: 'Jupiter'});
assert.equal(cache.retrieveAttribute(jupiter, 'name'), 'Jupiter');
Expand Down

0 comments on commit 4b5892d

Please sign in to comment.