Skip to content

Commit

Permalink
Tests added for revalidators
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan Graham Walker committed Mar 7, 2016
1 parent 3921fdd commit dcb5322
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 73 deletions.
12 changes: 8 additions & 4 deletions addon/mixins/components/conditional-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export default Ember.Mixin.create({
}
},

willDestroyElement() {
willDestroy() {
this._super(...arguments);
this._removeRevalidationObservers();
},

willInsertElement() {
init() {
this._super(...arguments);
this._revalidate();
},
Expand All @@ -29,13 +29,17 @@ export default Ember.Mixin.create({
Ember.assert('No validate() method detected. You must use the conditional validations mixin with the form mixin.', validateExists);

this.forEachRevalidator((property) => {
this.addObserver(property, this.validate);
this.addObserver(property, () => {
this.validate();
});
});
},

_removeRevalidationObservers() {
this.forEachRevalidator((property) => {
this.removeObserver(property, this.validate);
this.removeObserver(property, () => {
this.validate();
});
});
},
});
49 changes: 42 additions & 7 deletions tests/unit/mixins/components/conditional-validations-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import Ember from 'ember';
import ControllersConditionalValidationsMixin from 'ember-easy-form-extensions/mixins/components/conditional-validations';
import ComponentsConditionalValidationsMixin from 'ember-easy-form-extensions/mixins/components/conditional-validations';
import ComponentsFormMixin from 'ember-easy-form-extensions/mixins/components/form';
import { module, test } from 'qunit';

module('Unit | Mixin | controllers/conditional validations');
let subject;

module('Unit | Mixin | components/conditional validations', {

beforeEach() {
const ComponentsConditionalValidationsObject = Ember.Object.extend(
ComponentsFormMixin,
ComponentsConditionalValidationsMixin, {

bananas: null,
revalidateFor: ['bananas'],
});

subject = ComponentsConditionalValidationsObject.create();
},

});

test('Calls to validate', function(assert) {
const done = assert.async();

assert.expect(3);

subject.validate = function() {
assert.ok(true, 'Validate should be called when bananas changes');
}

assert.ok(subject.hasObserverFor('bananas'), 'Should add observers on init');

subject.set('bananas', true);

Ember.run(this, function() {
subject.destroy();


Ember.run.next(this, function() {
assert.notOk(subject.hasObserverFor('bananas'), 'Should remove observers on destroy');
done();
});
});

// Replace this with your real tests.
test('it works', function(assert) {
let ControllersConditionalValidationsObject = Ember.Object.extend(ControllersConditionalValidationsMixin);
let subject = ControllersConditionalValidationsObject.create();
assert.ok(subject);
});
62 changes: 0 additions & 62 deletions tests/unit/mixins/old-controllers/conditional-validations-test.js

This file was deleted.

0 comments on commit dcb5322

Please sign in to comment.