lundi 28 décembre 2015

How to test a directive which uses ngModel on same element

I have a directive which requires ngModel. In its link function it calls $setViewValue on the ngModel controller. I want to test that $setViewValue is being called under the right conditions, but I can't work out how to add my spy...

Here is my simplified code:

directive

angular.module("myApp").directive("myDir", function()
{
    return {
        restrict: "A",
        require: ["ngModel"],
        link: function(scope, element, attrs, ctrls)
        {
            var ngModel = ctrls[0];

            ......

            function changeHandler(event) {
                ngModel.$setViewValue(ckeditor.getData());
            }
        }
    };
});

html:

<textarea my-dir ng-model="myData"></textarea>

and I want my test to be something like:

describe("updating text in ckeditor", function() {
        it("should update model value", function() {
                $compile("<textarea my-dir ng-model='text'></textarea>")(scope);
                spyOn(ngModel, "$setViewValue");
                // trigger change handler
                expect(ngModel.$setViewValue).toHaveBeenCalled();
        });
});

Thanks!

Aucun commentaire:

Enregistrer un commentaire