Skip to content

Commit

Permalink
wait for run loop to end before resolving destroyRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
fivetanley committed Jul 17, 2018
1 parent 23e44c9 commit 860f14a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
3 changes: 2 additions & 1 deletion addon/-legacy-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ const Model = EmberObject.extend(Evented, {
destroyRecord(options) {
this.deleteRecord();
return this.save(options).then(() => {
this.unloadRecord();
Ember.run(() => this.unloadRecord());
return this;
});
},

Expand Down
113 changes: 59 additions & 54 deletions tests/integration/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,15 +871,15 @@ test('destroying records in a belongsTo relationship that loaded via links', fun
comment: Comment,
adapter: DS.RESTAdapter.extend({
shouldBackgroundReloadRecord: () => false,
})
}),
});

env.registry.register(
'adapter:post',
DS.RESTAdapter.extend({
deleteRecord() {
return RSVP.resolve();
}
},
})
);

Expand All @@ -890,10 +890,10 @@ test('destroying records in a belongsTo relationship that loaded via links', fun
return {
post: {
id: '1',
type: 'post'
}
}
}
type: 'post',
},
};
},
})
);

Expand All @@ -906,51 +906,53 @@ test('destroying records in a belongsTo relationship that loaded via links', fun
relationships: {
post: {
links: {
related: '/comments/1/post'
}
}
}
related: '/comments/1/post',
},
},
},
},
{
type: 'comment',
id: '2',
relationships: {
post: {
links: {
related: '/comments/2/post'
}
}
}
related: '/comments/2/post',
},
},
},
},
{
type: 'comment',
id: '3',
relationships: {
post: {
links: {
related: '/comments/3/post'
}
}
}
}
]
related: '/comments/3/post',
},
},
},
},
],
});
});

return run(() => {
let comments = env.store.peekAll('comment').toArray();
let posts = RSVP.map(comments, (cc) => {
let posts = RSVP.map(comments, cc => {
return cc.get('post');
});

return posts.then(posts => {
assert.deepEqual(Ember.A(posts).mapBy('id'), ['1', '1', '1']);
let post = env.store.peekRecord('post', '1');
return post.destroyRecord();
}).then(() => {
let comment = env.store.peekRecord('comment', '1');
assert.equal(comment.get('post.content'), null);
});
return posts
.then(posts => {
assert.deepEqual(Ember.A(posts).mapBy('id'), ['1', '1', '1']);
let post = env.store.peekRecord('post', '1');
return post.destroyRecord();
})
.then(() => {
let comment = env.store.peekRecord('comment', '1');
assert.equal(comment.get('post.content'), null);
});
});
});

Expand All @@ -969,7 +971,7 @@ test('destroying records in a belongsTo relationship that loaded via sideloading
env = setupStore({
post: Post,
comment: Comment,
adapter: DS.RESTAdapter.extend()
adapter: DS.RESTAdapter.extend(),
});

env.registry.register(
Expand All @@ -978,7 +980,7 @@ test('destroying records in a belongsTo relationship that loaded via sideloading
deleteRecord() {
return RSVP.resolve();
},
shouldBackgroundReloadRecord: () => false
shouldBackgroundReloadRecord: () => false,
})
);

Expand All @@ -989,10 +991,10 @@ test('destroying records in a belongsTo relationship that loaded via sideloading
return {
post: {
id: '1',
type: 'post'
}
}
}
type: 'post',
},
};
},
})
);

Expand All @@ -1005,8 +1007,9 @@ test('destroying records in a belongsTo relationship that loaded via sideloading
relationships: {
post: {
data: {
type: 'post', id: '1'
}
type: 'post',
id: '1',
},
},
},
},
Expand All @@ -1016,8 +1019,9 @@ test('destroying records in a belongsTo relationship that loaded via sideloading
relationships: {
post: {
data: {
type: 'post', id: '1'
}
type: 'post',
id: '1',
},
},
},
},
Expand All @@ -1027,33 +1031,34 @@ test('destroying records in a belongsTo relationship that loaded via sideloading
relationships: {
post: {
data: {
type: 'post', id: '1'
}
type: 'post',
id: '1',
},
},
},
},
],
included: [
{ type: 'post', id: '1' }
]
included: [{ type: 'post', id: '1' }],
});
});

return run(() => {
let comments = env.store.peekAll('comment').toArray();
let posts = RSVP.map(comments, (cc) => {
let posts = RSVP.map(comments, cc => {
return cc.get('post');
});

return posts.then(posts => {
console.log('posts', posts);
assert.deepEqual(Ember.A(posts).mapBy('id'), ['1', '1', '1']);
let post = env.store.peekRecord('post', '1');
return post.destroyRecord();
}).then(() => {
let comment = env.store.peekRecord('comment', '1');
assert.equal(comment.get('post.content'), null);
});
return posts
.then(posts => {
console.log('posts', posts);
assert.deepEqual(Ember.A(posts).mapBy('id'), ['1', '1', '1']);
let post = env.store.peekRecord('post', '1');
return post.destroyRecord();
})
.then(() => {
let comment = env.store.peekRecord('comment', '1');
assert.equal(comment.get('post.content'), null);
});
});
});

Expand Down

0 comments on commit 860f14a

Please sign in to comment.