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

Commit

Permalink
chore: ensure tests are gc'd
Browse files Browse the repository at this point in the history
- Ensure event listeners globally on service instantiation are garbage
  collected properly

Closes #5017
Fixes #4996
  • Loading branch information
wesleycho committed Dec 5, 2015
1 parent 8a7ebbf commit 8930a7f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,13 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
}
}

$document.bind('keydown', function(evt) {
$document.on('keydown', keydownListener);

$rootScope.$on('$destroy', function() {
$document.off('keydown', keydownListener);
});

function keydownListener(evt) {
if (evt.isDefaultPrevented()) {
return evt;
}
Expand Down Expand Up @@ -369,7 +375,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap'])
}
}
}
});
}

$modalStack.open = function(modalInstance, modal) {
var modalOpener = $document[0].activeElement,
Expand Down
1 change: 1 addition & 0 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ describe('$uibModal', function () {
body.find('div.modal').remove();
body.find('div.modal-backdrop').remove();
body.removeClass('modal-open');
$document.off('keydown');
});

function triggerKeyDown(element, keyCode, shiftKey) {
Expand Down
7 changes: 6 additions & 1 deletion src/popover/test/popover-html.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('popover', function() {
// load the template
beforeEach(module('uib/template/popover/popover-html.html'));

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

afterEach(function() {
$document.off('keypress');
});

it('should not be open initially', inject(function() {
expect(tooltipScope.isOpen).toBe(false);

Expand Down
10 changes: 8 additions & 2 deletions src/popover/test/popover-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ describe('popover template', function() {
elmBody,
scope,
elmScope,
tooltipScope;
tooltipScope,
$document;

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

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

afterEach(function() {
$document.off('keypress');
});

it('should open on click', inject(function() {
elm.trigger('click');
tooltipScope.$digest();
Expand Down
10 changes: 8 additions & 2 deletions src/popover/test/popover.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ describe('popover', function() {
elmBody,
scope,
elmScope,
tooltipScope;
tooltipScope,
$document;

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

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

beforeEach(inject(function($rootScope, $compile) {
beforeEach(inject(function($rootScope, $compile, _$document_) {
$document = _$document_;
elmBody = angular.element(
'<div><span uib-popover="popover text">Selector Text</span></div>'
);
Expand All @@ -24,6 +26,10 @@ describe('popover', function() {
tooltipScope = elmScope.$$childTail;
}));

afterEach(function() {
$document.off('keypress');
});

it('should not be open initially', inject(function() {
expect(tooltipScope.isOpen).toBe(false);

Expand Down
10 changes: 8 additions & 2 deletions src/tooltip/test/tooltip-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ describe('tooltip template', function() {
elmBody,
scope,
elmScope,
tooltipScope;
tooltipScope,
$document;

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

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

afterEach(function() {
$document.off('keypress');
});

function trigger(element, evt) {
evt = new Event(evt);

Expand Down
4 changes: 4 additions & 0 deletions src/tooltip/test/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('tooltip', function() {
tooltipScope = elmScope.$$childTail;
}));

afterEach(function() {
$document.off('keypress');
});

function trigger(element, evt) {
evt = new Event(evt);

Expand Down
4 changes: 4 additions & 0 deletions src/tooltip/test/tooltip2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe('tooltip directive', function() {
});
});

afterEach(function() {
$document.off('keypress');
});

function compileTooltip(ttipMarkup) {
var fragment = $compile('<div>' + ttipMarkup + '</div>')($rootScope);
$rootScope.$digest();
Expand Down
10 changes: 8 additions & 2 deletions src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
*/
this.$get = ['$window', '$compile', '$timeout', '$document', '$uibPosition', '$interpolate', '$rootScope', '$parse', '$$stackedMap', function($window, $compile, $timeout, $document, $position, $interpolate, $rootScope, $parse, $$stackedMap) {
var openedTooltips = $$stackedMap.createNew();
$document.on('keypress', function(e) {
$document.on('keypress', keypressListener);

$rootScope.$on('$destroy', function() {
$document.off('keypress', keypressListener);
});

function keypressListener(e) {
if (e.which === 27) {
var last = openedTooltips.top();
if (last) {
Expand All @@ -80,7 +86,7 @@ angular.module('ui.bootstrap.tooltip', ['ui.bootstrap.position', 'ui.bootstrap.s
last = null;
}
}
});
}

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

0 comments on commit 8930a7f

Please sign in to comment.