Skip to content

Commit

Permalink
test($rootScope): test removal of event listeners during event broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Oct 27, 2017
1 parent 6ad4c8d commit d87a4e4
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,90 @@ describe('Scope', function() {
}));


it('should call next listener after removing the current listener via its own handler', inject(function($rootScope) {
var listener1 = jasmine.createSpy('listener1').and.callFake(function() { remove1(); });
var remove1 = $rootScope.$on('abc', listener1);

var listener2 = jasmine.createSpy('listener2');
var remove2 = $rootScope.$on('abc', listener2);

var listener3 = jasmine.createSpy('listener3');
var remove3 = $rootScope.$on('abc', listener3);

$rootScope.$broadcast('abc');
expect(listener1).toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(listener3).toHaveBeenCalled();

listener1.calls.reset();
listener2.calls.reset();
listener3.calls.reset();

$rootScope.$broadcast('abc');
expect(listener1).not.toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(listener3).toHaveBeenCalled();
}));


it('should call all subsequent listeners when a previous listener is removed via a handler', inject(function($rootScope) {
var listener1 = jasmine.createSpy();
var remove1 = $rootScope.$on('abc', listener1);

var listener2 = jasmine.createSpy().and.callFake(remove1);
var remove2 = $rootScope.$on('abc', listener2);

var listener3 = jasmine.createSpy();
var remove3 = $rootScope.$on('abc', listener3);

$rootScope.$broadcast('abc');
expect(listener1).toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(listener3).toHaveBeenCalled();

listener1.calls.reset();
listener2.calls.reset();
listener3.calls.reset();

$rootScope.$broadcast('abc');
expect(listener1).not.toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(listener3).toHaveBeenCalled();
}));


it('should not call listener when removed by previous', inject(function($rootScope) {
var listener1 = jasmine.createSpy('listener1');
var remove1 = $rootScope.$on('abc', listener1);

var listener2 = jasmine.createSpy('listener2').and.callFake(function() { remove3(); });
var remove2 = $rootScope.$on('abc', listener2);

var listener3 = jasmine.createSpy('listener3');
var remove3 = $rootScope.$on('abc', listener3);

var listener4 = jasmine.createSpy('listener4');
var remove4 = $rootScope.$on('abc', listener4);

$rootScope.$broadcast('abc');
expect(listener1).toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(listener3).not.toHaveBeenCalled();
expect(listener4).toHaveBeenCalled();

listener1.calls.reset();
listener2.calls.reset();
listener3.calls.reset();
listener4.calls.reset();

$rootScope.$broadcast('abc');
expect(listener1).toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(listener3).not.toHaveBeenCalled();
expect(listener4).toHaveBeenCalled();
}));


it('should decrement ancestor $$listenerCount entries', inject(function($rootScope) {
var child1 = $rootScope.$new(),
child2 = child1.$new(),
Expand Down

0 comments on commit d87a4e4

Please sign in to comment.