|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +// @ts-ignore |
| 8 | +import { compile } from 'vega-lite/build-es5/vega-lite'; |
| 9 | + |
| 10 | +import euiThemeLight from '@elastic/eui/dist/eui_theme_light.json'; |
| 11 | + |
| 12 | +import { |
| 13 | + getColorSpec, |
| 14 | + getScatterplotMatrixVegaLiteSpec, |
| 15 | + COLOR_OUTLIER, |
| 16 | + COLOR_RANGE_NOMINAL, |
| 17 | + DEFAULT_COLOR, |
| 18 | + LEGEND_TYPES, |
| 19 | +} from './scatterplot_matrix_vega_lite_spec'; |
| 20 | + |
| 21 | +describe('getColorSpec()', () => { |
| 22 | + it('should return the default color for non-outlier specs', () => { |
| 23 | + const colorSpec = getColorSpec(euiThemeLight, false); |
| 24 | + |
| 25 | + expect(colorSpec).toEqual({ value: DEFAULT_COLOR }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return a conditional spec for outliers', () => { |
| 29 | + const colorSpec = getColorSpec(euiThemeLight, true); |
| 30 | + |
| 31 | + expect(colorSpec).toEqual({ |
| 32 | + condition: { |
| 33 | + test: "(datum['outlier_score'] >= mlOutlierScoreThreshold.cutoff)", |
| 34 | + value: COLOR_OUTLIER, |
| 35 | + }, |
| 36 | + value: euiThemeLight.euiColorMediumShade, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return a field based spec for non-outlier specs with legendType supplied', () => { |
| 41 | + const colorName = 'the-color-field'; |
| 42 | + |
| 43 | + const colorSpec = getColorSpec(euiThemeLight, false, colorName, LEGEND_TYPES.NOMINAL); |
| 44 | + |
| 45 | + expect(colorSpec).toEqual({ |
| 46 | + field: colorName, |
| 47 | + scale: { |
| 48 | + range: COLOR_RANGE_NOMINAL, |
| 49 | + }, |
| 50 | + type: 'nominal', |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('getScatterplotMatrixVegaLiteSpec()', () => { |
| 56 | + it('should return the default spec for non-outliers without a legend', () => { |
| 57 | + const data = [{ x: 1, y: 1 }]; |
| 58 | + |
| 59 | + const vegaLiteSpec = getScatterplotMatrixVegaLiteSpec(data, ['x', 'y'], euiThemeLight); |
| 60 | + |
| 61 | + // A valid Vega Lite spec shouldn't throw an error when compiled. |
| 62 | + expect(() => compile(vegaLiteSpec)).not.toThrow(); |
| 63 | + |
| 64 | + expect(vegaLiteSpec.repeat).toEqual({ |
| 65 | + column: ['x', 'y'], |
| 66 | + row: ['y', 'x'], |
| 67 | + }); |
| 68 | + expect(vegaLiteSpec.spec.transform).toEqual([ |
| 69 | + { as: 'x', calculate: "datum['x']" }, |
| 70 | + { as: 'y', calculate: "datum['y']" }, |
| 71 | + ]); |
| 72 | + expect(vegaLiteSpec.spec.data.values).toEqual(data); |
| 73 | + expect(vegaLiteSpec.spec.mark).toEqual({ |
| 74 | + opacity: 0.75, |
| 75 | + size: 8, |
| 76 | + type: 'circle', |
| 77 | + }); |
| 78 | + expect(vegaLiteSpec.spec.encoding.color).toEqual({ value: DEFAULT_COLOR }); |
| 79 | + expect(vegaLiteSpec.spec.encoding.tooltip).toEqual([ |
| 80 | + { field: 'x', type: 'quantitative' }, |
| 81 | + { field: 'y', type: 'quantitative' }, |
| 82 | + ]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return the spec for outliers', () => { |
| 86 | + const data = [{ x: 1, y: 1 }]; |
| 87 | + |
| 88 | + const vegaLiteSpec = getScatterplotMatrixVegaLiteSpec(data, ['x', 'y'], euiThemeLight, 'ml'); |
| 89 | + |
| 90 | + // A valid Vega Lite spec shouldn't throw an error when compiled. |
| 91 | + expect(() => compile(vegaLiteSpec)).not.toThrow(); |
| 92 | + |
| 93 | + expect(vegaLiteSpec.repeat).toEqual({ |
| 94 | + column: ['x', 'y'], |
| 95 | + row: ['y', 'x'], |
| 96 | + }); |
| 97 | + expect(vegaLiteSpec.spec.transform).toEqual([ |
| 98 | + { as: 'x', calculate: "datum['x']" }, |
| 99 | + { as: 'y', calculate: "datum['y']" }, |
| 100 | + { |
| 101 | + as: 'outlier_score', |
| 102 | + calculate: "datum['ml.outlier_score']", |
| 103 | + }, |
| 104 | + ]); |
| 105 | + expect(vegaLiteSpec.spec.data.values).toEqual(data); |
| 106 | + expect(vegaLiteSpec.spec.mark).toEqual({ |
| 107 | + opacity: 0.75, |
| 108 | + size: 8, |
| 109 | + type: 'circle', |
| 110 | + }); |
| 111 | + expect(vegaLiteSpec.spec.encoding.color).toEqual({ |
| 112 | + condition: { |
| 113 | + test: "(datum['outlier_score'] >= mlOutlierScoreThreshold.cutoff)", |
| 114 | + value: COLOR_OUTLIER, |
| 115 | + }, |
| 116 | + value: euiThemeLight.euiColorMediumShade, |
| 117 | + }); |
| 118 | + expect(vegaLiteSpec.spec.encoding.tooltip).toEqual([ |
| 119 | + { field: 'x', type: 'quantitative' }, |
| 120 | + { field: 'y', type: 'quantitative' }, |
| 121 | + { |
| 122 | + field: 'outlier_score', |
| 123 | + format: '.3f', |
| 124 | + type: 'quantitative', |
| 125 | + }, |
| 126 | + ]); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return the spec for classification', () => { |
| 130 | + const data = [{ x: 1, y: 1 }]; |
| 131 | + |
| 132 | + const vegaLiteSpec = getScatterplotMatrixVegaLiteSpec( |
| 133 | + data, |
| 134 | + ['x', 'y'], |
| 135 | + euiThemeLight, |
| 136 | + undefined, |
| 137 | + 'the-color-field', |
| 138 | + LEGEND_TYPES.NOMINAL |
| 139 | + ); |
| 140 | + |
| 141 | + // A valid Vega Lite spec shouldn't throw an error when compiled. |
| 142 | + expect(() => compile(vegaLiteSpec)).not.toThrow(); |
| 143 | + |
| 144 | + expect(vegaLiteSpec.repeat).toEqual({ |
| 145 | + column: ['x', 'y'], |
| 146 | + row: ['y', 'x'], |
| 147 | + }); |
| 148 | + expect(vegaLiteSpec.spec.transform).toEqual([ |
| 149 | + { as: 'x', calculate: "datum['x']" }, |
| 150 | + { as: 'y', calculate: "datum['y']" }, |
| 151 | + ]); |
| 152 | + expect(vegaLiteSpec.spec.data.values).toEqual(data); |
| 153 | + expect(vegaLiteSpec.spec.mark).toEqual({ |
| 154 | + opacity: 0.75, |
| 155 | + size: 8, |
| 156 | + type: 'circle', |
| 157 | + }); |
| 158 | + expect(vegaLiteSpec.spec.encoding.color).toEqual({ |
| 159 | + field: 'the-color-field', |
| 160 | + scale: { |
| 161 | + range: COLOR_RANGE_NOMINAL, |
| 162 | + }, |
| 163 | + type: 'nominal', |
| 164 | + }); |
| 165 | + expect(vegaLiteSpec.spec.encoding.tooltip).toEqual([ |
| 166 | + { field: 'x', type: 'quantitative' }, |
| 167 | + { field: 'y', type: 'quantitative' }, |
| 168 | + ]); |
| 169 | + }); |
| 170 | +}); |
0 commit comments