I am trying to write unit test to test my directive. When I invoke keydown event ng-model is not updated. How can I make changes to ng-model from unit test?
My spec file is given below: describe('validNumber', function () {
var scope, elem, compiled, html, usageScenarioModel;
var triggerKeyDown = function (element, keyCode) {
var e = angular.element.Event('keydown');
e.which = keyCode;
element.trigger(e);
};
beforeEach(function(){
html = '<input type="number" valid-number ng-model="values[0].percentage"/>';
inject(function($compile, $rootScope, _model_){
model = _model_;
scope = $rootScope.$new();
elem = angular.element(html);
scope.model = _model_;
scope.model.values = [{percentage:0}]};
compiled = $compile(elem);
compiled(scope);
scope.$digest();
})
});
it('should disable user to type non-numerical value into field', function(){
triggerKeyDown(elem, 50);
scope.$digest();
expect(elem.val()).toBe(2);
});
});
Aucun commentaire:
Enregistrer un commentaire