2

I am just wondering how can I test the handleAddClientBroadcast event?

I have a navigation service like so:

angular.module("ruleManagement.services")
    .factory('navigationService', function ($rootScope) {

        var navigationService = {};

        navigationService.prepForBroadcast = function() {
          this.broadCastIsAddClientItem();
        };

        navigationService.broadCastIsAddClientItem = function() {
          $rootScope.$broadcast('handleAddClientBroadcast');
        };

        return navigationService;
    });

I inject this navigation service into my clientsCtrl and catch the handleAddClientBroadcast like so:

$scope.$on('handleAddClientBroadcast', function () {
  $scope.clientModel = {
    id: 0,
    name: "",
    description: "",
    rules: []
  };

  var lastClient = _.findLast($scope.clients);

  if (typeof lastClient == 'undefined' || lastClient == null) {
    lastClient = $scope.clientModel;
  }

  $scope.clientModel.id = lastClient.id + 1;
  $scope.clients.push($scope.clientModel);
});

Thanks.

2

1 Answer 1

8

Assuming you're using Jasmine

spyOn($rootScope, '$broadcast').andCallThrough();

...

expect($rootScope.$broadcast).toHaveBeenCalledWith('eventName');
Sign up to request clarification or add additional context in comments.

1 Comment

It later versions of jasmine it is spyOn(scope, '$broadcast').and.callThrough();

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.