Skip to content

Commit

Permalink
Test case for validating changeset getter
Browse files Browse the repository at this point in the history
The use case is the following:
I want to be able to validate derived state, not the root state.
So, model contains a bunch of changes, but the condition of validity
is defined based on several changes.

With the regular approach when changes are applied to a model, I can
define a computed property (or a getter) and validate against it.

With the changeset approach, model's computed property won't be updated
because the model itself is not changed. So, the idea is to define a
computed property on the Changeset level. But it isn't even evaluated.

This case has been supported in validated-changeset.
This PR adds a test on the ember-changeset level.
  • Loading branch information
andreyfel committed May 28, 2020
1 parent cd3df19 commit 0f9e301
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,42 @@ module('Unit | Utility | changeset', function(hooks) {
]);
});

test('#validate changeset getter', async function(assert) {
class MyModel {
isOptionOne = false;
isOptionTwo = false;
isOptionThree = false;
}

const Validations = {
isOptionSelected: (newValue) => {
return newValue === true ? true : 'No options selected';
}
}

function myValidator({ key, newValue, oldValue, changes, content }) {
let validatorFn = get(Validations, key);

if (typeof validatorFn === 'function') {
return validatorFn(newValue, oldValue, changes, content);
}
}

const myObject = new MyModel();
const myChangeset = Changeset(myObject, myValidator, Validations);

Object.defineProperty(myChangeset, 'isOptionSelected', {
get() {
return this.get('isOptionOne') || this.get('isOptionTwo') || this.get('isOptionThree');
}
});
await myChangeset.validate();
assert.equal(myChangeset.isInvalid, true, 'Changeset is invalid as none of the options are selected');

set(myChangeset, 'isOptionTwo', true);
await myChangeset.validate();
assert.equal(myChangeset.isInvalid, false, 'Changeset is valid as one of the options is selected');
});

test('#changeset properties do not proxy', async function(assert) {
dummyModel.setProperties({ name: undefined, password: false, async: true, isValid: 'not proxied'});
Expand Down

0 comments on commit 0f9e301

Please sign in to comment.