Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(modules): stop leaking global variables in tests #4360

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
42 changes: 23 additions & 19 deletions src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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)
);
Expand Down Expand Up @@ -460,17 +464,17 @@ 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;
$rootScope.$broadcast('$routeChangeStart', next, last);
$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 {
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -566,4 +570,4 @@ function $RouteProvider(){
return result.join('');
}
}];
}
}
17 changes: 0 additions & 17 deletions src/ngRoute/routeUtils.js

This file was deleted.

12 changes: 6 additions & 6 deletions test/helpers/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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');
}

Expand All @@ -215,7 +215,7 @@ beforeEach(function() {
return result;
}

if (isDefined(regex)) {
if (angular.isDefined(regex)) {
return regex.test(exceptionMessage);
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/privateMocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="padded">...</div>')($rootScope);
$rootElement.append(elm);
Expand Down
2 changes: 1 addition & 1 deletion test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('ngMock', function() {


describe('$log', function() {
forEach([true, false], function(debugEnabled) {
angular.forEach([true, false], function(debugEnabled) {
describe('debug ' + debugEnabled, function() {
beforeEach(module(function($logProvider) {
$logProvider.debugEnabled(debugEnabled);
Expand Down
22 changes: 11 additions & 11 deletions test/ngRoute/directive/ngViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ describe('ngView', function() {

it('should be async even if served from cache', function() {
module(function($routeProvider) {
$routeProvider.when('/foo', {controller: noop, templateUrl: 'myUrl1'});
$routeProvider.when('/foo', {controller: angular.noop, templateUrl: 'myUrl1'});
});

inject(function($route, $rootScope, $location, $templateCache) {
Expand Down Expand Up @@ -498,16 +498,16 @@ describe('ngView', function() {
$location.url('/foo');
$rootScope.$digest();

forEach(element.contents(), function(node) {
angular.forEach(element.contents(), function(node) {
if(node.nodeType == 3 /* text node */) {
expect(jqLite(node).scope()).not.toBe($route.current.scope);
expect(jqLite(node).controller()).not.toBeDefined();
expect(angular.element(node).scope()).not.toBe($route.current.scope);
expect(angular.element(node).controller()).not.toBeDefined();
} else if(node.nodeType == 8 /* comment node */) {
expect(jqLite(node).scope()).toBe(element.scope());
expect(jqLite(node).controller()).toBe(element.controller());
expect(angular.element(node).scope()).toBe(element.scope());
expect(angular.element(node).controller()).toBe(element.controller());
} else {
expect(jqLite(node).scope()).toBe($route.current.scope);
expect(jqLite(node).controller()).toBeDefined();
expect(angular.element(node).scope()).toBe($route.current.scope);
expect(angular.element(node).controller()).toBeDefined();
}
});
});
Expand All @@ -530,7 +530,7 @@ describe('ngView animations', function() {
// we need to run animation on attached elements;
return function(_$rootElement_) {
$rootElement = _$rootElement_;
body = jqLite(document.body);
body = angular.element(document.body);
};
}));

Expand All @@ -541,8 +541,8 @@ describe('ngView animations', function() {


beforeEach(module(function($provide, $routeProvider) {
$routeProvider.when('/foo', {controller: noop, templateUrl: '/foo.html'});
$routeProvider.when('/bar', {controller: noop, templateUrl: '/bar.html'});
$routeProvider.when('/foo', {controller: angular.noop, templateUrl: '/foo.html'});
$routeProvider.when('/bar', {controller: angular.noop, templateUrl: '/bar.html'});
return function($templateCache) {
$templateCache.put('/foo.html', [200, '<div>data</div>', {}]);
$templateCache.put('/bar.html', [200, '<div>data2</div>', {}]);
Expand Down
24 changes: 12 additions & 12 deletions test/ngRoute/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('$route', function() {

module(function($routeProvider) {
$routeProvider.when('/Book/:book/Chapter/:chapter',
{controller: noop, templateUrl: 'Chapter.html'});
{controller: angular.noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Blank', {});
});
inject(function($route, $location, $rootScope) {
Expand Down Expand Up @@ -69,9 +69,9 @@ describe('$route', function() {

module(function($routeProvider) {
$routeProvider.when('/Book1/:book/Chapter/:chapter/:highlight*/edit',
{controller: noop, templateUrl: 'Chapter.html'});
{controller: angular.noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Book2/:book/:highlight*/Chapter/:chapter',
{controller: noop, templateUrl: 'Chapter.html'});
{controller: angular.noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Blank', {});
});
inject(function($route, $location, $rootScope) {
Expand Down Expand Up @@ -128,9 +128,9 @@ describe('$route', function() {

module(function($routeProvider) {
$routeProvider.when('/Book1/:book/Chapter/:chapter/:highlight*/edit',
{controller: noop, templateUrl: 'Chapter.html', caseInsensitiveMatch: true});
{controller: angular.noop, templateUrl: 'Chapter.html', caseInsensitiveMatch: true});
$routeProvider.when('/Book2/:book/:highlight*/Chapter/:chapter',
{controller: noop, templateUrl: 'Chapter.html'});
{controller: angular.noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Blank', {});
});
inject(function($route, $location, $rootScope) {
Expand Down Expand Up @@ -618,8 +618,8 @@ describe('$route', function() {

inject(function($route, $httpBackend, $location, $rootScope, $routeParams) {
var log = '';
$rootScope.$on('$routeChangeStart', function(e, next) { log += '$before' + toJson($routeParams) + ';'});
$rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after' + toJson($routeParams) + ';'});
$rootScope.$on('$routeChangeStart', function(e, next) { log += '$before' + angular.toJson($routeParams) + ';'});
$rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after' + angular.toJson($routeParams) + ';'});

$httpBackend.whenGET('r1.html').respond('R1');
$httpBackend.whenGET('r2.html').respond('R2');
Expand Down Expand Up @@ -876,7 +876,7 @@ describe('$route', function() {
var reloaded = jasmine.createSpy('route reload');

module(function($routeProvider) {
$routeProvider.when('/foo', {controller: noop});
$routeProvider.when('/foo', {controller: angular.noop});
});

inject(function($route, $location, $rootScope, $routeParams) {
Expand All @@ -901,7 +901,7 @@ describe('$route', function() {
routeUpdate = jasmine.createSpy('route update');

module(function($routeProvider) {
$routeProvider.when('/foo', {controller: noop, reloadOnSearch: false});
$routeProvider.when('/foo', {controller: angular.noop, reloadOnSearch: false});
});

inject(function($route, $location, $rootScope) {
Expand Down Expand Up @@ -931,7 +931,7 @@ describe('$route', function() {
var routeChange = jasmine.createSpy('route change');

module(function($routeProvider) {
$routeProvider.when('/foo/:fooId', {controller: noop, reloadOnSearch: false});
$routeProvider.when('/foo/:fooId', {controller: angular.noop, reloadOnSearch: false});
});

inject(function($route, $location, $rootScope) {
Expand Down Expand Up @@ -963,8 +963,8 @@ describe('$route', function() {
var routeParamsWatcher = jasmine.createSpy('routeParamsWatcher');

module(function($routeProvider) {
$routeProvider.when('/foo', {controller: noop});
$routeProvider.when('/bar/:barId', {controller: noop, reloadOnSearch: false});
$routeProvider.when('/foo', {controller: angular.noop});
$routeProvider.when('/bar/:barId', {controller: angular.noop, reloadOnSearch: false});
});

inject(function($route, $location, $rootScope, $routeParams) {
Expand Down