Skip to content

Commit

Permalink
handle errors caused when JSAPI is loaded by more than one acceptance…
Browse files Browse the repository at this point in the history
… test

this code is _very_ ugly, but it works

will try to fix this upstream in esri-loader and remove the ugly hacks here
  • Loading branch information
tomwayson committed Aug 30, 2017
1 parent 1f0e9ab commit 0d9bd07
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
51 changes: 47 additions & 4 deletions addon/services/esri-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,54 @@ export default Ember.Service.extend({
// otherwise create a promise that will resolve when the JSAPI is loaded
this._loadPromise = new Ember.RSVP.Promise((resolve, reject) => {
esriLoader.bootstrap(err => {
// need to kick of run loop in case we are in test mode
// see: https://discuss.emberjs.com/t/guide-asynchronous-side-effects-in-testing/2905
Ember.run(() => {
if (err) {
reject(err);
const message = err.message || err;
// has esriLoader.bootstrap already been called?
if (message === 'The ArcGIS API for JavaScript is already loaded.') {
// this can happen when there is more than one instance of this service
// running on the page at a time, for example, in acceptance tests

// TODO: this is _much_ better handled upstream
// so we want to get rid of all this once this issue is resolved:
// https://github.com/Esri/esri-loader/issues/28

// first check if it's the same script
// NOTE: will haev to update this every time it's updated here:
// https://github.com/Esri/esri-loader/blob/master/src/esri-loader.ts#L29
const defaultUrl = 'https://js.arcgis.com/4.4/';
const url = options.url || defaultUrl;
const script = document.querySelector('script[data-esri-loader]');
if (script.src !== url) {
// user tried to load two different versions of the JSAPI
reject(err);
} else {
// check if the script has loaded yet
if (script.dataset.esriLoader === 'loaded' || esriLoader.isLoaded()) {
// notify any watchers of isLoaded copmuted property
this.notifyPropertyChange('isLoaded');
// let the caller know that the API has been successfully loaded
// TODO: would there be something more useful to return here?
resolve({ success: true });
} else {
// wait for the script to load and then resolve
script.addEventListener('load', () => {
// more fun w/ Ember.run(), tests will fail w/o this
Ember.run(() => {
// notify any watchers of isLoaded copmuted property
this.notifyPropertyChange('isLoaded');
// let the caller know that the API has been successfully loaded
// TODO: would there be something more useful to return here?
resolve({ success: true });
});
}, false);
}
}
} else {
reject(err);
}
} else {
// notify any watchers of isLoaded copmuted property
this.notifyPropertyChange('isLoaded');
Expand Down Expand Up @@ -70,9 +115,7 @@ export default Ember.Service.extend({
_loadModules (moduleNames) {
return new Ember.RSVP.Promise(resolve => {
esriLoader.dojoRequire(moduleNames, (...modules) => {
Ember.run(() => {
resolve(modules);
});
resolve(modules);
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/acceptance/index-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { skip } from 'qunit';
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | index');
Expand All @@ -8,7 +8,7 @@ moduleForAcceptance('Acceptance | index');
// b/c it gets called twice and throws "The ArcGIS API for JavaScript is already loaded."
// and ember in it's infinite wisdom decides to fail the test even though the route does .catch() the error
// TODO: don't skip this test once we resolve the above issue
skip('visiting /', function(assert) {
test('visiting /', function(assert) {
visit('/');

andThen(function() {
Expand Down

0 comments on commit 0d9bd07

Please sign in to comment.