well, I'm stuck on testing my directive:
angular.module('fooApp')
.directive('numericbinding', function () {
return {
restrict: 'A',
require: 'ngModel',
scope: {
model: '=ngModel',
},
link: function (scope, element, attrs) {
scope.$watch(attrs.ngModel, function () {
if (!scope.model) {
// inits with default val
scope.model = 0;
} else if (scope.model && typeof scope.model === 'string') {
// console.log('value changed, new value is: ' + (typeof scope.model));
scope.model = parseInt(scope.model);
// console.log('value changed, new value is: ' + (typeof scope.model));
}
});
}
};
});
Unit test:
'use strict';
describe('Directive: numericbinding', function () {
var $compile;
var $rootScope;
beforeEach(module('fooApp'));
beforeEach(inject(function(_$compile_, _$rootScope_, $httpBackend){
$rootScope = _$rootScope_;
$compile = _$compile_;
$compile('<form name="form1"><input numericbinding ng-model="someModel.Property" name="data"></input></form>')($rootScope);
}));
it('converts string value to integer', inject(function () {
$rootScope.someModel.Property = '10';
// $rootScope.form1.data.$setViewValue('10')
$rootScope.$digest();
expect($rootScope.someModel.Property).toEqual(10);
}));
}
Fails with msg:
PhantomJS 1.9.7 (Linux) Directive: numericbinding converts string value to integer FAILED
Expected '10' to equal 10.
I've tested manually and string to integer conversion works OK. Still this particular test keeps failing.
Any idea what's wrong with that one?
UPDATE:
debugging in the chrome browser shows, that:
scope.$watch(attrs.ngModel, function () {
is not triggered when I call: $rootScope.$digest(); in my test. I even tried with: $rootScope.$apply(); but still, no change here.
scope.$watch(attrs.ngModel,...byscope.$watch(model,...? And is the output of yourconsole.logs in your directive OK?