From c3c41e09d7155a0ae8afee8f5f52916c0310e737 Mon Sep 17 00:00:00 2001 From: Scott Newcomer Date: Fri, 21 May 2021 20:52:47 -0500 Subject: [PATCH] [Bug]: better support for ember-data objects (#596) * [Bug]: better support for ember-data objects * [Test]: ember-data nested set * add more assertions --- package.json | 2 +- tests/unit/changeset-test.js | 48 +++++++++++++++++++++++++++++++++--- yarn.lock | 8 +++--- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 883abd75..453618da 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@glimmer/tracking": "^1.0.1", "ember-auto-import": "^1.5.2", "ember-cli-babel": "^7.19.0", - "validated-changeset": "~0.14.5" + "validated-changeset": "~0.14.6" }, "devDependencies": { "@ember/optional-features": "^1.0.0", diff --git a/tests/unit/changeset-test.js b/tests/unit/changeset-test.js index f3cfa7c4..8b845a30 100644 --- a/tests/unit/changeset-test.js +++ b/tests/unit/changeset-test.js @@ -881,7 +881,7 @@ module('Unit | Utility | changeset', function (hooks) { assert.equal(dummyChangeset.name.title.id, 'Mr', 'should have new change'); assert.equal(dummyChangeset.get('name.title.id'), 'Mr', 'should have new change using get'); - let changes = get(dummyChangeset, 'changes'); + let changes = dummyChangeset.changes; assert.deepEqual(changes, [{ key: 'name.title', value: title1 }], 'changes with nested key Ember.set'); set(dummyChangeset, 'name.title', title2); @@ -890,7 +890,7 @@ module('Unit | Utility | changeset', function (hooks) { assert.equal(dummyChangeset.name.title.id, 'Mrs', 'should have new change'); assert.equal(dummyChangeset.get('name.title.id'), 'Mrs', 'should have new change using get'); - changes = get(dummyChangeset, 'changes'); + changes = dummyChangeset.changes; assert.deepEqual(changes, [{ key: 'name.title', value: title2 }], 'changes with nested key Ember.set'); dummyChangeset.execute(); @@ -898,6 +898,46 @@ module('Unit | Utility | changeset', function (hooks) { assert.equal(dummyModel.name.title.id, 'Mrs', 'has new property'); }); + test('#set Ember.set with Ember Data Object actually does work TWICE for nested', async function (assert) { + let store = this.owner.lookup('service:store'); + + let mockProfileModel = store.createRecord('profile'); + let mockUserModel = store.createRecord('user', { + profile: mockProfileModel, + save: function () { + return Promise.resolve(this); + }, + }); + + let dummyChangeset = Changeset(mockUserModel); + let pet1 = store.createRecord('dog', { breed: 'jazzy' }); + let pet2 = store.createRecord('dog', { breed: 'hands' }); + + set(dummyChangeset, 'profile.pet', pet1); + + assert.equal(dummyChangeset.profile.pet.breed, 'jazzy', 'should have change'); + assert.equal(dummyChangeset.get('profile.pet.breed'), 'jazzy', 'should have change using get'); + assert.equal(dummyChangeset.get('profile.pet').breed, 'jazzy', 'should have change using get'); + assert.equal(dummyChangeset.get('profile').pet.breed, 'jazzy', 'should have change using get'); + + let changes = dummyChangeset.changes; + assert.equal(changes[0].value.breed, 'jazzy', 'changes with nested key Ember.set'); + + set(dummyChangeset, 'profile.pet', pet2); + + assert.equal(dummyChangeset.profile.pet.breed, 'hands', 'should have new change'); + assert.equal(dummyChangeset.get('profile.pet.breed'), 'hands', 'should have new change using get'); + assert.equal(dummyChangeset.get('profile.pet').breed, 'hands', 'should have new change using get'); + assert.equal(dummyChangeset.get('profile').pet.breed, 'hands', 'should have new change using get'); + + changes = dummyChangeset.changes; + assert.equal(changes[0].value.breed, 'hands', 'changes with nested key Ember.set'); + + dummyChangeset.execute(); + + assert.equal(get(mockUserModel, 'profile.pet.breed'), 'hands', 'has new property'); + }); + test('#set with Object should work TWICE for nested', async function (assert) { set(dummyModel, 'name', {}); let title1 = { id: 'Mr', description: 'Mister' }; @@ -909,7 +949,7 @@ module('Unit | Utility | changeset', function (hooks) { assert.equal(dummyChangeset.name.title.id, 'Mr', 'should have new change'); assert.equal(dummyChangeset.get('name.title.id'), 'Mr', 'should have new change using get'); - let changes = get(dummyChangeset, 'changes'); + let changes = dummyChangeset.changes; assert.deepEqual(changes, [{ key: 'name.title', value: title1 }], 'changes with nested key Ember.set'); dummyChangeset.set('name.title', title2); @@ -918,7 +958,7 @@ module('Unit | Utility | changeset', function (hooks) { assert.equal(dummyChangeset.name.title.id, 'Mrs', 'should have new change'); assert.equal(dummyChangeset.get('name.title.id'), 'Mrs', 'should have new change using get'); - changes = get(dummyChangeset, 'changes'); + changes = dummyChangeset.changes; assert.deepEqual(changes, [{ key: 'name.title', value: title2 }], 'changes with nested key Ember.set'); dummyChangeset.execute(); diff --git a/yarn.lock b/yarn.lock index 3eaf663a..e4a703f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13517,10 +13517,10 @@ validate-npm-package-name@~2.2.2: dependencies: builtins "0.0.7" -validated-changeset@~0.14.5: - version "0.14.5" - resolved "https://registry.yarnpkg.com/validated-changeset/-/validated-changeset-0.14.5.tgz#377feade30993b9eb7d0a87986483c36c8c71ad6" - integrity sha512-QKox80QIjm6I0jO13iVGb9KYpE2XmysaI+zv0wlBJvupUdKnzbtuDQIYm1W893ubl05oKi7TZLB2TNzvBXd1Jg== +validated-changeset@~0.14.6: + version "0.14.6" + resolved "https://registry.yarnpkg.com/validated-changeset/-/validated-changeset-0.14.6.tgz#811dd3174637cba0b0ba3f5d3ff25b43d045ef83" + integrity sha512-v89Lr0GN60gOwCxqZK/ReYHDrdiN3JcNyqQsOmxgBCwQlfbGs8fTwKUJDMTqDuWXS/HPsASDsBmQNkLOaolzIA== vary@~1.1.2: version "1.1.2"