From 7bded17357e10f0bc0b84e749465b7c77824e595 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 7 Jul 2020 20:12:00 -0400 Subject: [PATCH] Migrate service settings test to jest (#70992) Karma tests were no longer being run after NP-migration. --- .../service_settings.test.js} | 283 +++++++++--------- 1 file changed, 142 insertions(+), 141 deletions(-) rename src/plugins/maps_legacy/public/{__tests__/map/service_settings.js => map/service_settings.test.js} (55%) 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 55% 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 72c4323ed0736..01facdc54137e 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,118 @@ * under the License. */ -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import url from 'url'; +jest.mock('../kibana_services', () => ({ + getKibanaVersion() { + return '1.2.3'; + }, +})); -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'; +import url from 'url'; -describe('service_settings (FKA tilemaptest)', function () { - let serviceSettings; - let mapConfig; - let tilemapsConfig; +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 () { 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; + const defaultMapConfig = { + emsFileApiUrl, + emsTileApiUrl, + includeElasticMapsService: true, + emsTileLayerId: { + bright: 'road_map', + desaturated: 'road_map_desaturated', + dark: 'dark_map', + }, + }; + + const defaultTilemapConfig = { + deprecated: { + config: { + options: {}, + }, + }, + }; + + function makeServiceSettings(mapConfigOptions = {}, tilemapOptions = {}) { + const serviceSettings = new ServiceSettings( + { ...defaultMapConfig, ...mapConfigOptions }, + { ...defaultTilemapConfig, ...tilemapOptions } + ); + 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; } - }); - 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; - }); + } 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 +136,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 +144,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 +153,21 @@ 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 }, - }, - }; + serviceSettings = makeServiceSettings( + {}, + { + deprecated: { + isOverridden: true, + config: { + url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', + options: { minZoom: 0, maxZoom: 20 }, + }, + }, + } + ); - tilemapServices = await serviceSettings.getTMSServices(); + const tilemapServices = await serviceSettings.getTMSServices(); const expected = [ { attribution: '', @@ -202,17 +189,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 +224,40 @@ 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 }, + serviceSettings = makeServiceSettings( + { + includeElasticMapsService: false, }, - }; - mapConfig.includeElasticMapsService = false; - - tilemapServices = await serviceSettings.getTMSServices(); + { + deprecated: { + isOverridden: true, + config: { + url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', + options: { minZoom: 0, maxZoom: 20 }, + }, + }, + } + ); + const tilemapServices = await serviceSettings.getTMSServices(); const expected = [ { attribution: '', @@ -272,33 +265,37 @@ 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(); + serviceSettings = makeServiceSettings({ + includeElasticMapsService: false, + }); + // 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'); }); }); @@ -308,7 +305,7 @@ describe('service_settings (FKA tilemaptest)', function () { it('should load manifest (individual props)', async () => { const expected = { attribution: - 'Made with NaturalEarth | Elastic Maps Service', + 'Made with NaturalEarth | Elastic Maps Service', format: 'geojson', fields: [ { type: 'id', name: 'iso2', description: 'ISO 3166-1 alpha-2 code' }, @@ -319,28 +316,32 @@ 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; + const serviceSettings = makeServiceSettings({ + includeElasticMapsService: false, + }); 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 }); }); });