0

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.

3
  • Have you tried with other browsers than PhantomJS? Commented Dec 3, 2014 at 18:40
  • @glepretre yes, the same problem is with chrome for me Commented Dec 5, 2014 at 11:10
  • Could you try changing scope.$watch(attrs.ngModel,... by scope.$watch(model,...? And is the output of your console.logs in your directive OK? Commented Dec 5, 2014 at 14:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.