Building Your Own Directives
$scope.$digest();
expect(modelCtrl.$valid).toBeFalsy();
expect(modelCtrl.$viewValue).toBe(undefined);
});
it('should be valid if the modelValue changes to be the same as
the reference', function() {
modelValue.compareTo = 'different';
$scope.$digest();
expect(modelCtrl.$valid).toBeFalsy();
modelValue.testValue = 'different';
$scope.$digest();
expect(modelCtrl.$valid).toBeTruthy();
expect(modelCtrl.$viewValue).toBe('different');
});
});
Here, we modify the scope, both the model of the input element itself (modelValue.
testValue) and the model of the value with which to compare the input
(modelValue.compareTo). Then we test the validity of the input (modelCtl). We have
to call $digest() to ensure that the input has been updated from the model changes.
describe('input value changes', function() {
it('should be invalid if the input value changes', function() {
modelCtrl.$setViewValue('different');
expect(modelCtrl.$valid).toBeFalsy();
expect(modelValue.testValue).toBe(undefined);
});
it('should be valid if the input value changes to be the same as
the reference', function() {
modelValue.compareTo = 'different';
$scope.$digest();
expect(modelCtrl.$valid).toBeFalsy();
modelCtrl.$setViewValue('different');
expect(modelCtrl.$viewValue).toBe('different');
expect(modelCtrl.$valid).toBeTruthy();
});
});
});
Here we modify the input value, by calling $setViewValue(), which is what
happens if the user types or pastes into the input box.