Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 8930a7f

Browse files
committed
chore: ensure tests are gc'd
- Ensure event listeners globally on service instantiation are garbage collected properly Closes #5017 Fixes #4996
1 parent 8a7ebbf commit 8930a7f

9 files changed

+55
-11
lines changed

Diff for: src/modal/modal.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,13 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
331331
}
332332
}
333333

334-
$document.bind('keydown', function(evt) {
334+
$document.on('keydown', keydownListener);
335+
336+
$rootScope.$on('$destroy', function() {
337+
$document.off('keydown', keydownListener);
338+
});
339+
340+
function keydownListener(evt) {
335341
if (evt.isDefaultPrevented()) {
336342
return evt;
337343
}
@@ -369,7 +375,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
369375
}
370376
}
371377
}
372-
});
378+
}
373379

374380
$modalStack.open = function(modalInstance, modal) {
375381
var modalOpener = $document[0].activeElement,

Diff for: src/modal/test/modal.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ describe('$uibModal', function () {
154154
body.find('div.modal').remove();
155155
body.find('div.modal-backdrop').remove();
156156
body.removeClass('modal-open');
157+
$document.off('keydown');
157158
});
158159

159160
function triggerKeyDown(element, keyCode, shiftKey) {

Diff for: src/popover/test/popover-html.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('popover', function() {
1111
// load the template
1212
beforeEach(module('uib/template/popover/popover-html.html'));
1313

14-
beforeEach(inject(function($rootScope, $compile, $sce) {
14+
beforeEach(inject(function($rootScope, $compile, $sce, _$document_) {
15+
$document = _$document_;
1516
elmBody = angular.element(
1617
'<div><span uib-popover-html="template">Selector Text</span></div>'
1718
);
@@ -25,6 +26,10 @@ describe('popover', function() {
2526
tooltipScope = elmScope.$$childTail;
2627
}));
2728

29+
afterEach(function() {
30+
$document.off('keypress');
31+
});
32+
2833
it('should not be open initially', inject(function() {
2934
expect(tooltipScope.isOpen).toBe(false);
3035

Diff for: src/popover/test/popover-template.spec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ describe('popover template', function() {
33
elmBody,
44
scope,
55
elmScope,
6-
tooltipScope;
6+
tooltipScope,
7+
$document;
78

89
// load the popover code
910
beforeEach(module('ui.bootstrap.popover'));
@@ -16,7 +17,8 @@ describe('popover template', function() {
1617
$templateCache.put('myUrl', [200, '<span>{{ myTemplateText }}</span>', {}]);
1718
}));
1819

19-
beforeEach(inject(function($rootScope, $compile) {
20+
beforeEach(inject(function($rootScope, $compile, _$document_) {
21+
$document = _$document_;
2022
elmBody = angular.element(
2123
'<div><span uib-popover-template="templateUrl">Selector Text</span></div>'
2224
);
@@ -31,6 +33,10 @@ describe('popover template', function() {
3133
tooltipScope = elmScope.$$childTail;
3234
}));
3335

36+
afterEach(function() {
37+
$document.off('keypress');
38+
});
39+
3440
it('should open on click', inject(function() {
3541
elm.trigger('click');
3642
tooltipScope.$digest();

Diff for: src/popover/test/popover.spec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ describe('popover', function() {
33
elmBody,
44
scope,
55
elmScope,
6-
tooltipScope;
6+
tooltipScope,
7+
$document;
78

89
// load the popover code
910
beforeEach(module('ui.bootstrap.popover'));
1011

1112
// load the template
1213
beforeEach(module('uib/template/popover/popover.html'));
1314

14-
beforeEach(inject(function($rootScope, $compile) {
15+
beforeEach(inject(function($rootScope, $compile, _$document_) {
16+
$document = _$document_;
1517
elmBody = angular.element(
1618
'<div><span uib-popover="popover text">Selector Text</span></div>'
1719
);
@@ -24,6 +26,10 @@ describe('popover', function() {
2426
tooltipScope = elmScope.$$childTail;
2527
}));
2628

29+
afterEach(function() {
30+
$document.off('keypress');
31+
});
32+
2733
it('should not be open initially', inject(function() {
2834
expect(tooltipScope.isOpen).toBe(false);
2935

Diff for: src/tooltip/test/tooltip-template.spec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ describe('tooltip template', function() {
33
elmBody,
44
scope,
55
elmScope,
6-
tooltipScope;
6+
tooltipScope,
7+
$document;
78

89
// load the popover code
910
beforeEach(module('ui.bootstrap.tooltip'));
@@ -15,7 +16,8 @@ describe('tooltip template', function() {
1516
$templateCache.put('myUrl', [200, '<span>{{ myTemplateText }}</span>', {}]);
1617
}));
1718

18-
beforeEach(inject(function($rootScope, $compile) {
19+
beforeEach(inject(function($rootScope, $compile, _$document_) {
20+
$document = _$document_;
1921
elmBody = angular.element(
2022
'<div><span uib-tooltip-template="templateUrl">Selector Text</span></div>'
2123
);
@@ -30,6 +32,10 @@ describe('tooltip template', function() {
3032
tooltipScope = elmScope.$$childTail;
3133
}));
3234

35+
afterEach(function() {
36+
$document.off('keypress');
37+
});
38+
3339
function trigger(element, evt) {
3440
evt = new Event(evt);
3541

Diff for: src/tooltip/test/tooltip.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describe('tooltip', function() {
2626
tooltipScope = elmScope.$$childTail;
2727
}));
2828

29+
afterEach(function() {
30+
$document.off('keypress');
31+
});
32+
2933
function trigger(element, evt) {
3034
evt = new Event(evt);
3135

Diff for: src/tooltip/test/tooltip2.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ describe('tooltip directive', function() {
3737
});
3838
});
3939

40+
afterEach(function() {
41+
$document.off('keypress');
42+
});
43+
4044
function compileTooltip(ttipMarkup) {
4145
var fragment = $compile('<div>' + ttipMarkup + '</div>')($rootScope);
4246
$rootScope.$digest();

Diff for: src/tooltip/tooltip.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
7171
*/
7272
this.$get = ['$window', '$compile', '$timeout', '$document', '$uibPosition', '$interpolate', '$rootScope', '$parse', '$$stackedMap', function($window, $compile, $timeout, $document, $position, $interpolate, $rootScope, $parse, $$stackedMap) {
7373
var openedTooltips = $$stackedMap.createNew();
74-
$document.on('keypress', function(e) {
74+
$document.on('keypress', keypressListener);
75+
76+
$rootScope.$on('$destroy', function() {
77+
$document.off('keypress', keypressListener);
78+
});
79+
80+
function keypressListener(e) {
7581
if (e.which === 27) {
7682
var last = openedTooltips.top();
7783
if (last) {
@@ -80,7 +86,7 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
8086
last = null;
8187
}
8288
}
83-
});
89+
}
8490

8591
return function $tooltip(ttType, prefix, defaultTriggerShow, options) {
8692
options = angular.extend({}, defaultOptions, globalOptions, options);

0 commit comments

Comments
 (0)