adopted-ember-addons / adopted-ember-addons/ember-changeset

Setting values at one level of a (nested) EmberData changeset should not overwrite other levels (mergeDeep)

Aperta
#669 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
425
Fork
136
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

## Version
master

## Test Case

`
test('#set set with Ember Data Object should not change other nested ember-data objects', async function (assert) {

let store = this.owner.lookup('service:store');

let mockDogModel = store.createRecord('dog', { breed: 'jazzy' });
let mockProfileModel = store.createRecord('profile', {pet: mockDogModel});
let mockUserModel = store.createRecord('user', {
profile: mockProfileModel,
save: function () {
return Promise.resolve(this);
},
});

let dummyChangeset = Changeset(mockUserModel);

//Check our initial values - these all pass
//assert.strictEqual(dummyChangeset.profile.pet.breed, 'jazzy', 'should not change'); //fails as it is a proxy within a proxy within a model - requires the use of 'get'
assert.strictEqual(dummyChangeset.get('profile.pet.breed'), 'jazzy', 'should not change using get #1');
assert.strictEqual(dummyChangeset.get('profile.pet').breed, 'jazzy', 'should not change using get #2');
assert.strictEqual(dummyChangeset.get('profile').pet.breed, 'jazzy', 'should not change using get #3');

//test fails using either form of setter
//dummyChangeset.set('profile.lastName', 'Mysurname');
set(dummyChangeset, 'profile.lastName', 'Mysurname');

//Log changeset.changes and changeset._changes
let changes = dummyChangeset.changes;
console.log("changes", changes);
console.log("_changes", dummyChangeset._changes);

assert.strictEqual(changes[0].value, 'Mysurname', 'changes with nested key Ember.set');

//Check values after setting unrelated field - these all pass
//assert.strictEqual(dummyChangeset.profile.pet.breed, 'jazzy', 'should not change after other set'); //fails as it is a proxy within a proxy within a model - requires the use of 'get'
assert.strictEqual(dummyChangeset.get('profile.pet.breed'), 'jazzy', 'should not change after other set using get #1');
assert.strictEqual(dummyChangeset.get('profile.pet').breed, 'jazzy', 'should not change after other set using get #2');
assert.strictEqual(dummyChangeset.get('profile').pet.breed, 'jazzy', 'should not change after other set using get #3');

dummyChangeset.execute();
console.log("changes2", changes);
console.log("_changes2", dummyChangeset._changes);

//Recheck values after executing change on unrelated field - these tests FAIL
//assert.strictEqual(dummyChangeset.profile.pet.breed, 'jazzy', 'should not change after execute'); //fails as it is a proxy within a proxy within a model - requires the use of 'get'
assert.strictEqual(dummyChangeset.get('profile.pet.breed'), 'jazzy', 'should not change after execute using get #1');
assert.strictEqual(dummyChangeset.get('profile.pet').breed, 'jazzy', 'should not change after execute using get #2');
assert.strictEqual(dummyChangeset.get('profile').pet.breed, 'jazzy', 'should not change after execute using get #3');

//What did we actually get?
console.log(dummyChangeset.get('profile.pet')); //returns Proxy(ObjectTreeNode)

});
`

## Steps to reproduce

Execute the test above (add to tests/unit/changeset-test.js)

## Expected Behavior

All tests would pass - the setting of the profile surname should have no effect on the breed of the pet

## Actual Behavior
After calling execute(), the value of profile.pet has been set to a Proxy(objectTreeNode) instead of being left unchanged

changes/_changes as logged before execute()
![image](https://github.com/poteto/ember-changeset/assets/16014271/282d9311-b6ea-4025-9ad2-27d233f004b8)

changes/_changes as logged after execute()
![image](https://github.com/poteto/ember-changeset/assets/16014271/bf6bd543-2ab7-4c10-ac10-4aa90e368950)

Note empty 'profile.pet' element in the `_changes`
The value of dummyChangeSet.profile.pet is being set to this 'empty' object, instead of applying the (non-existent) changes to the existing profile.pet object.
As best I can tell this is because BOTH the hasEmberDataProperty() and propertyIsOnObject() are returning false for profile.pet
When logged to the console, dummyChangeset.get('profile.pet') shows:
![image](https://github.com/poteto/ember-changeset/assets/16014271/ae213ed3-c246-4e4b-9d3e-dae0890473b9)

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.