How to unit test spy on $ emit in a directive?

How to check if there was a call to $ emit in the unit test directive?

My directive is pretty simple (for the purpose of illustration):

angular.module('plunker', [])
.directive('uiList', [function () { 
    return {
        scope: {
            lengthModel: '=uiList',
        },
        link: function (scope, elm, attrs) {
            scope.$watch('lengthModel', function (newVal) {
                        scope.$emit('yolo');
            });
        }
    }
}])

therefore, every time the uiList attribute is changed, it throws an event.

And I have unit test code as follows:

describe('Testing $emit in directive', function() {
  var scope;
  var element;


  //you need to indicate your module in a test
    beforeEach(function () {
        module('plunker');
        inject(function ($rootScope, $compile) {
            scope = $rootScope.$new();
            scope.row= 1;
            spyOn(scope,'$emit');
        element = angular.element('<ul id="rows" ui-list="row">');
        $compile(element)(scope);
        });
    });

  it('should emit', function() {

    scope.$digest();
    scope.row = 2;
    scope.$digest();
    expect(scope.$emit).toHaveBeenCalledWith("yolo");
  });
});

It always gave me an error indicating that the area. $ emit has never been called. Is there something wrong in the field? Can anybody help?

Plunker: http://plnkr.co/edit/AqhPwp?p=preview

+4
source share
2 answers

An isolated area is created in your directive that calls $emit, so you need to look into it :)

describe('Testing $emit in directive', function() {
  var scope;
  var element;
  var elementScope;

  beforeEach(module('plunker'));

  beforeEach(inject(function($rootScope, $compile) {
      scope = $rootScope.$new();
      scope.row = 1;
      element = angular.element('<ul id="rows" ui-list="row">');
      $compile(element)(scope);
      scope.$digest();
      elementScope = element.scope();
  }));

  it('should emit', function() {
    // arrange
    spyOn(elementScope, '$emit');

    // act
    scope.$apply(function() {
      scope.row = 2;
    });

    // assert
    expect(elementScope.$emit).toHaveBeenCalledWith("yolo");
  });
});

plunker :)

+7

, glepetre ( , ). . , $new , , , element.scope() - . $id. , , element.scope(). , , .

:

    beforeEach(inject(function () {
        scope = $rootScope.$new();

        scope.$apply(function () {
            scope.field = null;
            scope.name = 'nolength';
        });

        spyOn(scope, '$new').and.returnValue(scope);
        spyOn(scope, '$emit');

        var element = angular.element('<my-directive name="name" field="field" title="\'Title\'" ></my-directive>');
        noLengthValidation = $compile(element)(scope);

        scope.$apply();
        elementScope = element.scope();
    }));

, , $new scope, , ! :

    it('The directive should not react on string length if no max-chars attribute presented', function () {
        scope.$apply();
        noLengthValidation.isolateScope().field = 'fieldisssssssveeeeerryyyyyylooooooonnnngggggggggggggggggggggggggggg';
        scope.$apply();
        expect(scope.$emit).toHaveBeenCalledWith('ON_MORE_AWASOME_EVENT', 'nolength');
    });

, $emit. :

        function validate() {
            scope.validate = 1;
            if (scope.field) {
                if (!isValid()) {
                    scope.$emit('ON_AWASOME_EVENT', {code : scope.name, text : 'Field ' + scope.title + '  is feels bad'});
                } else {
                    scope.$emit('ON_MORE_AWASOME_EVENT', scope.name);
                }

            }
        }
0

All Articles