From 529b9c3c28290f9b2d748ef8136437c7a637576f Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 8 Jul 2020 14:12:32 -0400 Subject: [PATCH] [7.8] Migrate service settings test to jest (#70992) (#71027) --- .../service_settings.test.js} | 301 +++++++++--------- 1 file changed, 159 insertions(+), 142 deletions(-) rename src/plugins/maps_legacy/public/{__tests__/map/service_settings.js => map/service_settings.test.js} (52%) diff --git a/src/plugins/maps_legacy/public/__tests__/map/service_settings.js b/src/plugins/maps_legacy/public/map/service_settings.test.js similarity index 52% rename from src/plugins/maps_legacy/public/__tests__/map/service_settings.js rename to src/plugins/maps_legacy/public/map/service_settings.test.js index 72c4323ed07365..4f30dcc8f64c57 100644 --- a/src/plugins/maps_legacy/public/__tests__/map/service_settings.js +++ b/src/plugins/maps_legacy/public/map/service_settings.test.js @@ -17,130 +17,136 @@ * under the License. */ -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; +import { getInjectedVarFunc } from '../kibana_services'; + +jest.mock('../kibana_services', () => ({ + getInjectedVarFunc: jest.fn(), +})); + import url from 'url'; -import EMS_FILES from './ems_mocks/sample_files.json'; -import EMS_TILES from './ems_mocks/sample_tiles.json'; -import EMS_STYLE_ROAD_MAP_BRIGHT from './ems_mocks/sample_style_bright'; -import EMS_STYLE_ROAD_MAP_DESATURATED from './ems_mocks/sample_style_desaturated'; -import EMS_STYLE_DARK_MAP from './ems_mocks/sample_style_dark'; -import { ORIGIN } from '../../common/constants/origin'; - -describe('service_settings (FKA tilemaptest)', function () { - let serviceSettings; - let mapConfig; - let tilemapsConfig; - - const emsFileApiUrl = 'https://files.foobar'; - const emsTileApiUrl = 'https://tiles.foobar'; - - const emsTileApiUrl2 = 'https://tiles_override.foobar'; - const emsFileApiUrl2 = 'https://files_override.foobar'; - - beforeEach( - ngMock.module('kibana', ($provide) => { - $provide.decorator('mapConfig', () => { - return { - emsFileApiUrl, - emsTileApiUrl, - includeElasticMapsService: true, - emsTileLayerId: { - bright: 'road_map', - desaturated: 'road_map_desaturated', - dark: 'dark_map', - }, - }; - }); - }) - ); - - let emsTileApiUrlOriginal; - let emsFileApiUrlOriginal; - let tilemapsConfigDeprecatedOriginal; - let getManifestStub; - beforeEach( - ngMock.inject(function ($injector, $rootScope) { - serviceSettings = $injector.get('serviceSettings'); - getManifestStub = serviceSettings.__debugStubManifestCalls(async (url) => { - //simulate network calls - if (url.startsWith('https://tiles.foobar')) { - if (url.includes('/manifest')) { - return EMS_TILES; - } else if (url.includes('osm-bright-desaturated.json')) { - return EMS_STYLE_ROAD_MAP_DESATURATED; - } else if (url.includes('osm-bright.json')) { - return EMS_STYLE_ROAD_MAP_BRIGHT; - } else if (url.includes('dark-matter.json')) { - return EMS_STYLE_DARK_MAP; - } - } else if (url.startsWith('https://files.foobar')) { - return EMS_FILES; +import EMS_FILES from '../__tests__/map/ems_mocks/sample_files.json'; +import EMS_TILES from '../__tests__/map/ems_mocks/sample_tiles.json'; +import EMS_STYLE_ROAD_MAP_BRIGHT from '../__tests__/map/ems_mocks/sample_style_bright'; +import EMS_STYLE_ROAD_MAP_DESATURATED from '../__tests__/map/ems_mocks/sample_style_desaturated'; +import EMS_STYLE_DARK_MAP from '../__tests__/map/ems_mocks/sample_style_dark'; +import { ORIGIN } from '../common/constants/origin'; +import { ServiceSettings } from './service_settings'; + +describe('service_settings (FKA tile_map test)', function () { + let mapConfigOptions = {}; + let tilemapConfigOptions = {}; + + beforeEach(() => { + const emsFileApiUrl = 'https://files.foobar'; + const emsTileApiUrl = 'https://tiles.foobar'; + + const defaultMapConfig = { + emsFileApiUrl, + emsTileApiUrl, + includeElasticMapsService: true, + emsTileLayerId: { + bright: 'road_map', + desaturated: 'road_map_desaturated', + dark: 'dark_map', + }, + }; + + const defaultTilemapConfig = { + deprecated: { + config: { + options: {}, + }, + }, + }; + getInjectedVarFunc.mockImplementation(() => { + return (param) => { + if (param === 'version') { + return '1.2.3'; + } else if (param === 'mapConfig') { + return { ...defaultMapConfig, ...mapConfigOptions }; + } else if (param === 'tilemapsConfig') { + return { ...defaultTilemapConfig, ...tilemapConfigOptions }; } - }); - mapConfig = $injector.get('mapConfig'); - tilemapsConfig = $injector.get('tilemapsConfig'); - - emsTileApiUrlOriginal = mapConfig.emsTileApiUrl; - emsFileApiUrlOriginal = mapConfig.emsFileApiUrl; - - tilemapsConfigDeprecatedOriginal = tilemapsConfig.deprecated; - $rootScope.$digest(); - }) - ); - - afterEach(function () { - getManifestStub.removeStub(); - mapConfig.emsTileApiUrl = emsTileApiUrlOriginal; - mapConfig.emsFileApiUrl = emsFileApiUrlOriginal; - tilemapsConfig.deprecated = tilemapsConfigDeprecatedOriginal; + }; + }); }); + afterEach(() => { + mapConfigOptions = {}; + tilemapConfigOptions = {}; + }); + + function makeServiceSettings() { + const serviceSettings = new ServiceSettings(); + serviceSettings.__debugStubManifestCalls(async (url) => { + //simulate network calls + if (url.startsWith('https://tiles.foobar')) { + if (url.includes('/manifest')) { + return EMS_TILES; + } else if (url.includes('osm-bright-desaturated.json')) { + return EMS_STYLE_ROAD_MAP_DESATURATED; + } else if (url.includes('osm-bright.json')) { + return EMS_STYLE_ROAD_MAP_BRIGHT; + } else if (url.includes('dark-matter.json')) { + return EMS_STYLE_DARK_MAP; + } + } else if (url.startsWith('https://files.foobar')) { + return EMS_FILES; + } + }); + return serviceSettings; + } + describe('TMS', function () { it('should NOT get url from the config', async function () { + const serviceSettings = makeServiceSettings(); const tmsServices = await serviceSettings.getTMSServices(); const tmsService = tmsServices[0]; - expect(typeof tmsService.url === 'undefined').to.equal(true); + expect(typeof tmsService.url === 'undefined').toEqual(true); }); it('should get url by resolving dynamically', async function () { + const serviceSettings = makeServiceSettings(); const tmsServices = await serviceSettings.getTMSServices(); const tmsService = tmsServices[0]; - expect(typeof tmsService.url === 'undefined').to.equal(true); + expect(typeof tmsService.url === 'undefined').toEqual(true); const attrs = await serviceSettings.getAttributesForTMSLayer(tmsService); - expect(attrs.url).to.contain('{x}'); - expect(attrs.url).to.contain('{y}'); - expect(attrs.url).to.contain('{z}'); + expect(attrs.url.includes('{x}')).toEqual(true); + expect(attrs.url.includes('{y}')).toEqual(true); + expect(attrs.url.includes('{z}')).toEqual(true); const urlObject = url.parse(attrs.url, true); - expect(urlObject.hostname).to.be('tiles.foobar'); - expect(urlObject.query).to.have.property('my_app_name', 'kibana'); - expect(urlObject.query).to.have.property('elastic_tile_service_tos', 'agree'); - expect(urlObject.query).to.have.property('my_app_version'); + expect(urlObject.hostname).toEqual('tiles.foobar'); + expect(urlObject.query.my_app_name).toEqual('kibana'); + expect(urlObject.query.elastic_tile_service_tos).toEqual('agree'); + expect(typeof urlObject.query.my_app_version).toEqual('string'); }); it('should get options', async function () { + const serviceSettings = makeServiceSettings(); const tmsServices = await serviceSettings.getTMSServices(); const tmsService = tmsServices[0]; - expect(tmsService).to.have.property('minZoom'); - expect(tmsService).to.have.property('maxZoom'); - expect(tmsService).to.have.property('attribution').contain('OpenStreetMap'); + expect(typeof tmsService.minZoom).toEqual('number'); + expect(typeof tmsService.maxZoom).toEqual('number'); + expect(tmsService.attribution.includes('OpenStreetMap')).toEqual(true); }); describe('modify - url', function () { let tilemapServices; + let serviceSettings; async function assertQuery(expected) { const attrs = await serviceSettings.getAttributesForTMSLayer(tilemapServices[0]); const urlObject = url.parse(attrs.url, true); Object.keys(expected).forEach((key) => { - expect(urlObject.query).to.have.property(key, expected[key]); + expect(urlObject.query[key]).toEqual(expected[key]); }); } it('accepts an object', async () => { + serviceSettings = makeServiceSettings(); serviceSettings.setQueryParams({ foo: 'bar' }); tilemapServices = await serviceSettings.getTMSServices(); await assertQuery({ foo: 'bar' }); @@ -148,6 +154,7 @@ describe('service_settings (FKA tilemaptest)', function () { it('merged additions with previous values', async () => { // ensure that changes are always additive + serviceSettings = makeServiceSettings(); serviceSettings.setQueryParams({ foo: 'bar' }); serviceSettings.setQueryParams({ bar: 'stool' }); tilemapServices = await serviceSettings.getTMSServices(); @@ -155,6 +162,7 @@ describe('service_settings (FKA tilemaptest)', function () { }); it('overwrites conflicting previous values', async () => { + serviceSettings = makeServiceSettings(); // ensure that conflicts are overwritten serviceSettings.setQueryParams({ foo: 'bar' }); serviceSettings.setQueryParams({ bar: 'stool' }); @@ -163,24 +171,19 @@ describe('service_settings (FKA tilemaptest)', function () { await assertQuery({ foo: 'tstool', bar: 'stool' }); }); - it('when overridden, should continue to work', async () => { - mapConfig.emsFileApiUrl = emsFileApiUrl2; - mapConfig.emsTileApiUrl = emsTileApiUrl2; - serviceSettings.setQueryParams({ foo: 'bar' }); - tilemapServices = await serviceSettings.getTMSServices(); - await assertQuery({ foo: 'bar' }); - }); - it('should merge in tilemap url', async () => { - tilemapsConfig.deprecated = { - isOverridden: true, - config: { - url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', - options: { minZoom: 0, maxZoom: 20 }, + tilemapConfigOptions = { + deprecated: { + isOverridden: true, + config: { + url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', + options: { minZoom: 0, maxZoom: 20 }, + }, }, }; + serviceSettings = makeServiceSettings(); - tilemapServices = await serviceSettings.getTMSServices(); + const tilemapServices = await serviceSettings.getTMSServices(); const expected = [ { attribution: '', @@ -202,17 +205,18 @@ describe('service_settings (FKA tilemaptest)', function () { const assertions = tilemapServices.map(async (actualService, index) => { const expectedService = expected[index]; - expect(actualService.id).to.equal(expectedService.id); - expect(actualService.attribution).to.equal(expectedService.attribution); + expect(actualService.id).toEqual(expectedService.id); + expect(actualService.attribution).toEqual(expectedService.attribution); const attrs = await serviceSettings.getAttributesForTMSLayer(actualService); - expect(attrs.url).to.equal(expectedService.url); + expect(attrs.url).toEqual(expectedService.url); }); return Promise.all(assertions); }); it('should load appropriate EMS attributes for desaturated and dark theme', async () => { - tilemapServices = await serviceSettings.getTMSServices(); + serviceSettings = makeServiceSettings(); + const tilemapServices = await serviceSettings.getTMSServices(); const roadMapService = tilemapServices.find((service) => service.id === 'road_map'); const desaturationFalse = await serviceSettings.getAttributesForTMSLayer( @@ -236,35 +240,39 @@ describe('service_settings (FKA tilemaptest)', function () { true ); - expect(desaturationFalse.url).to.equal( + expect(desaturationFalse.url).toEqual( 'https://tiles.foobar/raster/styles/osm-bright/{z}/{x}/{y}.png?elastic_tile_service_tos=agree&my_app_name=kibana&my_app_version=1.2.3' ); - expect(desaturationFalse.maxZoom).to.equal(10); - expect(desaturationTrue.url).to.equal( + expect(desaturationFalse.maxZoom).toEqual(10); + expect(desaturationTrue.url).toEqual( 'https://tiles.foobar/raster/styles/osm-bright-desaturated/{z}/{x}/{y}.png?elastic_tile_service_tos=agree&my_app_name=kibana&my_app_version=1.2.3' ); - expect(desaturationTrue.maxZoom).to.equal(18); - expect(darkThemeDesaturationFalse.url).to.equal( + expect(desaturationTrue.maxZoom).toEqual(18); + expect(darkThemeDesaturationFalse.url).toEqual( 'https://tiles.foobar/raster/styles/dark-matter/{z}/{x}/{y}.png?elastic_tile_service_tos=agree&my_app_name=kibana&my_app_version=1.2.3' ); - expect(darkThemeDesaturationFalse.maxZoom).to.equal(22); - expect(darkThemeDesaturationTrue.url).to.equal( + expect(darkThemeDesaturationFalse.maxZoom).toEqual(22); + expect(darkThemeDesaturationTrue.url).toEqual( 'https://tiles.foobar/raster/styles/dark-matter/{z}/{x}/{y}.png?elastic_tile_service_tos=agree&my_app_name=kibana&my_app_version=1.2.3' ); - expect(darkThemeDesaturationTrue.maxZoom).to.equal(22); + expect(darkThemeDesaturationTrue.maxZoom).toEqual(22); }); it('should exclude EMS', async () => { - tilemapsConfig.deprecated = { - isOverridden: true, - config: { - url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', - options: { minZoom: 0, maxZoom: 20 }, + mapConfigOptions = { + includeElasticMapsService: false, + }; + tilemapConfigOptions = { + deprecated: { + isOverridden: true, + config: { + url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', + options: { minZoom: 0, maxZoom: 20 }, + }, }, }; - mapConfig.includeElasticMapsService = false; - - tilemapServices = await serviceSettings.getTMSServices(); + serviceSettings = makeServiceSettings(); + const tilemapServices = await serviceSettings.getTMSServices(); const expected = [ { attribution: '', @@ -272,33 +280,38 @@ describe('service_settings (FKA tilemaptest)', function () { id: 'TMS in config/kibana.yml', }, ]; - expect(tilemapServices.length).to.eql(1); - expect(tilemapServices[0].attribution).to.eql(expected[0].attribution); - expect(tilemapServices[0].id).to.eql(expected[0].id); + expect(tilemapServices.length).toEqual(1); + expect(tilemapServices[0].attribution).toEqual(expected[0].attribution); + expect(tilemapServices[0].id).toEqual(expected[0].id); const attrs = await serviceSettings.getAttributesForTMSLayer(tilemapServices[0]); - expect(attrs.url).to.equal(expected[0].url); + expect(attrs.url).toEqual(expected[0].url); }); it('should exclude all when not configured', async () => { - mapConfig.includeElasticMapsService = false; - tilemapServices = await serviceSettings.getTMSServices(); + mapConfigOptions = { + includeElasticMapsService: false, + }; + serviceSettings = makeServiceSettings(); + // mapConfig.includeElasticMapsService = false; + const tilemapServices = await serviceSettings.getTMSServices(); const expected = []; - expect(tilemapServices).to.eql(expected); + expect(tilemapServices).toEqual(expected); }); }); }); describe('File layers', function () { it('should load manifest (all props)', async function () { + const serviceSettings = makeServiceSettings(); serviceSettings.setQueryParams({ foo: 'bar' }); const fileLayers = await serviceSettings.getFileLayers(); - expect(fileLayers.length).to.be(18); + expect(fileLayers.length).toEqual(18); const assertions = fileLayers.map(async function (fileLayer) { - expect(fileLayer.origin).to.be(ORIGIN.EMS); + expect(fileLayer.origin).toEqual(ORIGIN.EMS); const fileUrl = await serviceSettings.getUrlForRegionLayer(fileLayer); const urlObject = url.parse(fileUrl, true); Object.keys({ foo: 'bar', elastic_tile_service_tos: 'agree' }).forEach((key) => { - expect(urlObject.query).to.have.property(key); + expect(typeof urlObject.query[key]).toEqual('string'); }); }); @@ -307,8 +320,7 @@ describe('service_settings (FKA tilemaptest)', function () { it('should load manifest (individual props)', async () => { const expected = { - attribution: - 'Made with NaturalEarth | Elastic Maps Service', + attribution: `Made with NaturalEarth | Elastic Maps Service`, format: 'geojson', fields: [ { type: 'id', name: 'iso2', description: 'ISO 3166-1 alpha-2 code' }, @@ -319,28 +331,33 @@ describe('service_settings (FKA tilemaptest)', function () { name: 'World Countries', }; + const serviceSettings = makeServiceSettings(); const fileLayers = await serviceSettings.getFileLayers(); const actual = fileLayers[0]; - expect(expected.attribution).to.eql(actual.attribution); - expect(expected.format).to.eql(actual.format); - expect(expected.fields).to.eql(actual.fields); - expect(expected.name).to.eql(actual.name); + expect(expected.attribution).toEqual(actual.attribution); + expect(expected.format).toEqual(actual.format); + expect(expected.fields).toEqual(actual.fields); + expect(expected.name).toEqual(actual.name); - expect(expected.created_at).to.eql(actual.created_at); + expect(expected.created_at).toEqual(actual.created_at); }); it('should exclude all when not configured', async () => { - mapConfig.includeElasticMapsService = false; + mapConfigOptions = { + includeElasticMapsService: false, + }; + const serviceSettings = makeServiceSettings(); const fileLayers = await serviceSettings.getFileLayers(); const expected = []; - expect(fileLayers).to.eql(expected); + expect(fileLayers).toEqual(expected); }); it('should get hotlink', async () => { + const serviceSettings = makeServiceSettings(); const fileLayers = await serviceSettings.getFileLayers(); const hotlink = await serviceSettings.getEMSHotLink(fileLayers[0]); - expect(hotlink).to.eql('?locale=en#file/world_countries'); //url host undefined becuase emsLandingPageUrl is set at kibana-load + expect(hotlink).toEqual('?locale=en#file/world_countries'); //url host undefined becuase emsLandingPageUrl is set at kibana-load }); }); });