mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[ui/vis_maps] remove async/await usage because angular
This commit is contained in:
parent
3099bb0bfb
commit
084b914a1e
3 changed files with 94 additions and 83 deletions
|
@ -5,6 +5,7 @@ import url from 'url';
|
|||
describe('tilemaptest - TileMapSettingsTests-deprecated', function () {
|
||||
let tilemapSettings;
|
||||
let tilemapsConfig;
|
||||
let loadSettings;
|
||||
|
||||
beforeEach(ngMock.module('kibana', ($provide) => {
|
||||
$provide.decorator('tilemapsConfig', () => ({
|
||||
|
@ -23,17 +24,22 @@ describe('tilemaptest - TileMapSettingsTests-deprecated', function () {
|
|||
}));
|
||||
}));
|
||||
|
||||
beforeEach(ngMock.inject(function ($injector) {
|
||||
beforeEach(ngMock.inject(function ($injector, $rootScope) {
|
||||
tilemapSettings = $injector.get('tilemapSettings');
|
||||
tilemapsConfig = $injector.get('tilemapsConfig');
|
||||
|
||||
loadSettings = () => {
|
||||
tilemapSettings.loadSettings();
|
||||
$rootScope.$digest();
|
||||
};
|
||||
}));
|
||||
|
||||
describe('getting settings', function () {
|
||||
beforeEach(async function () {
|
||||
await tilemapSettings.loadSettings();
|
||||
beforeEach(function () {
|
||||
loadSettings();
|
||||
});
|
||||
|
||||
it('should get url', async function () {
|
||||
it('should get url', function () {
|
||||
|
||||
const mapUrl = tilemapSettings.getUrl();
|
||||
expect(mapUrl).to.contain('{x}');
|
||||
|
@ -46,7 +52,7 @@ describe('tilemaptest - TileMapSettingsTests-deprecated', function () {
|
|||
|
||||
});
|
||||
|
||||
it('should get options', async function () {
|
||||
it('should get options', function () {
|
||||
const options = tilemapSettings.getOptions();
|
||||
expect(options).to.have.property('minZoom');
|
||||
expect(options).to.have.property('maxZoom');
|
||||
|
|
|
@ -2,36 +2,16 @@ import expect from 'expect.js';
|
|||
import ngMock from 'ng_mock';
|
||||
import url from 'url';
|
||||
|
||||
function matchUrlWithoutQuery(expected) {
|
||||
return (url) => (
|
||||
expected === url.split('?')[0]
|
||||
);
|
||||
}
|
||||
|
||||
describe('tilemaptest - TileMapSettingsTests-mocked', function () {
|
||||
let tilemapSettings;
|
||||
let tilemapsConfig;
|
||||
let oldGetManifest;
|
||||
|
||||
const mockGetManifest = async function () {
|
||||
const data = JSON.parse(`
|
||||
{
|
||||
"version":"0.0.0",
|
||||
"expiry":"14d",
|
||||
"services":[
|
||||
{
|
||||
"id":"road_map",
|
||||
"url":"https://proxy-tiles.elastic.co/v1/default/{z}/{x}/{y}.png",
|
||||
"minZoom":0,
|
||||
"maxZoom":12,
|
||||
"attribution":"© [Elastic Tile Service](https://www.elastic.co/elastic-tile-service)",
|
||||
"query_parameters":{
|
||||
"elastic_tile_service_tos":"agree",
|
||||
"my_app_name":"kibana"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
`);
|
||||
|
||||
return {
|
||||
data: data
|
||||
};
|
||||
};
|
||||
let loadSettings;
|
||||
|
||||
beforeEach(ngMock.module('kibana', ($provide) => {
|
||||
$provide.decorator('tilemapsConfig', () => ({
|
||||
|
@ -50,26 +30,57 @@ describe('tilemaptest - TileMapSettingsTests-mocked', function () {
|
|||
}));
|
||||
}));
|
||||
|
||||
beforeEach(ngMock.inject(function ($injector) {
|
||||
beforeEach(ngMock.inject(($injector, $httpBackend) => {
|
||||
tilemapSettings = $injector.get('tilemapSettings');
|
||||
tilemapsConfig = $injector.get('tilemapsConfig');
|
||||
|
||||
oldGetManifest = tilemapSettings._getTileServiceManifest;
|
||||
tilemapSettings._getTileServiceManifest = mockGetManifest;
|
||||
// body and headers copied from https://proxy-tiles.elastic.co/v1/manifest
|
||||
const MANIFEST_BODY = `{
|
||||
"version":"0.0.0",
|
||||
"expiry":"14d",
|
||||
"services":[
|
||||
{
|
||||
"id":"road_map",
|
||||
"url":"https://proxy-tiles.elastic.co/v1/default/{z}/{x}/{y}.png",
|
||||
"minZoom":0,
|
||||
"maxZoom":12,
|
||||
"attribution":"© [Elastic Tile Service](https://www.elastic.co/elastic-tile-service)",
|
||||
"query_parameters":{
|
||||
"elastic_tile_service_tos":"agree",
|
||||
"my_app_name":"kibana"
|
||||
}
|
||||
}
|
||||
]
|
||||
}`;
|
||||
|
||||
const MANIFEST_HEADERS = {
|
||||
'access-control-allow-methods': 'GET, OPTIONS',
|
||||
'access-control-allow-origin': '*',
|
||||
'content-length': `${MANIFEST_BODY.length}`,
|
||||
'content-type': 'application/json; charset=utf-8',
|
||||
date: (new Date()).toUTCString(),
|
||||
server: 'tileprox/20170102101655-a02e54d',
|
||||
status: '200',
|
||||
};
|
||||
|
||||
$httpBackend
|
||||
.expect('GET', matchUrlWithoutQuery('http://foo.bar/manifest'))
|
||||
.respond(MANIFEST_BODY, MANIFEST_HEADERS);
|
||||
|
||||
loadSettings = () => {
|
||||
tilemapSettings.loadSettings();
|
||||
$httpBackend.flush();
|
||||
};
|
||||
}));
|
||||
|
||||
afterEach(function () {
|
||||
//restore overrides.
|
||||
tilemapSettings._getTileServiceManifest = oldGetManifest;
|
||||
});
|
||||
|
||||
afterEach(ngMock.inject($httpBackend => {
|
||||
$httpBackend.verifyNoOutstandingRequest();
|
||||
$httpBackend.verifyNoOutstandingExpectation();
|
||||
}));
|
||||
|
||||
describe('getting settings', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
tilemapSettings.loadSettings().then(function () {
|
||||
done();
|
||||
});
|
||||
beforeEach(() => {
|
||||
loadSettings();
|
||||
});
|
||||
|
||||
|
||||
|
@ -105,26 +116,26 @@ describe('tilemaptest - TileMapSettingsTests-mocked', function () {
|
|||
});
|
||||
}
|
||||
|
||||
it('accepts an object', async () => {
|
||||
it('accepts an object', () => {
|
||||
tilemapSettings.addQueryParams({ foo: 'bar' });
|
||||
await tilemapSettings.loadSettings();
|
||||
loadSettings();
|
||||
assertQuery({ foo: 'bar' });
|
||||
});
|
||||
|
||||
it('merged additions with previous values', async () => {
|
||||
it('merged additions with previous values', () => {
|
||||
// ensure that changes are always additive
|
||||
tilemapSettings.addQueryParams({ foo: 'bar' });
|
||||
tilemapSettings.addQueryParams({ bar: 'stool' });
|
||||
await tilemapSettings.loadSettings();
|
||||
loadSettings();
|
||||
assertQuery({ foo: 'bar', bar: 'stool' });
|
||||
});
|
||||
|
||||
it('overwrites conflicting previous values', async () => {
|
||||
it('overwrites conflicting previous values', () => {
|
||||
// ensure that conflicts are overwritten
|
||||
tilemapSettings.addQueryParams({ foo: 'bar' });
|
||||
tilemapSettings.addQueryParams({ bar: 'stool' });
|
||||
tilemapSettings.addQueryParams({ foo: 'tstool' });
|
||||
await tilemapSettings.loadSettings();
|
||||
loadSettings();
|
||||
assertQuery({ foo: 'tstool', bar: 'stool' });
|
||||
});
|
||||
|
||||
|
|
|
@ -54,40 +54,39 @@ uiModules.get('kibana')
|
|||
return true;
|
||||
}
|
||||
|
||||
let manifest;
|
||||
try {
|
||||
const response = await this._getTileServiceManifest(tilemapsConfig.manifestServiceUrl, this._queryParams,
|
||||
attributionFromConfig, optionsFromConfig);
|
||||
manifest = response.data;
|
||||
return this._getTileServiceManifest(tilemapsConfig.manifestServiceUrl, this._queryParams)
|
||||
.then(response => {
|
||||
const manifest = response.data;
|
||||
this._error = null;
|
||||
} catch (e) {
|
||||
//request failed. Continue to use old settings.
|
||||
|
||||
this._options = {
|
||||
attribution: $sanitize(marked(manifest.services[0].attribution)),
|
||||
minZoom: manifest.services[0].minZoom,
|
||||
maxZoom: manifest.services[0].maxZoom,
|
||||
subdomains: []
|
||||
};
|
||||
|
||||
//additional query params need to be propagated to the TMS endpoint as well.
|
||||
const queryparams = _.assign({ }, manifest.services[0].query_parameters, this._queryParams);
|
||||
const query = url.format({ query: queryparams });
|
||||
this._url = manifest.services[0].url + query;//must preserve {} patterns from the url, so do not format path.
|
||||
|
||||
this._settingsInitialized = true;
|
||||
this._error = new Error(`Could not retrieve map service configuration from the manifest-service. ${e.message}`);
|
||||
})
|
||||
.catch(e => {
|
||||
this._settingsInitialized = true;
|
||||
this._error = new Error(`Could not retrieve manifest from the tile service: ${e.message}`);
|
||||
})
|
||||
.then(() => {
|
||||
return true;
|
||||
}
|
||||
|
||||
this._options = {
|
||||
attribution: $sanitize(marked(manifest.services[0].attribution)),
|
||||
minZoom: manifest.services[0].minZoom,
|
||||
maxZoom: manifest.services[0].maxZoom,
|
||||
subdomains: []
|
||||
};
|
||||
|
||||
//additional query params need to be propagated to the TMS endpoint as well.
|
||||
const queryparams = _.assign({ }, manifest.services[0].query_parameters, this._queryParams);
|
||||
const query = url.format({ query: queryparams });
|
||||
this._url = manifest.services[0].url + query;//must preserve {} patterns from the url, so do not format path.
|
||||
|
||||
this._settingsInitialized = true;
|
||||
return true;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Must be called before getUrl/getOptions can be called.
|
||||
*/
|
||||
async loadSettings() {
|
||||
loadSettings() {
|
||||
return this._loadSettings();
|
||||
}
|
||||
|
||||
|
@ -153,22 +152,17 @@ uiModules.get('kibana')
|
|||
/**
|
||||
* Make this instance property to allow for overrides by test code
|
||||
*/
|
||||
async _getTileServiceManifest(manifestUrl, additionalQueryParams) {
|
||||
_getTileServiceManifest(manifestUrl, additionalQueryParams) {
|
||||
const manifestServiceTokens = url.parse(manifestUrl);
|
||||
manifestServiceTokens.query = _.assign({}, manifestServiceTokens.query, additionalQueryParams);
|
||||
const requestUrl = url.format(manifestServiceTokens);
|
||||
return await $http({
|
||||
return $http({
|
||||
url: requestUrl,
|
||||
method: 'GET'
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return new TilemapSettings();
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue