mardi 12 avril 2016

Spy on/ test if $interval was canceled

I am not able to spy on $interval cancel in my unit test. I am getting inside of the event listener on my else statement, but seem to be missing something else for my test to pass.

Here is the complete function. I am testing the else statment

if ($attrs.ngModel != null) {
        $scope.$watch($attrs.ngModel, function () {
            tail($el);
        });
    } else {
        var interval = $interval(function () {
            tail($el);
        }, 500, false);
        $scope.$on('$destroy', function () {
            console.log('inside')   /// *** I am reaching this ****
            $interval.cancel(interval);
        });
    }

On my unit tests I mocked ngModel to be null and created a spy in a beforeEach using sinon like so

 beforeEach(function() {
        sinon.spy($interval, 'cancel');
    });

In my test I am making sure to compile the element since $interval.cancel exists on an event listener. I am listening on $destroy but that is also not changing emitOccur to true.

it("should set emitOccur to true on $destroy", function () {
        html = angular.element("<input ha-tail>");
        element = $compile(html)($rootScope);

        var emitOccur = false;

        $rootScope.$on('$destroy', function () {
            emitOccur = true;
        });
        $interval.flush(4000);

        $rootScope.$apply();

        //expect(emitOccur).to.be.true;
        expect($interval.cancel).to.have.been.calledOnce;
    });

This test seems to need something additional but I am not sure what it is.

Aucun commentaire:

Enregistrer un commentaire