Add tests and documentation for ol.net.jsonp

ol.net.Jsonp was renamed to ol.net.jsonp, because it is not a constructor.
This commit is contained in:
Andreas Hocevar
2016-01-31 23:03:36 +01:00
parent 8d0ef13505
commit 136c8af878
6 changed files with 84 additions and 15 deletions

65
test/spec/ol/net.test.js Normal file
View File

@@ -0,0 +1,65 @@
goog.provide('ol.test.net');
describe('ol.net', function() {
describe('jsonp()', function() {
var head = goog.global.document.getElementsByTagName('head')[0];
var origAppendChild = head.appendChild;
var origSetTimeout = goog.global.setTimeout;
var key;
function createCallback(url, done) {
var callback = function(data) {
expect(data).to.be(url + key);
done();
};
key = 'olc_' + goog.getUid(callback);
return callback;
}
beforeEach(function() {
head.appendChild = function(element) {
origSetTimeout(function() {
goog.global[key](element.getAttribute('src'));
}, 0);
};
goog.global.setTimeout = function(fn, time) {
origSetTimeout(fn, 100);
};
});
afterEach(function() {
head.appendChild = origAppendChild;
goog.global.setTimeout = origSetTimeout;
});
it('appends callback param to url, cleans up after call', function(done) {
ol.net.jsonp('foo', createCallback('foo?callback=', done));
});
it('appends correct callback param to a url with query', function(done) {
var callback = createCallback('http://foo/bar?baz&callback=', done);
ol.net.jsonp('http://foo/bar?baz', callback);
});
it('calls errback when jsonp is not executed, cleans up', function(done) {
head.appendChild = function() {};
function callback() {
expect.fail();
}
function errback() {
expect(goog.global[key]).to.be(undefined);
done();
}
ol.net.jsonp('foo', callback, errback);
});
it('accepts a custom callback param', function(done) {
var callback = createCallback('foo?mycallback=', done);
ol.net.jsonp('foo', callback, undefined, 'mycallback');
});
});
});
goog.require('ol.net');

View File

@@ -7,9 +7,9 @@ describe('ol.source.BingMaps', function() {
var source, tileGrid;
beforeEach(function(done) {
var olNetJsonp = ol.net.Jsonp;
var olNetJsonp = ol.net.jsonp;
// mock ol.net.Jsonp (used in the ol.source.TileJSON constructor)
ol.net.Jsonp = function(url, callback) {
ol.net.jsonp = function(url, callback) {
var client = new XMLHttpRequest();
client.open('GET', 'spec/ol/data/bing_aerialwithlabels.json', true);
client.onload = function() {
@@ -21,7 +21,7 @@ describe('ol.source.BingMaps', function() {
imagerySet: 'AerialWithLabels',
key: ''
});
ol.net.Jsonp = olNetJsonp;
ol.net.jsonp = olNetJsonp;
var key = source.on('change', function() {
if (source.getState() === 'ready') {
ol.Observable.unByKey(key);
@@ -72,7 +72,7 @@ describe('ol.source.BingMaps', function() {
});
goog.require('ol.net.Jsonp');
goog.require('ol.net');
goog.require('ol.source.BingMaps');
goog.require('ol.tilecoord');
goog.require('ol.Observable');