From 07272608d8ae39a5dd220cdb5637b1439342d5c3 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 10 Oct 2013 09:11:21 +0100 Subject: fix(modules): stop leaking global variables in tests The routeUtils.js file was declaring a number of functions that were leaking into other modules such as ngMocks causing tests to pass incorrectly. Closes #4360 --- src/ngMock/angular-mocks.js | 2 +- src/ngRoute/route.js | 42 ++++++++++++++++++++---------------- src/ngRoute/routeUtils.js | 17 --------------- test/helpers/matchers.js | 12 +++++------ test/helpers/privateMocksSpec.js | 2 +- test/ngMock/angular-mocksSpec.js | 2 +- test/ngRoute/directive/ngViewSpec.js | 22 +++++++++---------- test/ngRoute/routeSpec.js | 24 ++++++++++----------- 8 files changed, 55 insertions(+), 68 deletions(-) delete mode 100644 src/ngRoute/routeUtils.js diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 3592aacd..05551ea7 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -769,7 +769,7 @@ angular.mock.animate = angular.module('mock.animate', ['ng']) } }; - forEach(['enter','leave','move','addClass','removeClass'], function(method) { + angular.forEach(['enter','leave','move','addClass','removeClass'], function(method) { animate[method] = function() { var params = arguments; animate.queue.push({ diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index 8a6dacd4..50e2405a 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -28,6 +28,10 @@ var ngRouteModule = angular.module('ngRoute', ['ng']). * Requires the {@link ngRoute `ngRoute`} module to be installed. */ function $RouteProvider(){ + function inherit(parent, extra) { + return angular.extend(new (angular.extend(function() {}, {prototype:parent}))(), extra); + } + var routes = {}; /** @@ -126,7 +130,7 @@ function $RouteProvider(){ * Adds a new route definition to the `$route` service. */ this.when = function(path, route) { - routes[path] = extend( + routes[path] = angular.extend( {reloadOnSearch: true}, route, path && pathRegExp(path, route) @@ -138,7 +142,7 @@ function $RouteProvider(){ ? path.substr(0, path.length-1) : path +'/'; - routes[redirectPath] = extend( + routes[redirectPath] = angular.extend( {redirectTo: path}, pathRegExp(redirectPath, route) ); @@ -460,9 +464,9 @@ function $RouteProvider(){ last = $route.current; if (next && last && next.$$route === last.$$route - && equals(next.pathParams, last.pathParams) && !next.reloadOnSearch && !forceReload) { + && angular.equals(next.pathParams, last.pathParams) && !next.reloadOnSearch && !forceReload) { last.params = next.params; - copy(last.params, $routeParams); + angular.copy(last.params, $routeParams); $rootScope.$broadcast('$routeUpdate', last); } else if (next || last) { forceReload = false; @@ -470,7 +474,7 @@ function $RouteProvider(){ $route.current = next; if (next) { if (next.redirectTo) { - if (isString(next.redirectTo)) { + if (angular.isString(next.redirectTo)) { $location.path(interpolate(next.redirectTo, next.params)).search(next.params) .replace(); } else { @@ -483,29 +487,29 @@ function $RouteProvider(){ $q.when(next). then(function() { if (next) { - var locals = extend({}, next.resolve), + var locals = angular.extend({}, next.resolve), template, templateUrl; - forEach(locals, function(value, key) { - locals[key] = isString(value) ? $injector.get(value) : $injector.invoke(value); + angular.forEach(locals, function(value, key) { + locals[key] = angular.isString(value) ? $injector.get(value) : $injector.invoke(value); }); - if (isDefined(template = next.template)) { - if (isFunction(template)) { + if (angular.isDefined(template = next.template)) { + if (angular.isFunction(template)) { template = template(next.params); } - } else if (isDefined(templateUrl = next.templateUrl)) { - if (isFunction(templateUrl)) { + } else if (angular.isDefined(templateUrl = next.templateUrl)) { + if (angular.isFunction(templateUrl)) { templateUrl = templateUrl(next.params); } templateUrl = $sce.getTrustedResourceUrl(templateUrl); - if (isDefined(templateUrl)) { + if (angular.isDefined(templateUrl)) { next.loadedTemplateUrl = templateUrl; template = $http.get(templateUrl, {cache: $templateCache}). then(function(response) { return response.data; }); } } - if (isDefined(template)) { + if (angular.isDefined(template)) { locals['$template'] = template; } return $q.all(locals); @@ -516,7 +520,7 @@ function $RouteProvider(){ if (next == $route.current) { if (next) { next.locals = locals; - copy(next.params, $routeParams); + angular.copy(next.params, $routeParams); } $rootScope.$broadcast('$routeChangeSuccess', next, last); } @@ -535,10 +539,10 @@ function $RouteProvider(){ function parseRoute() { // Match a route var params, match; - forEach(routes, function(route, path) { + angular.forEach(routes, function(route, path) { if (!match && (params = switchRouteMatcher($location.path(), route))) { match = inherit(route, { - params: extend({}, $location.search(), params), + params: angular.extend({}, $location.search(), params), pathParams: params}); match.$$route = route; } @@ -552,7 +556,7 @@ function $RouteProvider(){ */ function interpolate(string, params) { var result = []; - forEach((string||'').split(':'), function(segment, i) { + angular.forEach((string||'').split(':'), function(segment, i) { if (i === 0) { result.push(segment); } else { @@ -566,4 +570,4 @@ function $RouteProvider(){ return result.join(''); } }]; -} +} \ No newline at end of file diff --git a/src/ngRoute/routeUtils.js b/src/ngRoute/routeUtils.js deleted file mode 100644 index 0cff7213..00000000 --- a/src/ngRoute/routeUtils.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -var copy = angular.copy, - equals = angular.equals, - extend = angular.extend, - forEach = angular.forEach, - isDefined = angular.isDefined, - isFunction = angular.isFunction, - isString = angular.isString, - jqLite = angular.element, - noop = angular.noop, - toJson = angular.toJson; - - -function inherit(parent, extra) { - return extend(new (extend(function() {}, {prototype:parent}))(), extra); -} diff --git a/test/helpers/matchers.js b/test/helpers/matchers.js index 14430b37..c5d7d6cf 100644 --- a/test/helpers/matchers.js +++ b/test/helpers/matchers.js @@ -74,11 +74,11 @@ beforeEach(function() { this.message = function() { var expected; if (this.actual.message && this.actual.name == 'Error') { - expected = toJson(this.actual.message); + expected = angular.toJson(this.actual.message); } else { - expected = toJson(this.actual); + expected = angular.toJson(this.actual); } - return "Expected " + expected + " to be an Error with message " + toJson(message); + return "Expected " + expected + " to be an Error with message " + angular.toJson(message); }; return this.actual.name == 'Error' && this.actual.message == message; }, @@ -187,9 +187,9 @@ beforeEach(function() { codeRegex = new RegExp('^\\[' + escapeRegexp(namespace) + ':' + escapeRegexp(code) + '\\]'), not = this.isNot ? "not " : "", regex = jasmine.isA_("RegExp", content) ? content : - isDefined(content) ? new RegExp(escapeRegexp(content)) : undefined; + angular.isDefined(content) ? new RegExp(escapeRegexp(content)) : undefined; - if(!isFunction(this.actual)) { + if(!angular.isFunction(this.actual)) { throw new Error('Actual is not a function'); } @@ -215,7 +215,7 @@ beforeEach(function() { return result; } - if (isDefined(regex)) { + if (angular.isDefined(regex)) { return regex.test(exceptionMessage); } return result; diff --git a/test/helpers/privateMocksSpec.js b/test/helpers/privateMocksSpec.js index e58a2b75..d5a1e188 100644 --- a/test/helpers/privateMocksSpec.js +++ b/test/helpers/privateMocksSpec.js @@ -9,7 +9,7 @@ describe('private mocks', function() { var stylesheet = createMockStyleSheet($document, $window); expect(doc.styleSheets.length).toBe(count + 1); - jqLite(doc.body).append($rootElement); + angular.element(doc.body).append($rootElement); var elm = $compile('