I hope this is a simple question.
I have a select-box directive that should close when anything is clicked outside of the directive. So the window has a an event listener to hide the box once a click event is heard-
$window.addEventListener('click', function() {
$scope.expanded = false;
$scope.$apply();
});
Which works fine, no complaints. But I'm trying to write a unit test for this and can't figure out how to trigger the click even on the window itself.
it('should close the dropdown if the window is clicked', function() {
toggleButton.trigger('click');
expect(eleSimpleOptionBox.hasClass('ng-hide')).toBe(false);
$window.trigger('click');
expect(eleSimpleOptionBox.hasClass('ng-hide')).toBe(true);
});
I've tried the following-
$window.trigger('click'); // as well as .click()
$window.triggerHandler('click');
angular.element('body').trigger('click'); // as well as .click()
someWrappingElement.trigger('click'); // as well as .click()
$digeston your scope or$rootScopeafter triggering the event?$digestso I assume the second would. It's using the DOMs event listeners and not angulars event listeners.