Skip to content

Commit

Permalink
Merge d1486cc into 71a6438
Browse files Browse the repository at this point in the history
  • Loading branch information
osela committed Sep 7, 2016
2 parents 71a6438 + d1486cc commit 1f8e688
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/deferred-bootstrap.js
Expand Up @@ -32,8 +32,8 @@ function checkConfig (config) {
if (!isObject(config)) {
throw new Error('Bootstrap configuration must be an object.');
}
if (!isString(config.module)) {
throw new Error('\'config.module\' must be a string.');
if ((!isString(config.module) && !isArray(config.module)) || (isArray(config.module) && !config.module.length)) {
throw new Error('\'config.module\' must be a string or a non-empty array.');
}
if (config.resolve && config.moduleResolves) {
throw new Error('Bootstrap configuration can contain either \'resolve\' or \'moduleResolves\' but not both');
Expand Down Expand Up @@ -86,11 +86,11 @@ function createInjector (injectorModules, element) {
return angular.injector(modules, element);
}

function doBootstrap (element, module, bootstrapConfig) {
function doBootstrap (element, modules, bootstrapConfig) {
var deferred = $q.defer();

angular.element(document).ready(function () {
angular.bootstrap(element, [module], bootstrapConfig);
angular.bootstrap(element, modules, bootstrapConfig);
removeBodyClasses();

deferred.resolve(true);
Expand All @@ -102,7 +102,8 @@ function doBootstrap (element, module, bootstrapConfig) {
function bootstrap (configParam) {
var config = configParam || {},
element = config.element,
module = config.module,
modules = isArray(config.module) ? config.module : [config.module],
module,
injectorModules = config.injectorModules || [],
injector,
promises = [],
Expand All @@ -113,6 +114,10 @@ function bootstrap (configParam) {

addLoadingClass();
checkConfig(config);

// When bootstraping multiple modules, set constants on the first one
module = modules[0];

injector = createInjector(injectorModules, element);
$q = injector.get('$q');

Expand Down Expand Up @@ -146,7 +151,7 @@ function bootstrap (configParam) {
angular.module(moduleName).constant(constantName, result);
});

return doBootstrap(element, module, bootstrapConfig);
return doBootstrap(element, modules, bootstrapConfig);
}

function handleError (error) {
Expand Down
57 changes: 54 additions & 3 deletions test/deferred-bootstrap.spec.js
Expand Up @@ -58,6 +58,31 @@ describe('deferredBootstrapper', function () {
});
});

itAsync('should inject $location to first module in module array', function (done) {
bootstrap({
element: bodyElement,
module: [APP_NAME, 'module2'],
resolve: {
LOCATION: function ($http, $q, $location) {
var deferred = $q.defer();

deferred.resolve($location);

return deferred.promise;
}
}
});

angular.module('module2', []);

angular.module(APP_NAME, [])
.config(function (LOCATION) {
expect(LOCATION).toBeDefined();

done();
});
});

itAsync('should resolve with the value returned from the defined constant', function (done) {
bootstrap({
element: bodyElement,
Expand Down Expand Up @@ -326,7 +351,7 @@ describe('deferredBootstrapper', function () {

describe('checkConfig()', function () {

it('should accept valid deferred bootstrap config', function () {
it('should accept valid deferred bootstrap config with module set to a string', function () {
var config = {
element: {},
module: 'myModule',
Expand All @@ -345,7 +370,19 @@ describe('deferredBootstrapper', function () {
}).toThrow('Bootstrap configuration must be an object.');
});

it('should throw if module is not a string', function () {
it('should accept valid deferred bootstrap config with module set to an array', function () {
var config = {
element: {},
module: ['myModule'],
resolve: {
CONST: function () {
}
}
};
checkConfig(config);
});

it('should throw if module is an empty array', function () {
var config = {
element: {},
module: [],
Expand All @@ -356,7 +393,21 @@ describe('deferredBootstrapper', function () {
};
expect(function () {
checkConfig(config);
}).toThrow('\'config.module\' must be a string.');
}).toThrow('\'config.module\' must be a string or a non-empty array.');
});

it('should throw if module is not a string or an array', function () {
var config = {
element: {},
module: 7,
resolve: {
CONST: function () {
}
}
};
expect(function () {
checkConfig(config);
}).toThrow('\'config.module\' must be a string or a non-empty array.');
});

it('should throw if both resolve and moduleResolves are defined', function () {
Expand Down

0 comments on commit 1f8e688

Please sign in to comment.