From 5ed556bbd199d13d10a96aa46e4d90a2e63a459e Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Tue, 19 Nov 2019 12:32:51 -0800 Subject: [PATCH] [BUGFIX] fix detect usage for native class polymorphism (#6767) * polymorphic relations and native classes mixins bug test * [BUGFIX] fix detect usage for native class polymorphism --- .../polymorphic-mixins-has-many-test.js | 50 ++++++++++++++++++- packages/store/addon/-debug/index.js | 8 +-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/-ember-data/tests/integration/relationships/polymorphic-mixins-has-many-test.js b/packages/-ember-data/tests/integration/relationships/polymorphic-mixins-has-many-test.js index 7fc85e0d93e..8168e9e211a 100644 --- a/packages/-ember-data/tests/integration/relationships/polymorphic-mixins-has-many-test.js +++ b/packages/-ember-data/tests/integration/relationships/polymorphic-mixins-has-many-test.js @@ -14,13 +14,15 @@ module( function(hooks) { setupTest(hooks); + let Message; + hooks.beforeEach(function() { const User = Model.extend({ name: attr('string'), messages: hasMany('message', { async: true, polymorphic: true }), }); - const Message = Mixin.create({ + Message = Mixin.create({ title: attr('string'), user: belongsTo('user', { async: true }), }); @@ -131,6 +133,52 @@ module( }); }); + test('NATIVE CLASSES: Pushing to the hasMany reflects the change on the belongsTo side - async', function(assert) { + class Video extends Model.extend(Message) {} + + this.owner.register('model:video', Video); + + let store = this.owner.lookup('service:store'); + + var user, video; + run(function() { + store.push({ + data: [ + { + type: 'user', + id: '1', + attributes: { + name: 'Stanley', + }, + relationships: { + messages: { + data: [], + }, + }, + }, + { + type: 'video', + id: '2', + attributes: { + video: 'Here comes Youtube', + }, + }, + ], + }); + user = store.peekRecord('user', 1); + video = store.peekRecord('video', 2); + }); + + run(function() { + user.get('messages').then(function(fetchedMessages) { + fetchedMessages.pushObject(video); + video.get('user').then(function(fetchedUser) { + assert.equal(fetchedUser, user, 'user got set correctly'); + }); + }); + }); + }); + /* Local edits */ diff --git a/packages/store/addon/-debug/index.js b/packages/store/addon/-debug/index.js index 5574feb26c3..db66aa57839 100644 --- a/packages/store/addon/-debug/index.js +++ b/packages/store/addon/-debug/index.js @@ -29,9 +29,11 @@ let assertPolymorphicType; if (DEBUG) { let checkPolymorphic = function checkPolymorphic(modelClass, addedModelClass) { if (modelClass.__isMixin) { - //TODO Need to do this in order to support mixins, should convert to public api - //once it exists in Ember - return modelClass.__mixin.detect(addedModelClass.PrototypeMixin); + return ( + modelClass.__mixin.detect(addedModelClass.PrototypeMixin) || + // handle native class extension e.g. `class Post extends Model.extend(Commentable) {}` + modelClass.__mixin.detect(Object.getPrototypeOf(addedModelClass).PrototypeMixin) + ); } return addedModelClass.prototype instanceof modelClass || modelClass.detect(addedModelClass);