Skip to content

Commit

Permalink
Merge pull request #5461 from emberjs/figuring-out-nulling-relationsh…
Browse files Browse the repository at this point in the history
…ip-by-returning-empty-payload

[BUGFIX] update content of proxy for async belongs-to relationships when null data received
  • Loading branch information
runspired committed Apr 30, 2018
2 parents 55b4d6b + ddf5e95 commit 5b33009
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion addon/-private/system/relationships/state/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default class BelongsToRelationship extends Relationship {

_updateLoadingPromise(promise, content) {
if (this._loadingPromise) {
if (content) {
if (content !== undefined) {
this._loadingPromise.set('content', content)
}
this._loadingPromise.set('promise', promise)
Expand Down
71 changes: 71 additions & 0 deletions tests/integration/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,77 @@ module("integration/relationship/belongs_to Belongs-To Relationships", {
}
});

test("returning a null relationship from payload sets the relationship to null on both sides", function(assert) {
env.registry.register('model:app', DS.Model.extend({
name: attr('string'),
team: belongsTo('team', { async: true })
}));
env.registry.register('model:team', DS.Model.extend({
apps: hasMany('app', {async: true})
}));
run(() => {
env.store.push({
data: {
id: '1',
type: 'app',
relationships: {
team: {
data: {
id: '1',
type: 'team'
}
}
}
},
included: [
{
id: '1',
type: 'team',
relationships: {
apps: {
data: [{
id: '1',
type: 'app'
}]
}
}
}
]
});
});

const app = env.store.peekRecord('app', '1');
const team = env.store.peekRecord('team', '1');
assert.equal(app.get('team.id'), team.get('id'), 'sets team correctly on app');
assert.deepEqual(team.get('apps').toArray().mapBy('id'), ['1'], 'sets apps correctly on team');

env.adapter.shouldBackgroundReloadRecord = () => false;
env.adapter.updateRecord = (store, type, snapshot) => {
return RSVP.resolve({
data: {
id: '1',
type: 'app',
attributes: {
name: 'Hello'
},
relationships: {
team: {
data: null
}
}
}
});
};

return run(() => {
app.set('name', 'Hello');
return app.save().then(() => {
assert.equal(app.get('team.id'), null, 'team removed from app relationship');
assert.deepEqual(team.get('apps').toArray().mapBy('id'), [], 'app removed from team apps relationship');
});
});
});

test("The store can materialize a non loaded monomorphic belongsTo association", function(assert) {
assert.expect(1);

Expand Down

0 comments on commit 5b33009

Please sign in to comment.