mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-16 10:43:10 +00:00
feat(ngMock): allow passing an object literal as shorthand to module
This commit is contained in:
parent
8e48c4ff6a
commit
f737c97df0
2 changed files with 49 additions and 3 deletions
16
src/ngMock/angular-mocks.js
vendored
16
src/ngMock/angular-mocks.js
vendored
|
|
@ -1851,9 +1851,11 @@ angular.mock.clearDataCache = function() {
|
||||||
*
|
*
|
||||||
* See {@link angular.mock.inject inject} for usage example
|
* See {@link angular.mock.inject inject} for usage example
|
||||||
*
|
*
|
||||||
* @param {...(string|Function)} fns any number of modules which are represented as string
|
* @param {...(string|Function|Object)} fns any number of modules which are represented as string
|
||||||
* aliases or as anonymous module initialization functions. The modules are used to
|
* aliases or as anonymous module initialization functions. The modules are used to
|
||||||
* configure the injector. The 'ng' and 'ngMock' modules are automatically loaded.
|
* configure the injector. The 'ng' and 'ngMock' modules are automatically loaded. If an
|
||||||
|
* object literal is passed they will be register as values in the module, the key being
|
||||||
|
* the module name and the value being what is returned.
|
||||||
*/
|
*/
|
||||||
window.module = angular.mock.module = function() {
|
window.module = angular.mock.module = function() {
|
||||||
var moduleFns = Array.prototype.slice.call(arguments, 0);
|
var moduleFns = Array.prototype.slice.call(arguments, 0);
|
||||||
|
|
@ -1865,7 +1867,15 @@ angular.mock.clearDataCache = function() {
|
||||||
} else {
|
} else {
|
||||||
var modules = currentSpec.$modules || (currentSpec.$modules = []);
|
var modules = currentSpec.$modules || (currentSpec.$modules = []);
|
||||||
angular.forEach(moduleFns, function(module) {
|
angular.forEach(moduleFns, function(module) {
|
||||||
modules.push(module);
|
if (angular.isObject(module) && !angular.isArray(module)) {
|
||||||
|
modules.push(function($provide) {
|
||||||
|
angular.forEach(module, function(value, key) {
|
||||||
|
$provide.value(key, value);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
modules.push(module);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
test/ngMock/angular-mocksSpec.js
vendored
36
test/ngMock/angular-mocksSpec.js
vendored
|
|
@ -520,6 +520,42 @@ describe('ngMock', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('module', function() {
|
describe('module', function() {
|
||||||
|
|
||||||
|
describe('object literal format', function() {
|
||||||
|
var mock = { log: 'module' };
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
module({
|
||||||
|
'service': mock,
|
||||||
|
'other': { some: 'replacement'}
|
||||||
|
},
|
||||||
|
'ngResource',
|
||||||
|
function ($provide) { $provide.value('example', 'win'); }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should inject the mocked module', function() {
|
||||||
|
inject(function(service) {
|
||||||
|
expect(service).toEqual(mock);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support multiple key value pairs', function() {
|
||||||
|
inject(function(service, other) {
|
||||||
|
expect(other.some).toEqual('replacement');
|
||||||
|
expect(service).toEqual(mock);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should integrate with string and function', function() {
|
||||||
|
inject(function(service, $resource, example) {
|
||||||
|
expect(service).toEqual(mock);
|
||||||
|
expect($resource).toBeDefined();
|
||||||
|
expect(example).toEqual('win');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('in DSL', function() {
|
describe('in DSL', function() {
|
||||||
it('should load module', module(function() {
|
it('should load module', module(function() {
|
||||||
log += 'module';
|
log += 'module';
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue