Skip to content
This repository has been archived by the owner on Dec 1, 2020. It is now read-only.

esriLoader tests for require instead of esri global #163

Merged
merged 1 commit into from
Nov 22, 2015
Merged
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: 0 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ gulp.task('karma-once', function(done) {
new KarmaServer({
configFile: __dirname + '/test/unit/karma.conf.js',
singleRun: true,
// if phantom doesn't start, change this port
// there's always some live reload listening on 9876
// so we're defaulting to 6789
port: 6789,
browsers: ['PhantomJS']
}, done).start();
});
Expand Down
9 changes: 6 additions & 3 deletions src/core/esriLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @ngdoc service
* @name esri.core.factory:esriLoader
*
* @requires $q
*
* @description
* Use `esriLoader` to lazyload the Esri ArcGIS API or to require API modules.
*/
Expand All @@ -28,8 +30,9 @@
var deferred = $q.defer();

// Don't reload API if it is already loaded
if ( angular.isDefined(window.esri) ) {
if (isLoaded()) {
deferred.reject('ESRI API is already loaded.');
return deferred.promise;
}

// Default options object to empty hash
Expand All @@ -41,7 +44,7 @@
script.src = options.url || 'http://js.arcgis.com/3.14compact';

// Set onload callback to resolve promise
script.onload = function() { deferred.resolve( window.esri ); };
script.onload = function() { deferred.resolve( window.require ); };

document.body.appendChild(script);

Expand All @@ -56,7 +59,7 @@
* @return {Boolean} Returns a boolean if the Esri ArcGIS API for JavaScript is already loaded.
*/
function isLoaded() {
return angular.isDefined(window.esri);
return typeof window.require !== 'undefined';
}

/**
Expand Down
59 changes: 58 additions & 1 deletion test/unit/core/esriLoader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,59 @@ describe('esriLoader', function() {

});

describe('bootstrap', function() {

describe('when not loaded', function() {

// watch for appending a script
beforeEach(function() {
spyOn(document.body, 'appendChild');
});

describe('when not passing url in options', function() {
it('should default to 3.14compact', function() {
var url = 'http://js.arcgis.com/3.14compact';
esriLoader.bootstrap();
expect(document.body.appendChild.calls.argsFor(0)[0].src).toEqual(url);
});
});

describe('when passing url in options', function() {
it('should have same url', function() {
var url = 'http://js.arcgis.com/3.14';
esriLoader.bootstrap({
url: url
});
expect(document.body.appendChild.calls.argsFor(0)[0].src).toEqual(url);
});
});
});

describe('when loaded', function() {

// fake that JSAPI is loaded and mock dojo/require
// and watch for appending a script
beforeEach(function() {
window.require = function() {
console.log(arguments);
};
});

// clean up mocked requied
afterEach(function() {
delete window.require;
});

it('should reject the promise', function() {
esriLoader.bootstrap().catch(function(err) {
expect(typeof err).toEqual('string');
});
$rootScope.$digest();
});
});

});

describe('require', function() {

describe('when not loaded', function() {
Expand All @@ -40,7 +93,6 @@ describe('esriLoader', function() {

// fake that JSAPI is loaded and mock dojo/require
beforeEach(function() {
window.esri = true;
window.require = function(moduleNames, callback) {
var fakeModules = moduleNames.map(function(moduleName) {
return {
Expand All @@ -52,6 +104,11 @@ describe('esriLoader', function() {
spyOn(window, 'require').and.callThrough();
});

// clean up mocked requied
afterEach(function() {
delete window.require;
});

describe('when passed a single module name as string', function() {
describe('and a callback function', function() {
var callback;
Expand Down