Skip to content

Commit

Permalink
Failing test case for validating computed property
Browse files Browse the repository at this point in the history
Not sure if it is supposed to work, but this PR illustrates the issue.

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.
  • Loading branch information
andreyfel committed May 18, 2020
1 parent 0699822 commit 6dd3acb
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tests/unit/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { settled } from '@ember/test-helpers';
import { module, test } from 'qunit';

import EmberObject, {
defineProperty,
get,
set,
setProperties,
} from '@ember/object';

import { reads } from '@ember/object/computed';
import { or, reads } from '@ember/object/computed';
import ObjectProxy from '@ember/object/proxy';
import { dasherize } from '@ember/string';
import { isPresent } from '@ember/utils';
Expand Down Expand Up @@ -1687,6 +1688,37 @@ module('Unit | Utility | changeset', function(hooks) {
]);
});

test('validate computed property', 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);

defineProperty(myChangeset, 'isOptionSelected', or('isOptionOne', 'isOptionTwo', 'isOptionThree'));
myChangeset.validate();
assert.equal(myChangeset.isInvalid, true, 'Changeset is invalid as none of the options are selected');
set(myChangeset, 'isOptionTwo', true);

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 6dd3acb

Please sign in to comment.