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

Commit f737c97

Browse files
iammerrickvojtajina
authored andcommitted
feat(ngMock): allow passing an object literal as shorthand to module
1 parent 8e48c4f commit f737c97

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/ngMock/angular-mocks.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1851,9 +1851,11 @@ angular.mock.clearDataCache = function() {
18511851
*
18521852
* See {@link angular.mock.inject inject} for usage example
18531853
*
1854-
* @param {...(string|Function)} fns any number of modules which are represented as string
1854+
* @param {...(string|Function|Object)} fns any number of modules which are represented as string
18551855
* aliases or as anonymous module initialization functions. The modules are used to
1856-
* configure the injector. The 'ng' and 'ngMock' modules are automatically loaded.
1856+
* configure the injector. The 'ng' and 'ngMock' modules are automatically loaded. If an
1857+
* object literal is passed they will be register as values in the module, the key being
1858+
* the module name and the value being what is returned.
18571859
*/
18581860
window.module = angular.mock.module = function() {
18591861
var moduleFns = Array.prototype.slice.call(arguments, 0);
@@ -1865,7 +1867,15 @@ angular.mock.clearDataCache = function() {
18651867
} else {
18661868
var modules = currentSpec.$modules || (currentSpec.$modules = []);
18671869
angular.forEach(moduleFns, function(module) {
1868-
modules.push(module);
1870+
if (angular.isObject(module) && !angular.isArray(module)) {
1871+
modules.push(function($provide) {
1872+
angular.forEach(module, function(value, key) {
1873+
$provide.value(key, value);
1874+
});
1875+
});
1876+
} else {
1877+
modules.push(module);
1878+
}
18691879
});
18701880
}
18711881
}

test/ngMock/angular-mocksSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,42 @@ describe('ngMock', function() {
520520
});
521521

522522
describe('module', function() {
523+
524+
describe('object literal format', function() {
525+
var mock = { log: 'module' };
526+
527+
beforeEach(function() {
528+
module({
529+
'service': mock,
530+
'other': { some: 'replacement'}
531+
},
532+
'ngResource',
533+
function ($provide) { $provide.value('example', 'win'); }
534+
);
535+
});
536+
537+
it('should inject the mocked module', function() {
538+
inject(function(service) {
539+
expect(service).toEqual(mock);
540+
});
541+
});
542+
543+
it('should support multiple key value pairs', function() {
544+
inject(function(service, other) {
545+
expect(other.some).toEqual('replacement');
546+
expect(service).toEqual(mock);
547+
});
548+
});
549+
550+
it('should integrate with string and function', function() {
551+
inject(function(service, $resource, example) {
552+
expect(service).toEqual(mock);
553+
expect($resource).toBeDefined();
554+
expect(example).toEqual('win');
555+
});
556+
});
557+
});
558+
523559
describe('in DSL', function() {
524560
it('should load module', module(function() {
525561
log += 'module';

0 commit comments

Comments
 (0)