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

fix(ngMock): annotate $RootScopeDecorator #10275

Closed
wants to merge 2 commits into from
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
4 changes: 2 additions & 2 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ function createInjector(modulesToLoad, strictDi) {
}

var args = [],
$inject = annotate(fn, strictDi, serviceName),
$inject = createInjector.$$annotate(fn, strictDi, serviceName),
length, i,
key;

Expand Down Expand Up @@ -835,7 +835,7 @@ function createInjector(modulesToLoad, strictDi) {
invoke: invoke,
instantiate: instantiate,
get: getService,
annotate: annotate,
annotate: createInjector.$$annotate,
has: function(name) {
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
}
Expand Down
18 changes: 16 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ angular.mock.e2e.$httpBackendDecorator =
*
* In addition to all the regular `Scope` methods, the following helper methods are available:
*/
angular.mock.$RootScopeDecorator = function($delegate) {
angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {

var $rootScopePrototype = Object.getPrototypeOf($delegate);

Expand Down Expand Up @@ -2119,24 +2119,38 @@ angular.mock.$RootScopeDecorator = function($delegate) {

return count;
}
};
}];


if (window.jasmine || window.mocha) {

var currentSpec = null,
annotatedFunctions,
isSpecRunning = function() {
return !!currentSpec;
};

var hookedAnnotate = angular.injector.$$annotate;
angular.injector.$$annotate = function(fn) {
if (typeof fn === 'function' && !fn.$inject) {
annotatedFunctions.push(fn);
}
return hookedAnnotate.apply(this, arguments);
};


(window.beforeEach || window.setup)(function() {
annotatedFunctions = [];
currentSpec = this;
});

(window.afterEach || window.teardown)(function() {
var injector = currentSpec.$injector;

annotatedFunctions.forEach(function(fn) {
delete fn.$inject;
});

angular.forEach(currentSpec.$modules, function(module) {
if (module && module.$$hashKey) {
module.$$hashKey = undefined;
Expand Down
4 changes: 3 additions & 1 deletion test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ describe('injector', function() {


it('should publish annotate API', function() {
expect(injector.annotate).toBe(annotate);
function fn() {}
expect(injector.annotate(fn)).toEqual([]);
expect(fn.$inject).toEqual([]);
});
});

Expand Down
33 changes: 33 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,39 @@ describe('ngMock', function() {
expect(testFn.$$hashKey).toBeUndefined();
});
});

describe('$inject cleanup', function() {
function testFn() {

}

it('should add $inject when invoking test function', inject(function($injector) {
$injector.invoke(testFn);
expect(testFn.$inject).toBeDefined();
}));

it('should cleanup $inject after previous test', function() {
expect(testFn.$inject).toBeUndefined();
});

it('should add $inject when annotating test function', inject(function($injector) {
$injector.annotate(testFn);
expect(testFn.$inject).toBeDefined();
}));

it('should cleanup $inject after previous test', function() {
expect(testFn.$inject).toBeUndefined();
});

it('should invoke an already annotated function', inject(function($injector) {
testFn.$inject = [];
$injector.invoke(testFn);
}));

it('should not cleanup $inject after previous test', function() {
expect(testFn.$inject).toBeDefined();
});
});
});

describe('in DSL', function() {
Expand Down