Skip to content

Commit

Permalink
Merge pull request #18 from ArcGIS/tests/unit
Browse files Browse the repository at this point in the history
unit tests that mock globals and spy on calls
  • Loading branch information
tomwayson committed Mar 27, 2017
2 parents c821d4f + ddc8467 commit ac26b76
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -48,6 +48,8 @@
"ember-export-application-global": "^1.0.5",
"ember-load-initializers": "^0.6.0",
"ember-resolver": "^2.0.3",
"ember-sinon": "0.6.0",
"ember-sinon-qunit": "1.4.1",
"ember-source": "~2.11.0",
"ember-welcome-page": "^2.0.2",
"loader.js": "^4.0.10"
Expand Down
62 changes: 57 additions & 5 deletions tests/unit/services/esri-loader-test.js
@@ -1,20 +1,72 @@
import { moduleFor, test } from 'ember-qunit';
import { moduleFor } from 'ember-qunit';
import test from 'ember-sinon-qunit/test-support/test';

moduleFor('service:esri-loader', 'Unit | Service | esri loader', {
// Specify the other units that are required for this test.
// needs: ['service:foo']
beforeEach () {
// remove previously stubbed require function
// NOTE: this could cause problems if used w/ other tests
// that actually call laod() w/o stubbing the function (i.e. acceptance tests)
// but it prevents the "not yet loaded" test from causing failures
delete window.__dojoRequire;
}
});

// NOTE: for now not testing load() or loadModules() when loaded
// hoping to be able to use esri-loader library, see:
// https://github.com/ArcGIS/ember-esri-loader/issues/13
// in which case those tests should be covered in that repo
// that can be mutated outside of this test, like in an acceptance test
test('when has not yet been loaded', function(assert) {
assert.expect(2);
let service = this.subject();
// NOTE: this could fail if used w/ other tests that actually call laod()
assert.notOk(service.get('isLoaded'), 'isLoaded should be false');
// try loading modules before JSAPI is loaded - this should fail
// NOTE: if actually loaded this can cause tests to hang
return service.loadModules(['esri/map', 'esri/layers/VectorTileLayer']).then(() => {}, err => {
assert.equal(err.message, 'The ArcGIS API for JavaScript has not been loaded. You must first call esriLoader.load()');
});
});

// NOTE: hoping to be able to use esri-loader library, see:
// https://github.com/ArcGIS/ember-esri-loader/issues/13
// in which case most of the tests below should be covered in that repo

test('load', function(assert) {
const done = assert.async();
assert.expect(2);
let service = this.subject();
const stub = this.stub(document.body, 'appendChild', (el) => {
el.onload();
});
service.load();
assert.ok(stub.calledOnce, 'appendChild was called once');
assert.equal(stub.getCall(0).args[0].src, 'https://js.arcgis.com/4.3');
done();
});

test('load other version', function(assert) {
const done = assert.async();
assert.expect(2);
let service = this.subject();
const stub = this.stub(document.body, 'appendChild', (el) => {
el.onload();
});
service.load({
url: 'https://js.arcgis.com/3.20'
});
assert.ok(stub.calledOnce, 'appendChild was called once');
assert.equal(stub.getCall(0).args[0].src, 'https://js.arcgis.com/3.20');
done();
});

test('load modules', function(assert) {
const done = assert.async();
assert.expect(1);
let service = this.subject();
const moduleNames = ['esri/map', 'esri/layers/VectorTileLayer'];
window.__dojoRequire = function (names, callback) {
assert.equal(names, moduleNames, '__dojoRequire called w/ correct args');
callback();
};
service.loadModules(moduleNames);
done();
});

0 comments on commit ac26b76

Please sign in to comment.