Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Add XYZTMSSource unit test #58567

Merged
merged 10 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/maps/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions x-pack/legacy/plugins/maps/common/descriptor_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this import was an oversight. remove.


export interface ISourceDescriptor {
id: string;
type: string;
}

export interface IXYZTMSSourceDescriptor extends ISourceDescriptor {
urlTemplate: string;
}

export interface ILayerDescriptor {
sourceDescriptor: ISourceDescriptor;
id: string;
label?: string;
}
21 changes: 21 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/layer.d.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
}

export interface ILayerArguments {
layerDescriptor: ILayerDescriptor;
source: ISource;
}

export class AbstractLayer implements ILayer {
constructor(layerArguments: ILayerArguments);
getDisplayName(): Promise<string>;
}
19 changes: 19 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/source.d.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
}

export class AbstractSource implements ISource {
constructor(sourceDescriptor: ISourceDescriptor, inspectorAdapters: object);
createDefaultLayer(): ILayer;
getDisplayName(): Promise<string>;
}
15 changes: 15 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/tms_source.d.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
}

export class AbstractTMSSource extends AbstractSource implements ITMSSource {
getUrlTemplate(): Promise<string>;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor Author

@thomasneirynck thomasneirynck Feb 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is more to illustrate the typing in this PR. When TileLayer will be used in signatures in other .ts files (not yet the case), the actual type-checking will also be enforced by the compiler.

});

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);
});
});
18 changes: 18 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/tile_layer.d.ts
Original file line number Diff line number Diff line change
@@ -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);
}
55 changes: 55 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/tile_layer.test.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
return this._descriptor.urlTemplate;
}

async getUrlTemplate(): Promise<string> {
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());
});
});