From 02437dd9c71d2a23c21d80647be7b2da44d09cb4 Mon Sep 17 00:00:00 2001 From: Tom Wayson Date: Sat, 21 Nov 2015 13:38:38 -0800 Subject: [PATCH] esriLoader tests for require instead of esri global also added unit tests for esriLoader.bootstrap() and no longer running gulp karma on port 6789 --- gulpfile.js | 4 --- src/core/esriLoader.js | 9 +++-- test/unit/core/esriLoader.spec.js | 59 ++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 3e9bcd5..e0bb607 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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(); }); diff --git a/src/core/esriLoader.js b/src/core/esriLoader.js index 19ff29e..94e37fa 100644 --- a/src/core/esriLoader.js +++ b/src/core/esriLoader.js @@ -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. */ @@ -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 @@ -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); @@ -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'; } /** diff --git a/test/unit/core/esriLoader.spec.js b/test/unit/core/esriLoader.spec.js index 65d06c5..d51613e 100644 --- a/test/unit/core/esriLoader.spec.js +++ b/test/unit/core/esriLoader.spec.js @@ -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() { @@ -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 { @@ -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;