Skip to content

Commit

Permalink
feat(ngMock): invoke nested calls to module() immediately
Browse files Browse the repository at this point in the history
Before 1.3, it was possible to call `angular.mock.module()` from inside
another module. After this version additional calls were just ignored.

It is helpful to be able to do this since a test may have helper functions
that need to access "config"-time things such as constants or providers,
and without this change it is difficult to get hold of the `$provide`
object from within those helpers.

Closes angular#12887
  • Loading branch information
Shahar Talmi authored and petebacondarwin committed Oct 5, 2015
1 parent f02811f commit 51a27c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2282,16 +2282,21 @@ if (window.jasmine || window.mocha) {
if (currentSpec.$injector) {
throw new Error('Injector already created, can not register a module!');
} else {
var modules = currentSpec.$modules || (currentSpec.$modules = []);
var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);
angular.forEach(moduleFns, function(module) {
if (angular.isObject(module) && !angular.isArray(module)) {
modules.push(function($provide) {
fn = function($provide) {
angular.forEach(module, function(value, key) {
$provide.value(key, value);
});
});
};
} else {
modules.push(module);
fn = module;
}
if (currentSpec.$providerInjector) {
currentSpec.$providerInjector.invoke(fn);
} else {
modules.push(fn);
}
});
}
Expand Down Expand Up @@ -2405,6 +2410,9 @@ if (window.jasmine || window.mocha) {
function workFn() {
var modules = currentSpec.$modules || [];
var strictDi = !!currentSpec.$injectorStrict;
modules.unshift(function($injector) {
currentSpec.$providerInjector = $injector;
});
modules.unshift('ngMock');
modules.unshift('ng');
var injector = currentSpec.$injector;
Expand Down
13 changes: 13 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,19 @@ describe('ngMock', function() {
});
});

describe('nested calls', function() {
it('should invoke nested module calls immediately', function() {
module(function($provide) {
$provide.constant('someConst', 'blah');
module(function(someConst) {
log = someConst;
});
});
inject(function() {
expect(log).toBe('blah');
});
});
});

describe('inline in test', function() {
it('should load module', function() {
Expand Down

0 comments on commit 51a27c0

Please sign in to comment.