jeudi 31 décembre 2015

writing a unit test for an angular directive that requires ng-model

I am new to unit testing and trying to figure out how to set it all up. I have the following directive:

(function () {
    'use strict';

    angular
        .module('hntb-utils')
        .directive('notZero', notZero);

    //notZero.$inject = [];

    function notZero() {
        return {
            require: 'ngModel',
            link: link,
            restrict: 'A',
            scope: '='
        };

        function link(scope, element, attributes, ngModelCtrl) {
            ngModelCtrl.$validators.notZero = function (value) {
                return value != 0;
            }
        }
    }

})();

This directive (if added as an attribute not-zero to an element that also uses ng-model) ensures that the model value is not zero by adding a validator to the ngModelController.

I am trying to figure out how to test this directive. I figure I should probably have something like:

it("should ensure model is not zero", function () {
...
});

I am just really stuck on the implementation. I know I should use angular-mocks but how? What do I mock up?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire