Skip to content

Commit

Permalink
Failing test case for validating computed property (#482)
Browse files Browse the repository at this point in the history
* Test case for validating changeset getter

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.

* Bump validated-changeset to 0.5.11

The issue which this PR illustrates was fixed in
validated-changeset@0.5.11
  • Loading branch information
andreyfel authored May 28, 2020
1 parent cd3df19 commit eb1bbc7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 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 @@
"ember-auto-import": "^1.5.2",
"@glimmer/tracking": "^1.0.0",
"ember-cli-babel": "^7.19.0",
"validated-changeset": "~0.5.10"
"validated-changeset": "~0.5.11"
},
"devDependencies": {
"@ember/optional-features": "^1.0.0",
Expand Down
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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12777,10 +12777,10 @@ validate-npm-package-name@~2.2.2:
dependencies:
builtins "0.0.7"

validated-changeset@~0.5.10:
version "0.5.10"
resolved "https://registry.yarnpkg.com/validated-changeset/-/validated-changeset-0.5.10.tgz#4fbf68f64c6d84cac6f03b531aaff6b5aa24a35b"
integrity sha512-xPu81ng9WH1ITMcoiS9qWtGW07d97nhQguDIEBIuFG9tDE/JJyZvzCOewuY1UsQ6CRoLngLE/asmnp3BnrGe6w==
validated-changeset@~0.5.11:
version "0.5.11"
resolved "https://registry.yarnpkg.com/validated-changeset/-/validated-changeset-0.5.11.tgz#f6fe33af8320125d82f1b2f9599fad9188097c3f"
integrity sha512-ZGaaIHjGfb5yn4DbIxF0gTzGDyq0+hDJgAaQBSx8gLTUXnKlcN/wDlEhBcmRQMiNOS/oHQlN5At+0TNXIpcuWQ==

vary@~1.1.2:
version "1.1.2"
Expand Down

0 comments on commit eb1bbc7

Please sign in to comment.