Test XHR loading

This commit is contained in:
Tim Schaub
2016-05-10 07:01:40 -06:00
parent 5c07628044
commit a810feb257

View File

@@ -2,11 +2,10 @@ goog.provide('ol.test.source.TileUTFGrid');
describe('ol.source.TileUTFGrid', function() { describe('ol.source.TileUTFGrid', function() {
var url = 'spec/ol/data/tileutfgrid.json';
var tileJson = null; var tileJson = null;
// Called once for this describe section, this method loads a local
// tileutfgrid and stores it in a variable to compare it in the actual // Load and parse the UTFGrid fixture
// tests. This way we can test `handleTileJSONResponse` and also remove
// the dependency from the external service / URL.
before(function(done) { before(function(done) {
var client = new XMLHttpRequest(); var client = new XMLHttpRequest();
client.addEventListener('load', function() { client.addEventListener('load', function() {
@@ -14,57 +13,79 @@ describe('ol.source.TileUTFGrid', function() {
done(); done();
}); });
client.addEventListener('error', function() { client.addEventListener('error', function() {
done(new Error('Failed to fetch local tileutfgrid.json')) done(new Error('Failed to fetch ' + url));
}); });
client.open('GET', 'spec/ol/data/tileutfgrid.json'); client.open('GET', url);
client.send(); client.send();
}); });
after(function() { after(function() {
tileJson = null; tileJson = null;
}); });
var url = 'some-tileutfgrid-url';
function getTileUTFGrid() { function getTileUTFGrid() {
var source = new ol.source.TileUTFGrid({ return new ol.source.TileUTFGrid({
url: url url: url
}); });
return source;
} }
describe('constructor', function() { describe('constructor', function() {
it('needs to be constructed with url option', function() { it('needs to be constructed with url option', function() {
var source;
var source = new ol.source.TileUTFGrid({url: url});
expect(source).to.be.an(ol.source.TileUTFGrid);
expect(source).to.be.an(ol.source.Tile);
expect(function() { expect(function() {
// no options: will throw // no options: will throw
source = new ol.source.TileUTFGrid(); return new ol.source.TileUTFGrid();
}).to.throwException(); }).to.throwException();
expect(source).to.be(undefined);
expect(function() { expect(function() {
// no url-option: will throw // no url-option: will throw
source = new ol.source.TileUTFGrid({}); return new ol.source.TileUTFGrid({});
}).to.throwException(); }).to.throwException();
expect(source).to.be(undefined);
expect(function() { expect(getTileUTFGrid()).to.be.an(ol.source.TileUTFGrid);
// with a (bogus) url option all is fine
source = new ol.source.TileUTFGrid({
url: url
});
}).to.not.throwException();
expect(source).to.be.an(ol.source.TileUTFGrid);
expect(function() {
// also test our utility method
source = getTileUTFGrid();
}).to.not.throwException();
expect(source).to.be.an(ol.source.TileUTFGrid);
}); });
}); });
describe('change event (ready)', function() {
it('is fired when the source is ready', function(done) {
var source = new ol.source.TileUTFGrid({
url: url
});
expect(source.getState()).to.be(ol.source.State.LOADING);
expect(source.tileGrid).to.be(null);
source.on('change', function(event) {
if (source.getState() === ol.source.State.READY) {
expect(source.tileGrid).to.be.an(ol.tilegrid.TileGrid);
done();
}
});
});
});
describe('change event (error)', function(done) {
it('is fired when the source fails to initialize', function(done) {
var source = new ol.source.TileUTFGrid({
url: 'Bogus UTFGrid URL'
});
expect(source.getState()).to.be(ol.source.State.LOADING);
expect(source.tileGrid).to.be(null);
source.on('change', function(event) {
if (source.getState() === ol.source.State.ERROR) {
expect(source.tileGrid).to.be(null);
done();
}
});
});
});
describe('#handleTileJSONResponse', function() { describe('#handleTileJSONResponse', function() {
it('sets up a tileGrid', function() { it('sets up a tileGrid', function() {
@@ -268,6 +289,7 @@ describe('ol.source.TileUTFGrid', function() {
goog.require('ol.net'); goog.require('ol.net');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.State'); goog.require('ol.source.State');
goog.require('ol.source.Tile');
goog.require('ol.source.TileUTFGrid'); goog.require('ol.source.TileUTFGrid');
goog.require('ol.tilegrid.TileGrid'); goog.require('ol.tilegrid.TileGrid');
goog.require('ol.TileState'); goog.require('ol.TileState');