Skip to content

Commit

Permalink
[BUGFIX reload] fix sync belongsTo reload w/dematerialize (#6158)
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired authored Jun 17, 2019
1 parent 377b88d commit 48dd812
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';
import Model, { belongsTo, attr } from '@ember-data/model';
import EmberObject from '@ember/object';
import { reject } from 'rsvp';

module('Relationships | unloading new records', function(hooks) {
setupTest(hooks);
let store;
let entryNode;

class ErrorAdapter extends EmberObject {
shouldBackgroundReloadRecord() {
return false;
}
shouldBackgroundReloadAll() {
return false;
}
shouldReloadAll() {
return false;
}
shouldReloadRecord() {
return false;
}
findRecord() {
return reject(new Error(`Bad Request`));
}
}

class Node extends Model {
@attr() name;
@belongsTo('node', { inverse: null, async: false }) parent;
@belongsTo('node', { inverse: null, async: true }) relatedGraph;
}

hooks.beforeEach(function() {
const { owner } = this;
owner.register('model:node', Node);
owner.register('adapter:application', ErrorAdapter);
store = owner.lookup('service:store');

entryNode = store.push({
data: {
type: 'node',
id: '2',
attributes: { name: 'entry-node' },
relationships: {
parent: { data: { type: 'node', id: '1' } },
relatedGraph: { data: { type: 'node', id: '3' } },
},
},
included: [
{
type: 'node',
id: '1',
attributes: { name: 'root' },
},
{
type: 'node',
id: '3',
attributes: { name: 'an async relatedGraph entry' },
},
],
});
});

test('Reloading a sync belongsTo returns the error thrown', async function(assert) {
try {
await entryNode.belongsTo('parent').reload();
assert.ok(false, 'No error thrown');
} catch (e) {
assert.equal(e.message, 'Bad Request', 'We caught the correct error');
}
});

test('Reloading an async belongsTo returns the error thrown', async function(assert) {
try {
await entryNode.belongsTo('relatedGraph').reload();
assert.ok(false, 'No error thrown');
} catch (e) {
assert.equal(e.message, 'Bad Request', 'We caught the correct error');
}
});
});
5 changes: 4 additions & 1 deletion packages/store/addon/-private/system/model/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,10 @@ function handleCompletedRelationshipRequest(internalModel, key, relationship, va
// when a load fails, in this case we need
// to make sure that we aren't proxying
// to destroyed content
if (relationship.kind === 'belongsTo') {
// for the sync belongsTo reload case there will be no proxy
// for the async reload case there will be no proxy if the ui
// has never been accessed
if (proxy && relationship.kind === 'belongsTo') {
if (proxy.content.isDestroying) {
proxy.set('content', null);
}
Expand Down

0 comments on commit 48dd812

Please sign in to comment.