From 1ac042c7cae79d3f9c00a777169990a99eb5f2f1 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 26 Feb 2020 17:01:30 -0500 Subject: [PATCH] [Maps] Add XYZTMSSource and TileLayer unit test suites (#58567) (#58637) --- .../legacy/plugins/maps/common/constants.ts | 2 +- .../plugins/maps/common/descriptor_types.d.ts | 7 ++- .../plugins/maps/public/layers/layer.d.ts | 21 +++++++ .../maps/public/layers/sources/source.d.ts | 19 +++++++ .../public/layers/sources/tms_source.d.ts | 15 +++++ .../public/layers/sources/xyz_tms_source.d.ts | 11 ++++ .../public/layers/sources/xyz_tms_source.js | 3 +- .../layers/sources/xyz_tms_source.test.ts | 30 ++++++++++ .../maps/public/layers/tile_layer.d.ts | 18 ++++++ .../maps/public/layers/tile_layer.test.ts | 55 +++++++++++++++++++ 10 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 x-pack/legacy/plugins/maps/public/layers/layer.d.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/source.d.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/tms_source.d.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.d.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.test.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/tile_layer.d.ts create mode 100644 x-pack/legacy/plugins/maps/public/layers/tile_layer.test.ts diff --git a/x-pack/legacy/plugins/maps/common/constants.ts b/x-pack/legacy/plugins/maps/common/constants.ts index 542abfb004d0d6..0e93fd65937103 100644 --- a/x-pack/legacy/plugins/maps/common/constants.ts +++ b/x-pack/legacy/plugins/maps/common/constants.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ import { i18n } from '@kbn/i18n'; - export const EMS_CATALOGUE_PATH = 'ems/catalogue'; export const EMS_FILES_CATALOGUE_PATH = 'ems/files'; @@ -54,6 +53,7 @@ export const EMS_FILE = 'EMS_FILE'; export const ES_GEO_GRID = 'ES_GEO_GRID'; export const ES_SEARCH = 'ES_SEARCH'; export const ES_PEW_PEW = 'ES_PEW_PEW'; +export const EMS_XYZ = 'EMS_XYZ'; // identifies a custom TMS source. Name is a little unfortunate. export const FIELD_ORIGIN = { SOURCE: 'source', diff --git a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts index 05123c9c86b635..c024721dfb870f 100644 --- a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts +++ b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts @@ -4,14 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ -import { IFieldType } from '../../../../../src/plugins/data/common/index_patterns/fields'; - export interface ISourceDescriptor { id: string; type: string; } +export interface IXYZTMSSourceDescriptor extends ISourceDescriptor { + urlTemplate: string; +} + export interface ILayerDescriptor { sourceDescriptor: ISourceDescriptor; id: string; + label?: string; } diff --git a/x-pack/legacy/plugins/maps/public/layers/layer.d.ts b/x-pack/legacy/plugins/maps/public/layers/layer.d.ts new file mode 100644 index 00000000000000..ed7dcb062d8c40 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/layer.d.ts @@ -0,0 +1,21 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { ILayerDescriptor } from '../../common/descriptor_types'; +import { ISource } from './sources/source'; + +export interface ILayer { + getDisplayName(): Promise; +} + +export interface ILayerArguments { + layerDescriptor: ILayerDescriptor; + source: ISource; +} + +export class AbstractLayer implements ILayer { + constructor(layerArguments: ILayerArguments); + getDisplayName(): Promise; +} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/source.d.ts new file mode 100644 index 00000000000000..372981de425979 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/source.d.ts @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { ISourceDescriptor } from '../../../common/descriptor_types'; +import { ILayer } from '../layer'; + +export interface ISource { + createDefaultLayer(): ILayer; + getDisplayName(): Promise; +} + +export class AbstractSource implements ISource { + constructor(sourceDescriptor: ISourceDescriptor, inspectorAdapters: object); + createDefaultLayer(): ILayer; + getDisplayName(): Promise; +} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/tms_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/tms_source.d.ts new file mode 100644 index 00000000000000..90b6f28e050fd2 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/tms_source.d.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { AbstractSource, ISource } from './source'; + +export interface ITMSSource extends ISource { + getUrlTemplate(): Promise; +} + +export class AbstractTMSSource extends AbstractSource implements ITMSSource { + getUrlTemplate(): Promise; +} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.d.ts b/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.d.ts new file mode 100644 index 00000000000000..1150c39b79db52 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.d.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { AbstractTMSSource } from './tms_source'; +import { IXYZTMSSourceDescriptor } from '../../../common/descriptor_types'; + +export class XYZTMSSource extends AbstractTMSSource { + constructor(sourceDescriptor: IXYZTMSSourceDescriptor, inspectorAdapters: unknown); +} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.js index a4d331f48beae7..354883372e2444 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.js @@ -12,9 +12,10 @@ import { TileLayer } from '../tile_layer'; import { i18n } from '@kbn/i18n'; import { getDataSourceLabel, getUrlLabel } from '../../../common/i18n_getters'; import _ from 'lodash'; +import { EMS_XYZ } from '../../../common/constants'; export class XYZTMSSource extends AbstractTMSSource { - static type = 'EMS_XYZ'; + static type = EMS_XYZ; static title = i18n.translate('xpack.maps.source.ems_xyzTitle', { defaultMessage: 'Tile Map Service', }); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.test.ts b/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.test.ts new file mode 100644 index 00000000000000..5de85adde158ff --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/sources/xyz_tms_source.test.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { XYZTMSSource } from './xyz_tms_source'; +import { ILayer } from '../layer'; +import { TileLayer } from '../tile_layer'; +import { EMS_XYZ } from '../../../common/constants'; +import { IXYZTMSSourceDescriptor } from '../../../common/descriptor_types'; + +const descriptor: IXYZTMSSourceDescriptor = { + type: EMS_XYZ, + urlTemplate: 'https://example.com/{x}/{y}/{z}.png', + id: 'foobar', +}; +describe('xyz Tilemap Source', () => { + it('should create a tile-layer', () => { + const source = new XYZTMSSource(descriptor, null); + const layer: ILayer = source.createDefaultLayer(); + expect(layer instanceof TileLayer).toEqual(true); + }); + + it('should echo url template for url template', async () => { + const source = new XYZTMSSource(descriptor, null); + const template = await source.getUrlTemplate(); + expect(template).toEqual(descriptor.urlTemplate); + }); +}); diff --git a/x-pack/legacy/plugins/maps/public/layers/tile_layer.d.ts b/x-pack/legacy/plugins/maps/public/layers/tile_layer.d.ts new file mode 100644 index 00000000000000..fdb37a8af805f8 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/tile_layer.d.ts @@ -0,0 +1,18 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { AbstractLayer, ILayerArguments } from './layer'; +import { ITMSSource } from './sources/tms_source'; +import { ILayerDescriptor } from '../../common/descriptor_types'; + +interface ITileLayerArguments extends ILayerArguments { + source: ITMSSource; + layerDescriptor: ILayerDescriptor; +} + +export class TileLayer extends AbstractLayer { + constructor(args: ITileLayerArguments); +} diff --git a/x-pack/legacy/plugins/maps/public/layers/tile_layer.test.ts b/x-pack/legacy/plugins/maps/public/layers/tile_layer.test.ts new file mode 100644 index 00000000000000..b2d2b08c637cf7 --- /dev/null +++ b/x-pack/legacy/plugins/maps/public/layers/tile_layer.test.ts @@ -0,0 +1,55 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { TileLayer } from './tile_layer'; +import { EMS_XYZ } from '../../common/constants'; +import { IXYZTMSSourceDescriptor } from '../../common/descriptor_types'; +import { ITMSSource } from './sources/tms_source'; +import { ILayer } from './layer'; + +const sourceDescriptor: IXYZTMSSourceDescriptor = { + type: EMS_XYZ, + urlTemplate: 'https://example.com/{x}/{y}/{z}.png', + id: 'foobar', +}; + +class MockTileSource implements ITMSSource { + private _descriptor: IXYZTMSSourceDescriptor; + constructor(descriptor: IXYZTMSSourceDescriptor) { + this._descriptor = descriptor; + } + createDefaultLayer(): ILayer { + throw new Error('not implemented'); + } + + async getDisplayName(): Promise { + return this._descriptor.urlTemplate; + } + + async getUrlTemplate(): Promise { + return 'template/{x}/{y}/{z}.png'; + } +} + +describe('TileLayer', () => { + it('should use display-label from source', async () => { + const source = new MockTileSource(sourceDescriptor); + const layer: ILayer = new TileLayer({ + source, + layerDescriptor: { id: 'layerid', sourceDescriptor }, + }); + expect(await source.getDisplayName()).toEqual(await layer.getDisplayName()); + }); + + it('should override with custom display-label if present', async () => { + const source = new MockTileSource(sourceDescriptor); + const layer: ILayer = new TileLayer({ + source, + layerDescriptor: { id: 'layerid', sourceDescriptor, label: 'custom' }, + }); + expect('custom').toEqual(await layer.getDisplayName()); + }); +});