Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
betocantu93 committed Apr 16, 2021
1 parent 907ee21 commit b232cd9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1780,15 +1780,32 @@ module('Unit | Utility | changeset', function (hooks) {
test('observing #rollback values', async function (assert) {
let res;
let changeset = Changeset(dummyModel, dummyValidator);
changeset.addObserver('name', function () {
res = this.name;
//To consistently observe for changes (even for deep keys),
//we must adhere to a never changing value
//so the changeset.data (_content) is the silver bullet for now
//ember-changeset will keep notifying on it
//one downside is that you won't get the changeset as `this` inside the callback.
changeset.data.addObserver('name', function () {
res = changeset.get('name');
});
assert.equal(undefined, changeset.get('name'), 'initial value');
changeset.set('name', 'Jack');
assert.equal('Jack', res, 'observer fired when setting value');
changeset.rollback();
assert.equal(undefined, res, 'observer fired with the value name was rollback to');
});
test('observing deep keys on .data works', async function (assert) {
let res;
let changeset = Changeset(dummyModel, dummyValidator);
changeset.data.addObserver('some.really.nested.path', function () {
res = changeset.get('some.really.nested.path');
});
assert.equal(undefined, changeset.get('some.really.nested.path'), 'initial value');
changeset.set('some.really.nested.path', 'Jack');
assert.equal('Jack', res, 'observer fired when setting value some.really.nested.path');
changeset.rollback();
assert.equal(undefined, res, 'observer fired with the value some.really.nested.path was rollback to');
});

test('can update nested keys after rollback changes.', async function (assert) {
let expectedResult = {
Expand Down

0 comments on commit b232cd9

Please sign in to comment.