From 27a9725edcc9df38af5c5807128a9602ff995788 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Thu, 17 Oct 2019 12:04:22 +0300 Subject: [PATCH] Convert agg_types/__tests__ to JEST (#47967) --- .../public/agg_types/__tests__/agg_params.js | 101 ---------- .../ui/public/agg_types/__tests__/agg_type.js | 189 ------------------ .../ui/public/agg_types/__tests__/index.js | 53 ----- .../agg_types/__tests__/param_types/_json.js | 116 ----------- .../__tests__/param_types/_string.js | 84 -------- .../ui/public/agg_types/agg_params.test.ts | 83 ++++++++ .../ui/public/agg_types/agg_type.test.ts | 172 ++++++++++++++++ .../param_types/index.js => index.test.ts} | 31 ++- .../_field.js => param_types/field.test.ts} | 72 ++++--- .../public/agg_types/param_types/json.test.ts | 119 +++++++++++ .../optioned.test.ts} | 16 +- .../agg_types/param_types/string.test.ts | 81 ++++++++ .../agg_types/{__tests__ => }/utils.test.tsx | 12 +- 13 files changed, 540 insertions(+), 589 deletions(-) delete mode 100644 src/legacy/ui/public/agg_types/__tests__/agg_params.js delete mode 100644 src/legacy/ui/public/agg_types/__tests__/agg_type.js delete mode 100644 src/legacy/ui/public/agg_types/__tests__/index.js delete mode 100644 src/legacy/ui/public/agg_types/__tests__/param_types/_json.js delete mode 100644 src/legacy/ui/public/agg_types/__tests__/param_types/_string.js create mode 100644 src/legacy/ui/public/agg_types/agg_params.test.ts create mode 100644 src/legacy/ui/public/agg_types/agg_type.test.ts rename src/legacy/ui/public/agg_types/{__tests__/param_types/index.js => index.test.ts} (53%) rename src/legacy/ui/public/agg_types/{__tests__/param_types/_field.js => param_types/field.test.ts} (51%) create mode 100644 src/legacy/ui/public/agg_types/param_types/json.test.ts rename src/legacy/ui/public/agg_types/{__tests__/param_types/_optioned.js => param_types/optioned.test.ts} (71%) create mode 100644 src/legacy/ui/public/agg_types/param_types/string.test.ts rename src/legacy/ui/public/agg_types/{__tests__ => }/utils.test.tsx (81%) diff --git a/src/legacy/ui/public/agg_types/__tests__/agg_params.js b/src/legacy/ui/public/agg_types/__tests__/agg_params.js deleted file mode 100644 index d4fe2a663543a..0000000000000 --- a/src/legacy/ui/public/agg_types/__tests__/agg_params.js +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import expect from '@kbn/expect'; -import { initParams } from '../agg_params'; -import { BaseParamType } from '../param_types/base'; -import { FieldParamType } from '../param_types/field'; -import { OptionedParamType } from '../param_types/optioned'; - -describe('AggParams class', function () { - - describe('constructor args', function () { - it('accepts an array of param defs', function () { - const params = [ - { name: 'one' }, - { name: 'two' } - ]; - const aggParams = initParams(params); - - expect(aggParams).to.have.length(params.length); - expect(aggParams).to.be.an(Array); - }); - }); - - describe('AggParam creation', function () { - it('Uses the FieldParamType class for params with the name "field"', function () { - const params = [ - { name: 'field', type: 'field' } - ]; - const aggParams = initParams(params); - - expect(aggParams).to.have.length(params.length); - expect(aggParams[0]).to.be.a(FieldParamType); - }); - - it('Uses the OptionedParamType class for params of type "optioned"', function () { - const params = [ - { - name: 'interval', - type: 'optioned' - } - ]; - const aggParams = initParams(params); - - expect(aggParams).to.have.length(params.length); - expect(aggParams[0]).to.be.a(OptionedParamType); - }); - - it('Uses the OptionedParamType class for params of type "optioned"', function () { - const params = [ - { - name: 'order', - type: 'optioned' - } - ]; - const aggParams = initParams(params); - - expect(aggParams).to.have.length(params.length); - expect(aggParams[0]).to.be.a(OptionedParamType); - }); - - it('Always converts the params to a BaseParamType', function () { - const params = [ - { - name: 'height', - editor: 'high' - }, - { - name: 'weight', - editor: 'big' - }, - { - name: 'waist', - editor: 'small' - } - ]; - const aggParams = initParams(params); - - expect(aggParams).to.have.length(params.length); - aggParams.forEach(function (aggParam) { - expect(aggParam).to.be.a(BaseParamType); - }); - }); - }); -}); diff --git a/src/legacy/ui/public/agg_types/__tests__/agg_type.js b/src/legacy/ui/public/agg_types/__tests__/agg_type.js deleted file mode 100644 index 81daa9b54fa43..0000000000000 --- a/src/legacy/ui/public/agg_types/__tests__/agg_type.js +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import _ from 'lodash'; -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import '../../private'; -import { VisProvider } from '../../vis'; -import { fieldFormats } from '../../registry/field_formats'; -import { AggType } from '../agg_type'; -import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; - -describe('AggType Class', function () { - let indexPattern; - let Vis; - - - beforeEach(ngMock.module('kibana')); - beforeEach(ngMock.inject(function (Private) { - - Vis = Private(VisProvider); - indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider); - })); - - describe('constructor', function () { - - it('requires a config object as it\'s first param', function () { - expect(function () { - new AggType(null); - }).to.throwError(); - }); - - describe('application of config properties', function () { - const copiedConfigProps = [ - 'name', - 'title', - 'makeLabel', - 'ordered' - ]; - - describe('"' + copiedConfigProps.join('", "') + '"', function () { - it('assigns the config value to itself', function () { - const config = _.transform(copiedConfigProps, function (config, prop) { - config[prop] = {}; - }, {}); - - const aggType = new AggType(config); - - copiedConfigProps.forEach(function (prop) { - expect(aggType[prop]).to.be(config[prop]); - }); - }); - }); - - describe('makeLabel', function () { - it('makes a function when the makeLabel config is not specified', function () { - const someGetter = function () {}; - - let aggType = new AggType({ - makeLabel: someGetter - }); - - expect(aggType.makeLabel).to.be(someGetter); - - aggType = new AggType({ - name: 'pizza' - }); - - expect(aggType.makeLabel).to.be.a('function'); - expect(aggType.makeLabel()).to.be('pizza'); - }); - }); - - describe('getFormat', function () { - it('returns the formatter for the aggConfig', function () { - const aggType = new AggType({}); - - let vis = new Vis(indexPattern, { - aggs: [ - { - type: 'date_histogram', - schema: 'segment' - } - ] - }); - let aggConfig = vis.aggs.byName('date_histogram')[0]; - - expect(aggType.getFormat(aggConfig)).to.be(fieldFormats.getDefaultInstance('date')); - - vis = new Vis(indexPattern, { - aggs: [ - { - type: 'count', - schema: 'metric' - } - ] - }); - aggConfig = vis.aggs.byName('count')[0]; - - expect(aggType.getFormat(aggConfig)).to.be(fieldFormats.getDefaultInstance('string')); - }); - - it('can be overridden via config', function () { - const someGetter = function () {}; - - const aggType = new AggType({ - getFormat: someGetter - }); - - expect(aggType.getFormat).to.be(someGetter); - }); - }); - - describe('params', function () { - - it('defaults to AggParams object with JSON param', function () { - const aggType = new AggType({ - name: 'smart agg' - }); - - expect(aggType.params).to.be.an(Array); - expect(aggType.params.length).to.be(2); - expect(aggType.params[0].name).to.be('json'); - expect(aggType.params[1].name).to.be('customLabel'); - }); - - it('can disable customLabel', function () { - const aggType = new AggType({ - name: 'smart agg', - customLabels: false - }); - - expect(aggType.params.length).to.be(1); - expect(aggType.params[0].name).to.be('json'); - }); - - it('passes the params arg directly to the AggParams constructor', function () { - const params = [ - { name: 'one' }, - { name: 'two' } - ]; - const paramLength = params.length + 2; // json and custom label are always appended - - const aggType = new AggType({ - name: 'bucketeer', - params: params - }); - - expect(aggType.params).to.be.an(Array); - expect(aggType.params.length).to.be(paramLength); - }); - }); - - describe('getResponseAggs', function () { - it('copies the value', function () { - const football = {}; - const aggType = new AggType({ - getResponseAggs: football - }); - - expect(aggType.getResponseAggs).to.be(football); - }); - - it('defaults to noop', function () { - const aggType = new AggType({}); - const responseAggs = aggType.getRequestAggs(); - expect(responseAggs).to.be(undefined); - }); - }); - }); - - }); -}); diff --git a/src/legacy/ui/public/agg_types/__tests__/index.js b/src/legacy/ui/public/agg_types/__tests__/index.js deleted file mode 100644 index c977eb6eeb610..0000000000000 --- a/src/legacy/ui/public/agg_types/__tests__/index.js +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import expect from '@kbn/expect'; -import './agg_type'; -import './agg_params'; -import './buckets/_histogram'; -import './buckets/_geo_hash'; -import './buckets/_range'; -import './buckets/_terms_other_bucket_helper'; -import './buckets/date_histogram/_editor'; -import './buckets/date_histogram/_params'; -import { aggTypes } from '..'; -import { BucketAggType } from '../buckets/_bucket_agg_type'; -import { MetricAggType } from '../metrics/metric_agg_type'; - -const bucketAggs = aggTypes.buckets; -const metricAggs = aggTypes.metrics; - -describe('AggTypesComponent', function () { - - describe('bucket aggs', function () { - it('all extend BucketAggType', function () { - bucketAggs.forEach(function (bucketAgg) { - expect(bucketAgg).to.be.a(BucketAggType); - }); - }); - }); - - describe('metric aggs', function () { - it('all extend MetricAggType', function () { - metricAggs.forEach(function (metricAgg) { - expect(metricAgg).to.be.a(MetricAggType); - }); - }); - }); -}); diff --git a/src/legacy/ui/public/agg_types/__tests__/param_types/_json.js b/src/legacy/ui/public/agg_types/__tests__/param_types/_json.js deleted file mode 100644 index 1876593f52956..0000000000000 --- a/src/legacy/ui/public/agg_types/__tests__/param_types/_json.js +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import _ from 'lodash'; -import expect from '@kbn/expect'; -import { BaseParamType } from '../../param_types/base'; -import { JsonParamType } from '../../param_types/json'; - -// eslint-disable-next-line import/no-default-export -export default describe('JSON', function () { - const paramName = 'json_test'; - let aggParam; - let aggConfig; - let output; - - function initParamType(config) { - config = config || {}; - const defaults = { - name: paramName, - type: 'json' - }; - - aggParam = new JsonParamType(_.defaults(config, defaults)); - } - - // fetch out deps - beforeEach(function () { - aggConfig = { params: {} }; - output = { params: {} }; - - - initParamType(); - }); - - describe('constructor', function () { - it('it is an instance of BaseParamType', function () { - expect(aggParam).to.be.a(BaseParamType); - }); - }); - - describe('write', function () { - it('should do nothing when param is not defined', function () { - expect(aggConfig.params).not.to.have.property(paramName); - - aggParam.write(aggConfig, output); - expect(output).not.to.have.property(paramName); - }); - - it('should not append param when invalid JSON', function () { - aggConfig.params[paramName] = 'i am not json'; - - aggParam.write(aggConfig, output); - expect(aggConfig.params).to.have.property(paramName); - expect(output).not.to.have.property(paramName); - }); - - it('should append param when valid JSON', function () { - const jsonData = JSON.stringify({ - new_param: 'should exist in output' - }); - - output.params.existing = 'true'; - aggConfig.params[paramName] = jsonData; - - aggParam.write(aggConfig, output); - expect(aggConfig.params).to.have.property(paramName); - expect(output.params).to.eql({ - existing: 'true', - new_param: 'should exist in output' - }); - }); - - it('should not overwrite existing params', function () { - const jsonData = JSON.stringify({ - new_param: 'should exist in output', - existing: 'should be used' - }); - - output.params.existing = 'true'; - aggConfig.params[paramName] = jsonData; - - aggParam.write(aggConfig, output); - expect(output.params).to.eql(JSON.parse(jsonData)); - }); - - it('should drop nulled params', function () { - const jsonData = JSON.stringify({ - new_param: 'should exist in output', - field: null - }); - - output.params.field = 'extensions'; - aggConfig.params[paramName] = jsonData; - - aggParam.write(aggConfig, output); - expect(Object.keys(output.params)).to.contain('new_param'); - expect(Object.keys(output.params)).to.not.contain('field'); - }); - }); -}); diff --git a/src/legacy/ui/public/agg_types/__tests__/param_types/_string.js b/src/legacy/ui/public/agg_types/__tests__/param_types/_string.js deleted file mode 100644 index 10c965a53bab5..0000000000000 --- a/src/legacy/ui/public/agg_types/__tests__/param_types/_string.js +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import _ from 'lodash'; -import expect from '@kbn/expect'; -import { BaseParamType } from '../../param_types/base'; -import { StringParamType } from '../../param_types/string'; - -// eslint-disable-next-line import/no-default-export -export default describe('String', function () { - const paramName = 'json_test'; - let aggParam; - let aggConfig; - let output; - - function initAggParam(config) { - config = config || {}; - const defaults = { - name: paramName, - type: 'string' - }; - - aggParam = new StringParamType(_.defaults(config, defaults)); - } - - - // fetch our deps - beforeEach(function () { - - aggConfig = { params: {} }; - output = { params: {} }; - }); - - describe('constructor', function () { - it('it is an instance of BaseParamType', function () { - initAggParam(); - expect(aggParam).to.be.a(BaseParamType); - }); - }); - - describe('write', function () { - it('should append param by name', function () { - const paramName = 'testing'; - const params = {}; - params[paramName] = 'some input'; - - initAggParam({ name: paramName }); - - aggConfig.params = params; - aggParam.write(aggConfig, output); - - expect(output.params).to.eql(params); - }); - - it('should not be in output with empty input', function () { - const paramName = 'more_testing'; - const params = {}; - params[paramName] = ''; - - initAggParam({ name: paramName }); - - aggConfig.params = params; - aggParam.write(aggConfig, output); - - expect(output.params).to.eql({}); - }); - }); -}); diff --git a/src/legacy/ui/public/agg_types/agg_params.test.ts b/src/legacy/ui/public/agg_types/agg_params.test.ts new file mode 100644 index 0000000000000..28d852c7f2567 --- /dev/null +++ b/src/legacy/ui/public/agg_types/agg_params.test.ts @@ -0,0 +1,83 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { AggParam, initParams } from './agg_params'; +import { BaseParamType } from './param_types/base'; +import { FieldParamType } from './param_types/field'; +import { OptionedParamType } from './param_types/optioned'; + +jest.mock('ui/new_platform'); + +describe('AggParams class', () => { + describe('constructor args', () => { + it('accepts an array of param defs', () => { + const params = [{ name: 'one' }, { name: 'two' }] as AggParam[]; + const aggParams = initParams(params); + + expect(aggParams).toHaveLength(params.length); + expect(Array.isArray(aggParams)).toBeTruthy(); + }); + }); + + describe('AggParam creation', () => { + it('Uses the FieldParamType class for params with the name "field"', () => { + const params = [{ name: 'field', type: 'field' }] as AggParam[]; + const aggParams = initParams(params); + + expect(aggParams).toHaveLength(params.length); + expect(aggParams[0] instanceof FieldParamType).toBeTruthy(); + }); + + it('Uses the OptionedParamType class for params of type "optioned"', () => { + const params = [ + { + name: 'order', + type: 'optioned', + }, + ]; + const aggParams = initParams(params); + + expect(aggParams).toHaveLength(params.length); + expect(aggParams[0] instanceof OptionedParamType).toBeTruthy(); + }); + + it('Always converts the params to a BaseParamType', function() { + const params = [ + { + name: 'height', + displayName: 'height', + }, + { + name: 'weight', + displayName: 'weight', + }, + { + name: 'waist', + displayName: 'waist', + }, + ] as AggParam[]; + + const aggParams = initParams(params); + + expect(aggParams).toHaveLength(params.length); + + aggParams.forEach(aggParam => expect(aggParam instanceof BaseParamType).toBeTruthy()); + }); + }); +}); diff --git a/src/legacy/ui/public/agg_types/agg_type.test.ts b/src/legacy/ui/public/agg_types/agg_type.test.ts new file mode 100644 index 0000000000000..1c1453b74fe98 --- /dev/null +++ b/src/legacy/ui/public/agg_types/agg_type.test.ts @@ -0,0 +1,172 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { AggType, AggTypeConfig } from './agg_type'; +import { AggConfig } from './agg_config'; + +jest.mock('ui/new_platform'); + +jest.mock('ui/registry/field_formats', () => ({ + fieldFormats: { + getDefaultInstance: jest.fn(() => 'default'), + }, +})); + +describe('AggType Class', () => { + describe('constructor', () => { + it("requires a valid config object as it's first param", () => { + expect(() => { + const aggConfig: AggTypeConfig = (undefined as unknown) as AggTypeConfig; + new AggType(aggConfig); + }).toThrowError(); + }); + + describe('application of config properties', () => { + it('assigns the config value to itself', () => { + const config: AggTypeConfig = { + name: 'name', + title: 'title', + }; + + const aggType = new AggType(config); + + expect(aggType.name).toBe('name'); + expect(aggType.title).toBe('title'); + }); + + describe('makeLabel', () => { + it('makes a function when the makeLabel config is not specified', () => { + const makeLabel = () => 'label'; + const aggConfig = {} as AggConfig; + const config: AggTypeConfig = { + name: 'name', + title: 'title', + makeLabel, + }; + + const aggType = new AggType(config); + + expect(aggType.makeLabel).toBe(makeLabel); + expect(aggType.makeLabel(aggConfig)).toBe('label'); + }); + }); + + describe('getResponseAggs/getRequestAggs', () => { + it('copies the value', () => { + const testConfig = (aggConfig: AggConfig) => [aggConfig]; + + const aggType = new AggType({ + name: 'name', + title: 'title', + getResponseAggs: testConfig, + getRequestAggs: testConfig, + }); + + expect(aggType.getResponseAggs).toBe(testConfig); + expect(aggType.getResponseAggs).toBe(testConfig); + }); + + it('defaults to noop', () => { + const aggConfig = {} as AggConfig; + const aggType = new AggType({ + name: 'name', + title: 'title', + }); + const responseAggs = aggType.getRequestAggs(aggConfig); + + expect(responseAggs).toBe(undefined); + }); + }); + + describe('params', () => { + it('defaults to AggParams object with JSON param', () => { + const aggType = new AggType({ + name: 'smart agg', + title: 'title', + }); + + expect(Array.isArray(aggType.params)).toBeTruthy(); + expect(aggType.params.length).toBe(2); + expect(aggType.params[0].name).toBe('json'); + expect(aggType.params[1].name).toBe('customLabel'); + }); + + it('can disable customLabel', () => { + const aggType = new AggType({ + name: 'smart agg', + title: 'title', + customLabels: false, + }); + + expect(aggType.params.length).toBe(1); + expect(aggType.params[0].name).toBe('json'); + }); + + it('passes the params arg directly to the AggParams constructor', () => { + const params = [{ name: 'one' }, { name: 'two' }]; + const paramLength = params.length + 2; // json and custom label are always appended + + const aggType = new AggType({ + name: 'bucketeer', + title: 'title', + params, + }); + + expect(Array.isArray(aggType.params)).toBeTruthy(); + expect(aggType.params.length).toBe(paramLength); + }); + }); + }); + + describe('getFormat', function() { + let aggConfig: AggConfig; + let field: any; + + beforeEach(() => { + aggConfig = ({ + getField: jest.fn(() => field), + } as unknown) as AggConfig; + }); + + it('returns the formatter for the aggConfig', () => { + const aggType = new AggType({ + name: 'name', + title: 'title', + }); + + field = { + format: 'format', + }; + + expect(aggType.getFormat(aggConfig)).toBe('format'); + }); + + it('returns default formatter', () => { + const aggType = new AggType({ + name: 'name', + title: 'title', + }); + + field = undefined; + + expect(aggType.getFormat(aggConfig)).toBe('default'); + }); + }); + }); +}); diff --git a/src/legacy/ui/public/agg_types/__tests__/param_types/index.js b/src/legacy/ui/public/agg_types/index.test.ts similarity index 53% rename from src/legacy/ui/public/agg_types/__tests__/param_types/index.js rename to src/legacy/ui/public/agg_types/index.test.ts index 507df89960c6f..a867769a77fc1 100644 --- a/src/legacy/ui/public/agg_types/__tests__/param_types/index.js +++ b/src/legacy/ui/public/agg_types/index.test.ts @@ -17,9 +17,30 @@ * under the License. */ -import './_field'; -import './_optioned'; -import './_string'; -import './_json'; -describe('ParamTypes', function () { +import { aggTypes } from './index'; + +import { isBucketAggType } from './buckets/_bucket_agg_type'; +import { isMetricAggType } from './metrics/metric_agg_type'; + +const bucketAggs = aggTypes.buckets; +const metricAggs = aggTypes.metrics; + +jest.mock('ui/new_platform'); + +describe('AggTypesComponent', () => { + describe('bucket aggs', () => { + it('all extend BucketAggType', () => { + bucketAggs.forEach(bucketAgg => { + expect(isBucketAggType(bucketAgg)).toBeTruthy(); + }); + }); + }); + + describe('metric aggs', () => { + it('all extend MetricAggType', () => { + metricAggs.forEach(metricAgg => { + expect(isMetricAggType(metricAgg)).toBeTruthy(); + }); + }); + }); }); diff --git a/src/legacy/ui/public/agg_types/__tests__/param_types/_field.js b/src/legacy/ui/public/agg_types/param_types/field.test.ts similarity index 51% rename from src/legacy/ui/public/agg_types/__tests__/param_types/_field.js rename to src/legacy/ui/public/agg_types/param_types/field.test.ts index 94a976f98e984..2434f95056b78 100644 --- a/src/legacy/ui/public/agg_types/__tests__/param_types/_field.js +++ b/src/legacy/ui/public/agg_types/param_types/field.test.ts @@ -17,56 +17,74 @@ * under the License. */ -import expect from '@kbn/expect'; -import { reject } from 'lodash'; -import ngMock from 'ng_mock'; -import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; -import { BaseParamType } from '../../param_types/base'; -import { FieldParamType } from '../../param_types/field'; +import { BaseParamType } from './base'; +import { FieldParamType } from './field'; +import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../../../../../plugins/data/common'; -describe('Field', function () { +jest.mock('ui/new_platform'); - let indexPattern; +describe('Field', () => { + const indexPattern = { + id: '1234', + title: 'logstash-*', + fields: [ + { + name: 'field1', + type: KBN_FIELD_TYPES.NUMBER, + esTypes: [ES_FIELD_TYPES.INTEGER], + aggregatable: true, + filterable: true, + searchable: true, + }, + { + name: 'field2', + type: KBN_FIELD_TYPES.STRING, + esTypes: [ES_FIELD_TYPES.TEXT], + aggregatable: false, + filterable: false, + searchable: true, + }, + ], + } as any; - beforeEach(ngMock.module('kibana')); - // fetch out deps - beforeEach(ngMock.inject(function (Private) { - indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider); - })); - - describe('constructor', function () { - it('it is an instance of BaseParamType', function () { + describe('constructor', () => { + it('it is an instance of BaseParamType', () => { const aggParam = new FieldParamType({ - name: 'field', type: 'field' + name: 'field', + type: 'field', }); - expect(aggParam).to.be.a(BaseParamType); + expect(aggParam instanceof BaseParamType).toBeTruthy(); }); }); - describe('getAvailableFields', function () { - it('should return only aggregatable fields by default', function () { + describe('getAvailableFields', () => { + it('should return only aggregatable fields by default', () => { const aggParam = new FieldParamType({ - name: 'field', type: 'field' + name: 'field', + type: 'field', }); const fields = aggParam.getAvailableFields(indexPattern.fields); - expect(fields).to.not.have.length(0); + + expect(fields.length).toBe(1); + for (const field of fields) { - expect(field.aggregatable).to.be(true); + expect(field.aggregatable).toBe(true); } }); - it('should return all fields if onlyAggregatable is false', function () { + it('should return all fields if onlyAggregatable is false', () => { const aggParam = new FieldParamType({ - name: 'field', type: 'field' + name: 'field', + type: 'field', }); aggParam.onlyAggregatable = false; const fields = aggParam.getAvailableFields(indexPattern.fields); - const nonAggregatableFields = reject(fields, 'aggregatable'); - expect(nonAggregatableFields).to.not.be.empty(); + + expect(fields.length).toBe(2); }); }); }); diff --git a/src/legacy/ui/public/agg_types/param_types/json.test.ts b/src/legacy/ui/public/agg_types/param_types/json.test.ts new file mode 100644 index 0000000000000..fb31385505a76 --- /dev/null +++ b/src/legacy/ui/public/agg_types/param_types/json.test.ts @@ -0,0 +1,119 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { BaseParamType } from './base'; +import { JsonParamType } from './json'; +import { AggConfig } from 'ui/agg_types'; + +jest.mock('ui/new_platform'); + +describe('JSON', function() { + const paramName = 'json_test'; + let aggConfig: AggConfig; + let output: Record; + + function initAggParam(config: Record = {}) { + return new JsonParamType({ + ...config, + type: 'json', + name: paramName, + }); + } + + beforeEach(function() { + aggConfig = { params: {} } as AggConfig; + output = { params: {} }; + }); + + describe('constructor', () => { + it('it is an instance of BaseParamType', () => { + const aggParam = initAggParam(); + + expect(aggParam instanceof BaseParamType).toBeTruthy(); + }); + }); + + describe('write', () => { + it('should do nothing when param is not defined', () => { + const aggParam = initAggParam(); + + expect(aggConfig.params).not.toHaveProperty(paramName); + + aggParam.write(aggConfig, output); + expect(output).not.toHaveProperty(paramName); + }); + + it('should not append param when invalid JSON', () => { + const aggParam = initAggParam(); + + aggConfig.params[paramName] = 'i am not json'; + + aggParam.write(aggConfig, output); + expect(aggConfig.params).toHaveProperty(paramName); + expect(output).not.toHaveProperty(paramName); + }); + + it('should append param when valid JSON', () => { + const aggParam = initAggParam(); + const jsonData = JSON.stringify({ + new_param: 'should exist in output', + }); + + output.params.existing = 'true'; + aggConfig.params[paramName] = jsonData; + + aggParam.write(aggConfig, output); + expect(aggConfig.params).toHaveProperty(paramName); + + expect(output.params).toEqual({ + existing: 'true', + new_param: 'should exist in output', + }); + }); + + it('should not overwrite existing params', () => { + const aggParam = initAggParam(); + const jsonData = JSON.stringify({ + new_param: 'should exist in output', + existing: 'should be used', + }); + + output.params.existing = 'true'; + aggConfig.params[paramName] = jsonData; + + aggParam.write(aggConfig, output); + expect(output.params).toEqual(JSON.parse(jsonData)); + }); + + it('should drop nulled params', () => { + const aggParam = initAggParam(); + const jsonData = JSON.stringify({ + new_param: 'should exist in output', + field: null, + }); + + output.params.field = 'extensions'; + aggConfig.params[paramName] = jsonData; + + aggParam.write(aggConfig, output); + expect(Object.keys(output.params)).toContain('new_param'); + expect(Object.keys(output.params)).not.toContain('field'); + }); + }); +}); diff --git a/src/legacy/ui/public/agg_types/__tests__/param_types/_optioned.js b/src/legacy/ui/public/agg_types/param_types/optioned.test.ts similarity index 71% rename from src/legacy/ui/public/agg_types/__tests__/param_types/_optioned.js rename to src/legacy/ui/public/agg_types/param_types/optioned.test.ts index 4e66f6cfbd41b..6b58d81914097 100644 --- a/src/legacy/ui/public/agg_types/__tests__/param_types/_optioned.js +++ b/src/legacy/ui/public/agg_types/param_types/optioned.test.ts @@ -17,20 +17,20 @@ * under the License. */ -import expect from '@kbn/expect'; -import { BaseParamType } from '../../param_types/base'; -import { OptionedParamType } from '../../param_types/optioned'; +import { BaseParamType } from './base'; +import { OptionedParamType } from './optioned'; -describe('Optioned', function () { +jest.mock('ui/new_platform'); - describe('constructor', function () { - it('it is an instance of BaseParamType', function () { +describe('Optioned', () => { + describe('constructor', () => { + it('it is an instance of BaseParamType', () => { const aggParam = new OptionedParamType({ name: 'some_param', - type: 'optioned' + type: 'optioned', }); - expect(aggParam).to.be.a(BaseParamType); + expect(aggParam instanceof BaseParamType).toBeTruthy(); }); }); }); diff --git a/src/legacy/ui/public/agg_types/param_types/string.test.ts b/src/legacy/ui/public/agg_types/param_types/string.test.ts new file mode 100644 index 0000000000000..3d496ecf898e4 --- /dev/null +++ b/src/legacy/ui/public/agg_types/param_types/string.test.ts @@ -0,0 +1,81 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { BaseParamType } from './base'; +import { StringParamType } from './string'; +import { AggConfig } from 'ui/agg_types'; + +jest.mock('ui/new_platform'); + +describe('String', function() { + let paramName = 'json_test'; + let aggConfig: AggConfig; + let output: Record; + + function initAggParam(config: Record = {}) { + return new StringParamType({ + ...config, + type: 'string', + name: paramName, + }); + } + + beforeEach(() => { + aggConfig = { params: {} } as AggConfig; + output = { params: {} }; + }); + + describe('constructor', () => { + it('it is an instance of BaseParamType', () => { + const aggParam = initAggParam(); + + expect(aggParam instanceof BaseParamType).toBeTruthy(); + }); + }); + + describe('write', () => { + it('should append param by name', () => { + const params = { + [paramName]: 'some input', + }; + + const aggParam = initAggParam({ name: paramName }); + + aggConfig.params = params; + aggParam.write(aggConfig, output); + + expect(output.params).toEqual(params); + }); + + it('should not be in output with empty input', () => { + paramName = 'more_testing'; + + const params = { + [paramName]: '', + }; + + const aggParam = initAggParam({ name: paramName }); + + aggConfig.params = params; + aggParam.write(aggConfig, output); + + expect(output.params).toEqual({}); + }); + }); +}); diff --git a/src/legacy/ui/public/agg_types/__tests__/utils.test.tsx b/src/legacy/ui/public/agg_types/utils.test.tsx similarity index 81% rename from src/legacy/ui/public/agg_types/__tests__/utils.test.tsx rename to src/legacy/ui/public/agg_types/utils.test.tsx index 655b606bb46b0..a3c7f24f3927d 100644 --- a/src/legacy/ui/public/agg_types/__tests__/utils.test.tsx +++ b/src/legacy/ui/public/agg_types/utils.test.tsx @@ -17,7 +17,7 @@ * under the License. */ -import { isValidJson } from '../utils'; +import { isValidJson } from './utils'; jest.mock('ui/new_platform'); @@ -29,23 +29,23 @@ const input = { describe('AggType utils', () => { describe('isValidJson', () => { it('should return true when empty string', () => { - expect(isValidJson('')).toBe(true); + expect(isValidJson('')).toBeTruthy(); }); it('should return true when undefine', () => { - expect(isValidJson(undefined as any)).toBe(true); + expect(isValidJson(undefined as any)).toBeTruthy(); }); it('should return false when invalid string', () => { - expect(isValidJson(input.invalid)).toBe(false); + expect(isValidJson(input.invalid)).toBeFalsy(); }); it('should return true when valid string', () => { - expect(isValidJson(input.valid)).toBe(true); + expect(isValidJson(input.valid)).toBeTruthy(); }); it('should return false if a number', () => { - expect(isValidJson('0')).toBe(false); + expect(isValidJson('0')).toBeFalsy(); }); }); });