From 860f14a0bd4738a9795c1805520659b33b5323c9 Mon Sep 17 00:00:00 2001 From: Stanley Stuart Date: Wed, 27 Jun 2018 16:29:47 -0700 Subject: [PATCH] wait for run loop to end before resolving destroyRecord --- addon/-legacy-private/system/model/model.js | 3 +- .../relationships/belongs-to-test.js | 113 +++++++++--------- 2 files changed, 61 insertions(+), 55 deletions(-) diff --git a/addon/-legacy-private/system/model/model.js b/addon/-legacy-private/system/model/model.js index a8c463bba6a..fdcdae4d4d9 100644 --- a/addon/-legacy-private/system/model/model.js +++ b/addon/-legacy-private/system/model/model.js @@ -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; }); }, diff --git a/tests/integration/relationships/belongs-to-test.js b/tests/integration/relationships/belongs-to-test.js index 5d651e952fd..9dc212e4e0e 100644 --- a/tests/integration/relationships/belongs-to-test.js +++ b/tests/integration/relationships/belongs-to-test.js @@ -871,7 +871,7 @@ test('destroying records in a belongsTo relationship that loaded via links', fun comment: Comment, adapter: DS.RESTAdapter.extend({ shouldBackgroundReloadRecord: () => false, - }) + }), }); env.registry.register( @@ -879,7 +879,7 @@ test('destroying records in a belongsTo relationship that loaded via links', fun DS.RESTAdapter.extend({ deleteRecord() { return RSVP.resolve(); - } + }, }) ); @@ -890,10 +890,10 @@ test('destroying records in a belongsTo relationship that loaded via links', fun return { post: { id: '1', - type: 'post' - } - } - } + type: 'post', + }, + }; + }, }) ); @@ -906,10 +906,10 @@ 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', @@ -917,10 +917,10 @@ test('destroying records in a belongsTo relationship that loaded via links', fun relationships: { post: { links: { - related: '/comments/2/post' - } - } - } + related: '/comments/2/post', + }, + }, + }, }, { type: 'comment', @@ -928,29 +928,31 @@ test('destroying records in a belongsTo relationship that loaded via links', fun 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); + }); }); }); @@ -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( @@ -978,7 +980,7 @@ test('destroying records in a belongsTo relationship that loaded via sideloading deleteRecord() { return RSVP.resolve(); }, - shouldBackgroundReloadRecord: () => false + shouldBackgroundReloadRecord: () => false, }) ); @@ -989,10 +991,10 @@ test('destroying records in a belongsTo relationship that loaded via sideloading return { post: { id: '1', - type: 'post' - } - } - } + type: 'post', + }, + }; + }, }) ); @@ -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', + }, }, }, }, @@ -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', + }, }, }, }, @@ -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); + }); }); });