1

I'm new and doing my first Karma testing and have referred this post for $emit test case. I have some trouble understanding the concept.

This is my code:

ctrl.toggleUserMode = function(pUserId, pUserName, userModeFlag) {
  $scope.$emit('userModeToggleEvent', userModeFlag);
}

Sorry If my question seems silly. Can anyone explain what args, MY_EVENT_ID should I provide in the .toHaveBeenCalledWith method.

spyOn(scope, "$emit")
//run code to test
expect(scope.$emit).toHaveBeenCalledWith("MY_EVENT_ID", other, possible, args);
coderpc
  • 4,119
  • 6
  • 51
  • 93

1 Answers1

0

In your test, you would set up the spy and invoke the code under test, then assert that your event was emitted with whatever arguments the code under test should be using.

// Arrange
spyOn(scope, "$emit");
var pUserId = "test userId";
var pUserName= "test userName";
var userModeFlag = true;

// Act
ctrl.toggleUserMode(pUserId, pUserName, userModeFlag);

// Assert
expect(scope.$emit).toHaveBeenCalledWith("userModeToggleEvent", userModeFlag);
Joe Wilson
  • 5,591
  • 2
  • 27
  • 38