Skip to content

Commit

Permalink
test($rootScope): test removal of event listerns during event broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 9, 2017
1 parent 21a2f4b commit 4b105c2
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,98 @@ describe('Scope', function() {
}));


// See issue https://github.com/angular/angular.js/issues/16135
it('should deallocate the listener array entry', inject(function($rootScope) {
var remove1 = $rootScope.$on('abc', noop);
$rootScope.$on('abc', noop);

expect($rootScope.$$listeners['abc'].length).toBe(2);
expect(0 in $rootScope.$$listeners['abc']).toBe(true);

remove1();

expect($rootScope.$$listeners['abc'].length).toBe(2);
expect(0 in $rootScope.$$listeners['abc']).toBe(false);
}));


it('should call next listener when removing current', inject(function($rootScope) {
var remove1 = $rootScope.$on('abc', function() { remove1() });

var listener2 = jasmine.createSpy();
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 call all listeners when removing previous', 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();
var remove1 = $rootScope.$on('abc', listener1);

var listener2 = jasmine.createSpy().and.callFake(function() { remove3(); });
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).not.toHaveBeenCalled();

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

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


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

0 comments on commit 4b105c2

Please sign in to comment.