Skip to content

Commit

Permalink
[Bug]: better support for ember-data objects (#596)
Browse files Browse the repository at this point in the history
* [Bug]: better support for ember-data objects

* [Test]: ember-data nested set

* add more assertions
  • Loading branch information
snewcomer authored May 22, 2021
1 parent 837a23a commit c3c41e0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 44 additions & 4 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -890,14 +890,54 @@ 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();

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' };
Expand All @@ -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);
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c3c41e0

Please sign in to comment.