diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js b/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js deleted file mode 100644 index e397bda763f1..000000000000 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_colors_from_palette.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 expect from '@kbn/expect'; -import { getColorsFromPalette } from '../../lib/get_colors_from_palette'; -import { - grayscalePalette, - gradientPalette, -} from '../../../canvas_plugin_src/functions/common/__tests__/fixtures/test_styles'; - -describe('getColorsFromPalette', () => { - it('returns the array of colors from a palette object when gradient is false', () => { - expect(getColorsFromPalette(grayscalePalette, 20)).to.eql(grayscalePalette.colors); - }); - - it('returns an array of colors with equidistant colors with length equal to the number of series when gradient is true', () => { - const result = getColorsFromPalette(gradientPalette, 16); - expect(result) - .to.have.length(16) - .and.to.eql([ - '#ffffff', - '#eeeeee', - '#dddddd', - '#cccccc', - '#bbbbbb', - '#aaaaaa', - '#999999', - '#888888', - '#777777', - '#666666', - '#555555', - '#444444', - '#333333', - '#222222', - '#111111', - '#000000', - ]); - }); -}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_legend_config.js b/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_legend_config.js deleted file mode 100644 index ba43db7a8367..000000000000 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_legend_config.js +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 expect from '@kbn/expect'; -import { getLegendConfig } from '../get_legend_config'; - -describe('getLegendConfig', () => { - describe('show', () => { - it('hides the legend', () => { - expect(getLegendConfig(false, 2)) - .to.only.have.key('show') - .and.to.have.property('show', false); - expect(getLegendConfig(false, 10)) - .to.only.have.key('show') - .and.to.have.property('show', false); - }); - - it('hides the legend when there are less than 2 series', () => { - expect(getLegendConfig(false, 1)) - .to.only.have.key('show') - .and.to.have.property('show', false); - expect(getLegendConfig(true, 1)) - .to.only.have.key('show') - .and.to.have.property('show', false); - }); - - it('shows the legend when there are two or more series', () => { - expect(getLegendConfig('sw', 2)).to.have.property('show', true); - expect(getLegendConfig(true, 5)).to.have.property('show', true); - }); - }); - - describe('position', () => { - it('sets the position of the legend', () => { - expect(getLegendConfig('nw')).to.have.property('position', 'nw'); - expect(getLegendConfig('ne')).to.have.property('position', 'ne'); - expect(getLegendConfig('sw')).to.have.property('position', 'sw'); - expect(getLegendConfig('se')).to.have.property('position', 'se'); - }); - - it("defaults to 'ne'", () => { - expect(getLegendConfig(true)).to.have.property('position', 'ne'); - expect(getLegendConfig('foo')).to.have.property('position', 'ne'); - }); - }); -}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/autocomplete.test.ts b/x-pack/legacy/plugins/canvas/common/lib/autocomplete.test.ts index 616e45c86c4a..88bb32c846c2 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/autocomplete.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/autocomplete.test.ts @@ -7,184 +7,195 @@ jest.mock('ui/new_platform'); import { functionSpecs } from '../../__tests__/fixtures/function_specs'; -import { getAutocompleteSuggestions } from './autocomplete'; +import { getAutocompleteSuggestions, getFnArgDefAtPosition } from './autocomplete'; -describe('getAutocompleteSuggestions', () => { - it('should suggest functions', () => { - const suggestions = getAutocompleteSuggestions(functionSpecs, '', 0); - expect(suggestions.length).toBe(functionSpecs.length); - expect(suggestions[0].start).toBe(0); - expect(suggestions[0].end).toBe(0); +describe('autocomplete', () => { + describe('getFnArgDefAtPosition', () => { + it('should return function definition for plot', () => { + const expression = 'plot '; + const def = getFnArgDefAtPosition(functionSpecs, expression, expression.length); + const plotFn = functionSpecs.find(spec => spec.name === 'plot'); + expect(def.fnDef).toBe(plotFn); + }); }); - it('should suggest functions filtered by text', () => { - const expression = 'pl'; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, 0); - const nonmatching = suggestions.map(s => s.text).filter(text => !text.includes(expression)); - expect(nonmatching.length).toBe(0); - expect(suggestions[0].start).toBe(0); - expect(suggestions[0].end).toBe(expression.length); - }); + describe('getAutocompleteSuggestions', () => { + it('should suggest functions', () => { + const suggestions = getAutocompleteSuggestions(functionSpecs, '', 0); + expect(suggestions.length).toBe(functionSpecs.length); + expect(suggestions[0].start).toBe(0); + expect(suggestions[0].end).toBe(0); + }); - it('should suggest arguments', () => { - const expression = 'plot '; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const plotFn = functionSpecs.find(spec => spec.name === 'plot'); - expect(suggestions.length).toBe(Object.keys(plotFn.args).length); - expect(suggestions[0].start).toBe(expression.length); - expect(suggestions[0].end).toBe(expression.length); - }); + it('should suggest functions filtered by text', () => { + const expression = 'pl'; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, 0); + const nonmatching = suggestions.map(s => s.text).filter(text => !text.includes(expression)); + expect(nonmatching.length).toBe(0); + expect(suggestions[0].start).toBe(0); + expect(suggestions[0].end).toBe(expression.length); + }); - it('should suggest arguments filtered by text', () => { - const expression = 'plot axis'; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const plotFn = functionSpecs.find(spec => spec.name === 'plot'); - const matchingArgs = Object.keys(plotFn.args).filter(key => key.includes('axis')); - expect(suggestions.length).toBe(matchingArgs.length); - expect(suggestions[0].start).toBe('plot '.length); - expect(suggestions[0].end).toBe('plot axis'.length); - }); + it('should suggest arguments', () => { + const expression = 'plot '; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const plotFn = functionSpecs.find(spec => spec.name === 'plot'); + expect(suggestions.length).toBe(Object.keys(plotFn.args).length); + expect(suggestions[0].start).toBe(expression.length); + expect(suggestions[0].end).toBe(expression.length); + }); - it('should suggest values', () => { - const expression = 'shape shape='; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); - expect(suggestions.length).toBe(shapeFn.args.shape.options.length); - expect(suggestions[0].start).toBe(expression.length); - expect(suggestions[0].end).toBe(expression.length); - }); + it('should suggest arguments filtered by text', () => { + const expression = 'plot axis'; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const plotFn = functionSpecs.find(spec => spec.name === 'plot'); + const matchingArgs = Object.keys(plotFn.args).filter(key => key.includes('axis')); + expect(suggestions.length).toBe(matchingArgs.length); + expect(suggestions[0].start).toBe('plot '.length); + expect(suggestions[0].end).toBe('plot axis'.length); + }); - it('should suggest values filtered by text', () => { - const expression = 'shape shape=ar'; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); - const matchingValues = shapeFn.args.shape.options.filter((key: string) => key.includes('ar')); - expect(suggestions.length).toBe(matchingValues.length); - expect(suggestions[0].start).toBe(expression.length - 'ar'.length); - expect(suggestions[0].end).toBe(expression.length); - }); + it('should suggest values', () => { + const expression = 'shape shape='; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); + expect(suggestions.length).toBe(shapeFn.args.shape.options.length); + expect(suggestions[0].start).toBe(expression.length); + expect(suggestions[0].end).toBe(expression.length); + }); - it('should suggest functions inside an expression', () => { - const expression = 'if {}'; - const suggestions = getAutocompleteSuggestions( - functionSpecs, - expression, - expression.length - 1 - ); - expect(suggestions.length).toBe(functionSpecs.length); - expect(suggestions[0].start).toBe(expression.length - 1); - expect(suggestions[0].end).toBe(expression.length - 1); - }); + it('should suggest values filtered by text', () => { + const expression = 'shape shape=ar'; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); + const matchingValues = shapeFn.args.shape.options.filter((key: string) => key.includes('ar')); + expect(suggestions.length).toBe(matchingValues.length); + expect(suggestions[0].start).toBe(expression.length - 'ar'.length); + expect(suggestions[0].end).toBe(expression.length); + }); - it('should suggest arguments inside an expression', () => { - const expression = 'if {lt }'; - const suggestions = getAutocompleteSuggestions( - functionSpecs, - expression, - expression.length - 1 - ); - const ltFn = functionSpecs.find(spec => spec.name === 'lt'); - expect(suggestions.length).toBe(Object.keys(ltFn.args).length); - expect(suggestions[0].start).toBe(expression.length - 1); - expect(suggestions[0].end).toBe(expression.length - 1); - }); + it('should suggest functions inside an expression', () => { + const expression = 'if {}'; + const suggestions = getAutocompleteSuggestions( + functionSpecs, + expression, + expression.length - 1 + ); + expect(suggestions.length).toBe(functionSpecs.length); + expect(suggestions[0].start).toBe(expression.length - 1); + expect(suggestions[0].end).toBe(expression.length - 1); + }); - it('should suggest values inside an expression', () => { - const expression = 'if {shape shape=}'; - const suggestions = getAutocompleteSuggestions( - functionSpecs, - expression, - expression.length - 1 - ); - const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); - expect(suggestions.length).toBe(shapeFn.args.shape.options.length); - expect(suggestions[0].start).toBe(expression.length - 1); - expect(suggestions[0].end).toBe(expression.length - 1); - }); + it('should suggest arguments inside an expression', () => { + const expression = 'if {lt }'; + const suggestions = getAutocompleteSuggestions( + functionSpecs, + expression, + expression.length - 1 + ); + const ltFn = functionSpecs.find(spec => spec.name === 'lt'); + expect(suggestions.length).toBe(Object.keys(ltFn.args).length); + expect(suggestions[0].start).toBe(expression.length - 1); + expect(suggestions[0].end).toBe(expression.length - 1); + }); - it('should suggest values inside quotes', () => { - const expression = 'shape shape="ar"'; - const suggestions = getAutocompleteSuggestions( - functionSpecs, - expression, - expression.length - 1 - ); - const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); - const matchingValues = shapeFn.args.shape.options.filter((key: string) => key.includes('ar')); - expect(suggestions.length).toBe(matchingValues.length); - expect(suggestions[0].start).toBe(expression.length - '"ar"'.length); - expect(suggestions[0].end).toBe(expression.length); - }); + it('should suggest values inside an expression', () => { + const expression = 'if {shape shape=}'; + const suggestions = getAutocompleteSuggestions( + functionSpecs, + expression, + expression.length - 1 + ); + const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); + expect(suggestions.length).toBe(shapeFn.args.shape.options.length); + expect(suggestions[0].start).toBe(expression.length - 1); + expect(suggestions[0].end).toBe(expression.length - 1); + }); - it('should prioritize functions that start with text', () => { - const expression = 't'; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const tableIndex = suggestions.findIndex(suggestion => suggestion.text.includes('table')); - const alterColumnIndex = suggestions.findIndex(suggestion => - suggestion.text.includes('alterColumn') - ); - expect(tableIndex).toBeLessThan(alterColumnIndex); - }); + it('should suggest values inside quotes', () => { + const expression = 'shape shape="ar"'; + const suggestions = getAutocompleteSuggestions( + functionSpecs, + expression, + expression.length - 1 + ); + const shapeFn = functionSpecs.find(spec => spec.name === 'shape'); + const matchingValues = shapeFn.args.shape.options.filter((key: string) => key.includes('ar')); + expect(suggestions.length).toBe(matchingValues.length); + expect(suggestions[0].start).toBe(expression.length - '"ar"'.length); + expect(suggestions[0].end).toBe(expression.length); + }); - it('should prioritize functions that match the previous function type', () => { - const expression = 'plot | '; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const renderIndex = suggestions.findIndex(suggestion => suggestion.text.includes('render')); - const anyIndex = suggestions.findIndex(suggestion => suggestion.text.includes('any')); - expect(renderIndex).toBeLessThan(anyIndex); - }); + it('should prioritize functions that start with text', () => { + const expression = 't'; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const tableIndex = suggestions.findIndex(suggestion => suggestion.text.includes('table')); + const alterColumnIndex = suggestions.findIndex(suggestion => + suggestion.text.includes('alterColumn') + ); + expect(tableIndex).toBeLessThan(alterColumnIndex); + }); - it('should alphabetize functions', () => { - const expression = ''; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const metricIndex = suggestions.findIndex(suggestion => suggestion.text.includes('metric')); - const anyIndex = suggestions.findIndex(suggestion => suggestion.text.includes('any')); - expect(anyIndex).toBeLessThan(metricIndex); - }); + it('should prioritize functions that match the previous function type', () => { + const expression = 'plot | '; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const renderIndex = suggestions.findIndex(suggestion => suggestion.text.includes('render')); + const anyIndex = suggestions.findIndex(suggestion => suggestion.text.includes('any')); + expect(renderIndex).toBeLessThan(anyIndex); + }); - it('should prioritize arguments that start with text', () => { - const expression = 'plot y'; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const yaxisIndex = suggestions.findIndex(suggestion => suggestion.text.includes('yaxis')); - const defaultStyleIndex = suggestions.findIndex(suggestion => - suggestion.text.includes('defaultStyle') - ); - expect(yaxisIndex).toBeLessThan(defaultStyleIndex); - }); + it('should alphabetize functions', () => { + const expression = ''; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const metricIndex = suggestions.findIndex(suggestion => suggestion.text.includes('metric')); + const anyIndex = suggestions.findIndex(suggestion => suggestion.text.includes('any')); + expect(anyIndex).toBeLessThan(metricIndex); + }); - it('should prioritize unnamed arguments', () => { - const expression = 'case '; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const whenIndex = suggestions.findIndex(suggestion => suggestion.text.includes('when')); - const thenIndex = suggestions.findIndex(suggestion => suggestion.text.includes('then')); - expect(whenIndex).toBeLessThan(thenIndex); - }); + it('should prioritize arguments that start with text', () => { + const expression = 'plot y'; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const yaxisIndex = suggestions.findIndex(suggestion => suggestion.text.includes('yaxis')); + const defaultStyleIndex = suggestions.findIndex(suggestion => + suggestion.text.includes('defaultStyle') + ); + expect(yaxisIndex).toBeLessThan(defaultStyleIndex); + }); - it('should alphabetize arguments', () => { - const expression = 'plot '; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - const yaxisIndex = suggestions.findIndex(suggestion => suggestion.text.includes('yaxis')); - const defaultStyleIndex = suggestions.findIndex(suggestion => - suggestion.text.includes('defaultStyle') - ); - expect(defaultStyleIndex).toBeLessThan(yaxisIndex); - }); + it('should prioritize unnamed arguments', () => { + const expression = 'case '; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const whenIndex = suggestions.findIndex(suggestion => suggestion.text.includes('when')); + const thenIndex = suggestions.findIndex(suggestion => suggestion.text.includes('then')); + expect(whenIndex).toBeLessThan(thenIndex); + }); - it('should quote string values', () => { - const expression = 'shape shape='; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - expect(suggestions[0].text.trim()).toMatch(/^".*"$/); - }); + it('should alphabetize arguments', () => { + const expression = 'plot '; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + const yaxisIndex = suggestions.findIndex(suggestion => suggestion.text.includes('yaxis')); + const defaultStyleIndex = suggestions.findIndex(suggestion => + suggestion.text.includes('defaultStyle') + ); + expect(defaultStyleIndex).toBeLessThan(yaxisIndex); + }); - it('should not quote sub expression value suggestions', () => { - const expression = 'plot font='; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - expect(suggestions[0].text.trim()).toBe('{font}'); - }); + it('should quote string values', () => { + const expression = 'shape shape='; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + expect(suggestions[0].text.trim()).toMatch(/^".*"$/); + }); - it('should not quote booleans', () => { - const expression = 'table paginate=true'; - const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); - expect(suggestions[0].text.trim()).toBe('true'); + it('should not quote sub expression value suggestions', () => { + const expression = 'plot font='; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + expect(suggestions[0].text.trim()).toBe('{font}'); + }); + + it('should not quote booleans', () => { + const expression = 'table paginate=true'; + const suggestions = getAutocompleteSuggestions(functionSpecs, expression, expression.length); + expect(suggestions[0].text.trim()).toBe('true'); + }); }); }); diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/dataurl.test.ts b/x-pack/legacy/plugins/canvas/common/lib/dataurl.test.ts similarity index 77% rename from x-pack/legacy/plugins/canvas/common/lib/__tests__/dataurl.test.ts rename to x-pack/legacy/plugins/canvas/common/lib/dataurl.test.ts index acd9e6d1821d..8bfe723bc2ae 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/dataurl.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/dataurl.test.ts @@ -4,13 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -import { isValidDataUrl, parseDataUrl } from '../dataurl'; +import { isValidDataUrl, parseDataUrl } from './dataurl'; const BASE64_TEXT = 'data:text/plain;charset=utf-8;base64,VGhpcyBpcyBhIHRlc3Q='; const BASE64_SVG = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciLz4='; const BASE64_PIXEL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk8PxfDwADYgHJvQ16TAAAAABJRU5ErkJggg=='; +const INVALID_BASE64_PIXEL = + 'data:image/png;%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%01%00%00%00%01%08%06%00%00%00%1F%15%C4%89%0'; const RAW_TEXT = 'data:text/plain;charset=utf-8,This%20is%20a%20test'; const RAW_SVG = @@ -20,10 +22,13 @@ const RAW_PIXEL = describe('dataurl', () => { describe('isValidDataUrl', () => { - test('invalid data url', () => { + it('returns false for an invalid data url', () => { expect(isValidDataUrl('somestring')).toBe(false); }); - test('valid data urls', () => { + it('returns false for an empty string', () => { + expect(isValidDataUrl('')).toBe(false); + }); + it('returns true for valid data urls', () => { expect(isValidDataUrl(BASE64_TEXT)).toBe(true); expect(isValidDataUrl(BASE64_SVG)).toBe(true); expect(isValidDataUrl(BASE64_PIXEL)).toBe(true); @@ -34,10 +39,13 @@ describe('dataurl', () => { }); describe('dataurl.parseDataUrl', () => { - test('invalid data url', () => { + it('returns null for an invalid data url', () => { expect(parseDataUrl('somestring')).toBeNull(); }); - test('text data urls', () => { + it('returns null for an invalid base64 image', () => { + expect(parseDataUrl(INVALID_BASE64_PIXEL)).toBeNull(); + }); + it('returns correct values for text data urls', () => { expect(parseDataUrl(BASE64_TEXT)).toEqual({ charset: 'utf-8', data: null, @@ -55,7 +63,7 @@ describe('dataurl', () => { mimetype: 'text/plain', }); }); - test('png data urls', () => { + it('returns correct values for png data urls', () => { expect(parseDataUrl(RAW_PIXEL)).toBeNull(); expect(parseDataUrl(BASE64_PIXEL)).toEqual({ charset: undefined, @@ -66,7 +74,7 @@ describe('dataurl', () => { mimetype: 'image/png', }); }); - test('svg data urls', () => { + it('returns correct values for svg data urls', () => { expect(parseDataUrl(RAW_SVG)).toEqual({ charset: undefined, data: null, diff --git a/x-pack/legacy/plugins/canvas/common/lib/errors.test.js b/x-pack/legacy/plugins/canvas/common/lib/errors.test.js new file mode 100644 index 000000000000..a589fde5dadb --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/errors.test.js @@ -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 { RenderError } from './errors'; + +describe('errors', () => { + it('creates a test error', () => { + // eslint-disable-next-line new-cap + const throwTestError = () => RenderError(); + expect(throwTestError.name).toBe('throwTestError'); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/expression_form_handlers.test.js b/x-pack/legacy/plugins/canvas/common/lib/expression_form_handlers.test.js new file mode 100644 index 000000000000..ae46661b50cd --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/expression_form_handlers.test.js @@ -0,0 +1,17 @@ +/* + * 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 { ExpressionFormHandlers } from './expression_form_handlers'; + +describe('ExpressionFormHandlers', () => { + it('executes destroy function', () => { + const handler = new ExpressionFormHandlers(); + handler.onDestroy(() => { + return 'DESTROYED!'; + }); + expect(handler.destroy()).toBe('DESTROYED!'); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/fetch.test.ts b/x-pack/legacy/plugins/canvas/common/lib/fetch.test.ts new file mode 100644 index 000000000000..d06c2af4f062 --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/fetch.test.ts @@ -0,0 +1,24 @@ +/* + * 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 { fetch, arrayBufferFetch } from './fetch'; + +describe('fetch', () => { + it('test fetch headers', () => { + expect(fetch.defaults.headers.Accept).toBe('application/json'); + expect(fetch.defaults.headers['Content-Type']).toBe('application/json'); + expect(fetch.defaults.headers['kbn-xsrf']).toBe('professionally-crafted-string-of-text'); + }); + + it('test arrayBufferFetch headers', () => { + expect(arrayBufferFetch.defaults.headers.Accept).toBe('application/json'); + expect(arrayBufferFetch.defaults.headers['Content-Type']).toBe('application/json'); + expect(arrayBufferFetch.defaults.headers['kbn-xsrf']).toBe( + 'professionally-crafted-string-of-text' + ); + expect(arrayBufferFetch.defaults.responseType).toBe('arraybuffer'); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/get_colors_from_palette.test.js b/x-pack/legacy/plugins/canvas/common/lib/get_colors_from_palette.test.js new file mode 100644 index 000000000000..ebc72db1f67f --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/get_colors_from_palette.test.js @@ -0,0 +1,40 @@ +/* + * 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 { + grayscalePalette, + gradientPalette, +} from '../../canvas_plugin_src/functions/common/__tests__/fixtures/test_styles'; +import { getColorsFromPalette } from './get_colors_from_palette'; + +describe('getColorsFromPalette', () => { + it('returns the array of colors from a palette object when gradient is false', () => { + expect(getColorsFromPalette(grayscalePalette, 20)).toBe(grayscalePalette.colors); + }); + + it('returns an array of colors with equidistant colors with length equal to the number of series when gradient is true', () => { + const result = getColorsFromPalette(gradientPalette, 16); + expect(result).toEqual([ + '#ffffff', + '#eeeeee', + '#dddddd', + '#cccccc', + '#bbbbbb', + '#aaaaaa', + '#999999', + '#888888', + '#777777', + '#666666', + '#555555', + '#444444', + '#333333', + '#222222', + '#111111', + '#000000', + ]); + expect(result).toHaveLength(16); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_field_type.test.ts b/x-pack/legacy/plugins/canvas/common/lib/get_field_type.test.ts similarity index 87% rename from x-pack/legacy/plugins/canvas/common/lib/__tests__/get_field_type.test.ts rename to x-pack/legacy/plugins/canvas/common/lib/get_field_type.test.ts index 34cfbb5a2bef..82e724c33ecc 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/get_field_type.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/get_field_type.test.ts @@ -4,11 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { getFieldType } from '../get_field_type'; import { emptyTable, testTable, -} from '../../../canvas_plugin_src/functions/common/__tests__/fixtures/test_tables'; +} from '../../canvas_plugin_src/functions/common/__tests__/fixtures/test_tables'; +import { getFieldType } from './get_field_type'; describe('getFieldType', () => { it('returns type of a field in a datatable', () => { diff --git a/x-pack/legacy/plugins/canvas/common/lib/get_legend_config.test.js b/x-pack/legacy/plugins/canvas/common/lib/get_legend_config.test.js new file mode 100644 index 000000000000..b9ab9ae6aba5 --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/get_legend_config.test.js @@ -0,0 +1,40 @@ +/* + * 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 { getLegendConfig } from './get_legend_config'; + +describe('getLegendConfig', () => { + describe('show', () => { + it('hides the legend', () => { + expect(getLegendConfig(false, 2)).toHaveProperty('show', false); + expect(getLegendConfig(false, 10)).toHaveProperty('show', false); + }); + + it('hides the legend when there are less than 2 series', () => { + expect(getLegendConfig(false, 1)).toHaveProperty('show', false); + expect(getLegendConfig(true, 1)).toHaveProperty('show', false); + }); + + it('shows the legend when there are two or more series', () => { + expect(getLegendConfig('sw', 2)).toHaveProperty('show', true); + expect(getLegendConfig(true, 5)).toHaveProperty('show', true); + }); + }); + + describe('position', () => { + it('sets the position of the legend', () => { + expect(getLegendConfig('nw')).toHaveProperty('position', 'nw'); + expect(getLegendConfig('ne')).toHaveProperty('position', 'ne'); + expect(getLegendConfig('sw')).toHaveProperty('position', 'sw'); + expect(getLegendConfig('se')).toHaveProperty('position', 'se'); + }); + + it("defaults to 'ne'", () => { + expect(getLegendConfig(true)).toHaveProperty('position', 'ne'); + expect(getLegendConfig('foo')).toHaveProperty('position', 'ne'); + }); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/handlebars.test.js b/x-pack/legacy/plugins/canvas/common/lib/handlebars.test.js new file mode 100644 index 000000000000..5fcb2d42395f --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/handlebars.test.js @@ -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 { testTable } from '../../canvas_plugin_src/functions/common/__tests__/fixtures/test_tables'; +import { Handlebars } from './handlebars'; + +describe('handlebars', () => { + it('registers math function and returns argument error', () => { + const template = Handlebars.compile("test math: {{math rows 'mean(price * quantity)' 2}}"); + expect(template()).toBe('test math: MATH ERROR: first argument must be an array'); + }); + it('evaluates math function successfully', () => { + const template = Handlebars.compile("test math: {{math rows 'mean(price * quantity)' 2}}"); + expect(template(testTable)).toBe('test math: 82164.33'); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/hex_to_rgb.test.ts b/x-pack/legacy/plugins/canvas/common/lib/hex_to_rgb.test.ts similarity index 80% rename from x-pack/legacy/plugins/canvas/common/lib/__tests__/hex_to_rgb.test.ts rename to x-pack/legacy/plugins/canvas/common/lib/hex_to_rgb.test.ts index d9aa56314948..00b4b40fa983 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/hex_to_rgb.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/hex_to_rgb.test.ts @@ -4,21 +4,21 @@ * you may not use this file except in compliance with the Elastic License. */ -import { hexToRgb } from '../hex_to_rgb'; +import { hexToRgb } from './hex_to_rgb'; describe('hexToRgb', () => { - test('invalid hex', () => { + it('returns null for an invalid hex', () => { expect(hexToRgb('hexadecimal')).toBeNull(); expect(hexToRgb('#00')).toBeNull(); expect(hexToRgb('#00000')).toBeNull(); }); - test('shorthand', () => { + it('returns correct value for shorthand hex codes', () => { expect(hexToRgb('#000')).toEqual([0, 0, 0]); expect(hexToRgb('#FFF')).toEqual([255, 255, 255]); expect(hexToRgb('#fff')).toEqual([255, 255, 255]); expect(hexToRgb('#fFf')).toEqual([255, 255, 255]); }); - test('longhand', () => { + it('returns correct value for longhand hex codes', () => { expect(hexToRgb('#000000')).toEqual([0, 0, 0]); expect(hexToRgb('#ffffff')).toEqual([255, 255, 255]); expect(hexToRgb('#fffFFF')).toEqual([255, 255, 255]); diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/httpurl.test.ts b/x-pack/legacy/plugins/canvas/common/lib/httpurl.test.ts similarity index 97% rename from x-pack/legacy/plugins/canvas/common/lib/__tests__/httpurl.test.ts rename to x-pack/legacy/plugins/canvas/common/lib/httpurl.test.ts index 2a7cef7cf423..65bc2469647a 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/httpurl.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/httpurl.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { isValidHttpUrl } from '../httpurl'; +import { isValidHttpUrl } from './httpurl'; describe('httpurl.isValidHttpUrl', () => { it('matches HTTP URLs', () => { diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/pivot_object_array.test.ts b/x-pack/legacy/plugins/canvas/common/lib/pivot_object_array.test.ts similarity index 97% rename from x-pack/legacy/plugins/canvas/common/lib/__tests__/pivot_object_array.test.ts rename to x-pack/legacy/plugins/canvas/common/lib/pivot_object_array.test.ts index 6f6d42e7129a..faf319769cab 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/pivot_object_array.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/pivot_object_array.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { pivotObjectArray } from '../pivot_object_array'; +import { pivotObjectArray } from './pivot_object_array'; interface Car { make: string; diff --git a/x-pack/legacy/plugins/canvas/common/lib/resolve_dataurl.test.js b/x-pack/legacy/plugins/canvas/common/lib/resolve_dataurl.test.js new file mode 100644 index 000000000000..bbbd4f51d483 --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/resolve_dataurl.test.js @@ -0,0 +1,41 @@ +/* + * 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 { missingImage } from '../../common/lib/missing_asset'; +import { resolveFromArgs, resolveWithMissingImage } from './resolve_dataurl'; + +describe('resolve_dataurl', () => { + describe('resolveFromArgs', () => { + it('finds and returns the dataurl from args successfully', () => { + const args = { + name: 'dataurl', + argType: 'imageUpload', + dataurl: [missingImage, 'test2'], + }; + expect(resolveFromArgs(args)).toBe(missingImage); + }); + it('finds and returns null for invalid dataurl', () => { + const args = { + name: 'dataurl', + argType: 'imageUpload', + dataurl: ['invalid url', 'test2'], + }; + expect(resolveFromArgs(args)).toBe(null); + }); + }); + + describe('resolveWithMissingImage', () => { + it('returns valid dataurl', () => { + expect(resolveWithMissingImage(missingImage)).toBe(missingImage); + }); + it('returns missingImage for invalid dataurl', () => { + expect(resolveWithMissingImage('invalid dataurl')).toBe(missingImage); + }); + it('returns null for null dataurl', () => { + expect(resolveWithMissingImage(null)).toBe(null); + }); + }); +}); diff --git a/x-pack/legacy/plugins/canvas/common/lib/__tests__/unquote_string.test.ts b/x-pack/legacy/plugins/canvas/common/lib/unquote_string.test.ts similarity index 94% rename from x-pack/legacy/plugins/canvas/common/lib/__tests__/unquote_string.test.ts rename to x-pack/legacy/plugins/canvas/common/lib/unquote_string.test.ts index e67e46f9e5da..d6eeb9392f40 100644 --- a/x-pack/legacy/plugins/canvas/common/lib/__tests__/unquote_string.test.ts +++ b/x-pack/legacy/plugins/canvas/common/lib/unquote_string.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { unquoteString } from '../unquote_string'; +import { unquoteString } from './unquote_string'; describe('unquoteString', () => { it('removes double quotes', () => { diff --git a/x-pack/legacy/plugins/canvas/common/lib/url.test.js b/x-pack/legacy/plugins/canvas/common/lib/url.test.js new file mode 100644 index 000000000000..d49d12c6bf38 --- /dev/null +++ b/x-pack/legacy/plugins/canvas/common/lib/url.test.js @@ -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 { missingImage } from '../../common/lib/missing_asset'; +import { isValidUrl } from './url'; + +describe('resolve_dataurl', () => { + it('returns valid dataurl', () => { + expect(isValidUrl(missingImage)).toBe(true); + }); + it('returns valid http url', () => { + const httpurl = 'https://test.com/s/'; + expect(isValidUrl(httpurl)).toBe(true); + }); + it('returns false for invalid url', () => { + expect(isValidUrl('test')).toBe(false); + }); +});