From e28869c63d7befa7c9192847875243584ffac5ae Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Thu, 29 Jan 2015 14:20:39 -0500 Subject: [PATCH] Fix cleanup of resize events --- .../static/js/directives/host-count-graph.js | 7 ++++++- .../static/js/directives/job-status-graph.js | 9 +++++---- .../unit/directives/job-status-graph-test.js | 20 +++++++++++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/awx/ui/static/js/directives/host-count-graph.js b/awx/ui/static/js/directives/host-count-graph.js index debf9b49e5..9b0f99440f 100644 --- a/awx/ui/static/js/directives/host-count-graph.js +++ b/awx/ui/static/js/directives/host-count-graph.js @@ -15,13 +15,18 @@ angular.module('DashboardGraphs'). createGraph(data.hosts, data.license); }); + function onResize() { + if(!license_graph) return; + adjustGraphSize(license_graph, element); + } + angular.element($window).on('resize', function(e) { if(!license_graph) return; adjustGraphSize(license_graph, element); }); element.on('$destroy', function() { - angular.element($window).off('resize', adjustGraphSize); + angular.element($window).off('resize', onResize); }); diff --git a/awx/ui/static/js/directives/job-status-graph.js b/awx/ui/static/js/directives/job-status-graph.js index a56ab2b2d9..1d5f6f3827 100644 --- a/awx/ui/static/js/directives/job-status-graph.js +++ b/awx/ui/static/js/directives/job-status-graph.js @@ -109,15 +109,16 @@ angular.module('DashboardGraphs') }); adjustGraphSize(job_status_chart, element); - } - angular.element($window).on('resize', function() { + function onResize() { adjustGraphSize(job_status_chart, element); - }); + } + + angular.element($window).on('resize', onResize); element.on('$destroy', function() { - angular.element($window).off('resize', adjustGraphSize); + angular.element($window).off('resize', onResize); }); if (scope.removeGraphDataReady) { diff --git a/awx/ui/tests/unit/directives/job-status-graph-test.js b/awx/ui/tests/unit/directives/job-status-graph-test.js index 09628f8e86..18f5596916 100644 --- a/awx/ui/tests/unit/directives/job-status-graph-test.js +++ b/awx/ui/tests/unit/directives/job-status-graph-test.js @@ -1,10 +1,13 @@ describe('Job Status Graph Directive', function() { var element, scope, httpBackend; + var resizeHandler = sinon.spy(); + beforeEach(module('Tower')); beforeEach(module(function($provide) { $provide.value('LoadBasePaths', angular.noop); + $provide.value('adjustGraphSize', resizeHandler); })); beforeEach(inject(function($rootScope, $compile, $httpBackend) { @@ -36,8 +39,9 @@ describe('Job Status Graph Directive', function() { })); afterEach(function() { - httpBackend.verifyNoOutstandingExpectation(); - httpBackend.verifyNoOutstandingRequest(); + element.trigger('$destroy'); + httpBackend.verifyNoOutstandingExpectation(); + httpBackend.verifyNoOutstandingRequest(); }); function filterDataSeries(key, data) { @@ -68,5 +72,17 @@ describe('Job Status Graph Directive', function() { {x: 5, y: 0, series: 1}]); }); + it('cleans up external bindings', function() { + element.trigger('$destroy'); + + resizeHandler.reset(); + + inject(['$window', function($window) { + angular.element($window).trigger('resize'); + }]); + + expect(resizeHandler).not.to.have.been.called; + }); + });