From 765fda22d8993ec733b833749748d006dbec5264 Mon Sep 17 00:00:00 2001 From: Stanley Stuart Date: Thu, 26 Apr 2018 10:55:48 -0700 Subject: [PATCH] failing test for record not unloaded when a payload is returned It seems common for APIs to return a paylaod when deleting a record. Although there is a test for when `undefined` is returned from `deleteRecord` in the adapter here: https://github.com/emberjs/data/blob/master/tests/integration/records/delete-record-test.js#L87 There are currently no tests showing what happens when you return a payload. It seems like the correct behavior for destroyRecord should be `deleteRecord` + `save` + `unloadRecord`. --- .../integration/records/delete-record-test.js | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/integration/records/delete-record-test.js b/tests/integration/records/delete-record-test.js index 116b533e824..0c09a99f3de 100644 --- a/tests/integration/records/delete-record-test.js +++ b/tests/integration/records/delete-record-test.js @@ -139,6 +139,77 @@ test('deleting a record that is part of a hasMany removes it from the hasMany re assert.equal(group.get('people.length'), 1, 'expected 1 related records after delete'); }); +test('deleting a record that is part of a hasMany removes it from the hasMany recordArray when adapter deleteRecord function returns a payload', function(assert) { + let group; + let person; + const Group = DS.Model.extend({ + people: DS.hasMany('person', { inverse: null, async: false }) + }); + Group.toString = () => { return 'Group'; } + + env.adapter.deleteRecord = function() { + return EmberPromise.resolve({ + data: { + id: '1', + type: 'group', + attributes: {} + } + }); + }; + + env.registry.register('model:group', Group); + + run(function() { + env.store.push({ + data: { + type: 'group', + id: '1', + relationships: { + people: { + data: [ + { type: 'person', id: '1' }, + { type: 'person', id: '2' } + ] + } + } + }, + included: [ + { + type: 'person', + id: '1', + attributes: { + name: 'Adam Sunderland' + } + }, + { + type: 'person', + id: '2', + attributes: { + name: 'Dave Sunderland' + } + } + ] + }); + + group = env.store.peekRecord('group', '1'); + person = env.store.peekRecord('person', '1'); + }); + + // Sanity Check we are in the correct state. + assert.equal(group.get('people.length'), 2, 'expected 2 related records before delete'); + assert.equal(person.get('name'), 'Adam Sunderland', 'expected related records to be loaded'); + + const promise = run(() => group.destroyRecord()); + + return promise.then(() => { + // qunit complains if i pass the record to QUnit due to some assertion in ember + // iterating over the properties. + const hasGroup = env.store.peekRecord('group', '1') ? true : false; + assert.equal(hasGroup, false); + assert.equal(person.get('group'), null, 'expected relationship to be null'); + }); +}); + test('records can be deleted during record array enumeration', function(assert) { var adam, dave;