Skip to content

Commit

Permalink
add test that observers error..validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan committed Mar 4, 2020
1 parent a4a87d0 commit b1da933
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/unit/helpers/changeset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { settled } from '@ember/test-helpers';
import { changeset } from 'ember-changeset-validations/helpers/changeset';
import { module, test } from 'qunit';
import { validatePresence, validateLength } from 'ember-changeset-validations/validators';
import { reads } from '@ember/object/computed';

function validateUnique() {
return (_key, newValue) => {
Expand Down Expand Up @@ -80,6 +81,30 @@ module('Unit | Helper | changeset', function() {
assert.deepEqual(changesetInstance.get('error').username, expectedError, 'username should error');
});

test('it allows to observe changes to validation results on error object', async function(assert) {
let user = EmberObject.extend({ name: null }).create();
let userValidations = {
name: [
validatePresence(true),
],
};
let changesetInstance = changeset([user, userValidations]);

let hostObject = EmberObject.extend({
errorsForName: reads('changeset.error.name.validation'),
}).create({
changeset: changesetInstance,
});

changesetInstance.set('name', 'Max');
assert.equal(changesetInstance.get('error').name, undefined, 'get validation error directly');
assert.equal(hostObject.errorsForName, undefined, 'get validation error through alias on host object');

changesetInstance.set('name', null);
assert.deepEqual(changesetInstance.get('error').name.validation, ["[CUSTOM] Name can't be blank"], 'get validation error directly');
assert.deepEqual(hostObject.errorsForName, ["[CUSTOM] Name can't be blank"], 'get validation error through alias on host object');
});

test('it passes the original object into validators', async function(assert) {
let User = EmberObject.extend({
firstName: null,
Expand Down

0 comments on commit b1da933

Please sign in to comment.