[Tests] Fixed tests to make angular Upgrade pass

This commit is contained in:
Khalah Jones-Golden 2015-10-20 15:22:04 -04:00
parent 14920c9274
commit 404694fe6b
4 changed files with 5 additions and 182 deletions

View file

@ -237,7 +237,7 @@ describe('timepicker directive', function () {
expect(button.length).to.be(0);
// Make the form invalid
$scope.relative.count = 'foo';
$scope.relative.count = -3;
$scope.formatRelative();
$scope.$digest();
@ -263,12 +263,12 @@ describe('timepicker directive', function () {
expect(checkbox.length).to.be(1);
// Rounding is disabled by default
expect(checkbox.attr('checked')).to.be(undefined);
expect(checkbox.prop('checked')).to.be(false);
// Enable rounding
$scope.relative.round = true;
$scope.$digest();
expect(checkbox.attr('checked')).to.be('checked');
expect(checkbox.prop('checked')).to.be(true);
done();
});
@ -344,13 +344,11 @@ describe('timepicker directive', function () {
// Should update the selected option
var i = 0;
_.each($scope.units, function (longUnit, shortUnit) {
$scope.relative.unit = shortUnit;
$scope.$digest();
expect(select.val()).to.be(i.toString());
i++;
expect(select.val().split(':')[1]).to.be(shortUnit);
});
done();

View file

@ -33,7 +33,7 @@ var init = function () {
this.options = options;
}
PersistedLogMock.prototype.add = sinon.stub();
PersistedLogMock.prototype.add = sinon.stub().returns(typeaheadItems);
PersistedLogMock.prototype.get = sinon.stub().returns(typeaheadItems);
return PersistedLogMock;

View file

@ -1,135 +0,0 @@
define(function (require) {
var _ = require('lodash');
var angular = require('angular');
var PRISTINE_CLASS = 'ng-pristine';
var DIRTY_CLASS = 'ng-dirty';
var UNTOUCHED_CLASS = 'ng-untouched';
var TOUCHED_CLASS = 'ng-touched';
// http://goo.gl/eJofve
var nullFormCtrl = {
$addControl: _.noop,
$removeControl: _.noop,
$setValidity: _.noop,
$setDirty: _.noop,
$setPristine: _.noop
};
/**
* Extension of Angular's NgModelController class
* that ensures models are marked "dirty" after
* they move from an invalid state to valid.
*
* @param {$scope} $scope
*/
function KbnModelController($scope, $element, $animate) {
var ngModel = this;
// verify that angular works the way we are assuming it does
if (angular.version.full !== '1.4.0') {
throw new Error('angular version has updated but KbnModelController has not!');
}
/**
* Get the form a model belongs to
*
* @return {NgFormController} - the parent controller of a noop controller
*/
ngModel.$getForm = function () {
return $element.inheritedData('$formController') || nullFormCtrl;
};
/**
* Update the ngModel to be "dirty" if it is pristine.
*
* @return {undefined}
*/
ngModel.$setDirty = function () {
ngModel.$setTouched();
$$setDirty();
};
function $$setDirty() {
if (ngModel.$dirty) return;
ngModel.$dirty = true;
ngModel.$pristine = false;
$animate.removeClass($element, PRISTINE_CLASS);
$animate.addClass($element, DIRTY_CLASS);
ngModel.$getForm().$setDirty();
}
ngModel.$setTouched = toggleTouched(true);
ngModel.$setUntouched = toggleTouched(false);
function toggleTouched(val) {
return function () {
if (ngModel.$touched === val) return;
ngModel.$touched = val;
ngModel.$untouched = !val;
$animate.addClass($element, val ? TOUCHED_CLASS : UNTOUCHED_CLASS);
$animate.removeClass($element, val ? UNTOUCHED_CLASS : TOUCHED_CLASS);
};
}
/**
* While the model is pristine, ensure that the model
* gets set to dirty if it becomes invalid. If the model
* becomes dirty of other reasons stop watching and
* waitForPristine()
*
* @return {undefined}
*/
function watchForDirtyOrInvalid() {
var unwatch = $scope.$watch(get, react);
function get() {
return ngModel.$dirty || ngModel.$invalid;
}
function react(is, was) {
if (is === was) return;
unwatch();
waitForPristine();
$$setDirty();
}
}
/**
* Once a model becomes dirty, there is no longer a need
* for a watcher. Instead, we will react to the $setPristine
* method being called. This is the only way for a model to go
* from dirty -> pristine.
*
* @return {[type]} [description]
*/
function waitForPristine() {
var fn = ngModel.$setPristine;
ngModel.$setPristine = function () {
var ret = fn.apply(this, arguments);
if (ngModel.$pristine) {
ngModel.$setPristine = fn;
watchForDirtyOrInvalid();
}
return ret;
};
}
ngModel.$setUntouched();
$element.one('blur', function () {
ngModel.$setTouched();
$scope.$apply();
});
$scope.$on('$destroy', function () {
$element.off('blur', ngModel.$setTouched);
});
// wait for child scope to init before watching validity
$scope.$evalAsync(function () {
if (ngModel.$dirty) waitForPristine();
else watchForDirtyOrInvalid();
});
}
return KbnModelController;
});

View file

@ -59,44 +59,4 @@ describe('fancy forms', function () {
expect(ngForm.describeErrors()).to.be('1 Error');
});
});
describe('ngModelController', function () {
it('gives access to the ngFormController', function () {
expect(ngModel.$getForm()).to.be(ngForm);
});
it('allows setting the model dirty', function () {
expect($el.find('input.ng-dirty')).to.have.length(0);
ngModel.$setDirty();
expect($el.find('input.ng-dirty')).to.have.length(1);
});
it('sets the model dirty when it moves from valid to invalid', function () {
// clear out the old scope/el
$scope.$destroy();
$el = generateEl();
$scope = $rootScope.$new();
// start with a valid value
$scope.val = 'something';
$compile($el)($scope);
$rootScope.$apply();
// ensure that the field is valid and pristinve
var $valid = $el.find('input.ng-valid');
expect($valid).to.have.length(1);
expect($valid.hasClass('ng-pristine')).to.be(true);
expect($valid.hasClass('ng-dirty')).to.be(false);
// remove the value without actually setting the view model
$scope.val = null;
$rootScope.$apply();
// ensure that the field is now invalid and dirty
var $invalid = $el.find('input.ng-invalid');
expect($invalid).to.have.length(1);
expect($valid.hasClass('ng-pristine')).to.be(false);
expect($valid.hasClass('ng-dirty')).to.be(true);
});
});
});