|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks'; |
| 9 | +import type { |
| 10 | + Datatable, |
| 11 | + DefaultInspectorAdapters, |
| 12 | + ExecutionContext, |
| 13 | +} from '@kbn/expressions-plugin/common'; |
| 14 | +import type { DatatableArgs } from '../..'; |
| 15 | +import { datatableFn } from './datatable_fn'; |
| 16 | +import { shuffle } from 'lodash'; |
| 17 | + |
| 18 | +const context = { |
| 19 | + variables: { embeddableTitle: 'title' }, |
| 20 | +} as unknown as ExecutionContext<DefaultInspectorAdapters>; |
| 21 | + |
| 22 | +const mockFormatFactory = fieldFormatsServiceMock.createStartContract().deserialize; |
| 23 | + |
| 24 | +describe('datatableFn', () => { |
| 25 | + function buildTable(): Datatable { |
| 26 | + return { |
| 27 | + type: 'datatable', |
| 28 | + columns: [ |
| 29 | + { id: 'bucket1', name: 'bucket1', meta: { type: 'string' } }, |
| 30 | + { id: 'bucket2', name: 'bucket2', meta: { type: 'string' } }, |
| 31 | + { id: 'bucket3', name: 'bucket3', meta: { type: 'string' } }, |
| 32 | + { id: 'metric1', name: 'metric1', meta: { type: 'number' } }, |
| 33 | + { id: 'metric2', name: 'metric2', meta: { type: 'number' } }, |
| 34 | + ], |
| 35 | + rows: [ |
| 36 | + { bucket1: 'A', bucket2: 'D', bucket3: 'X', metric1: 1, metric2: 2 }, |
| 37 | + { bucket1: 'A', bucket2: 'D', bucket3: 'Y', metric1: 3, metric2: 4 }, |
| 38 | + { bucket1: 'A', bucket2: 'D', bucket3: 'Z', metric1: 5, metric2: 6 }, |
| 39 | + { bucket1: 'A', bucket2: 'E', bucket3: 'X', metric1: 7, metric2: 8 }, |
| 40 | + { bucket1: 'A', bucket2: 'E', bucket3: 'Y', metric1: 9, metric2: 10 }, |
| 41 | + { bucket1: 'A', bucket2: 'E', bucket3: 'Z', metric1: 11, metric2: 12 }, |
| 42 | + { bucket1: 'A', bucket2: 'F', bucket3: 'X', metric1: 13, metric2: 14 }, |
| 43 | + { bucket1: 'A', bucket2: 'F', bucket3: 'Y', metric1: 15, metric2: 16 }, |
| 44 | + { bucket1: 'A', bucket2: 'F', bucket3: 'Z', metric1: 17, metric2: 18 }, |
| 45 | + { bucket1: 'B', bucket2: 'D', bucket3: 'X', metric1: 19, metric2: 20 }, |
| 46 | + { bucket1: 'B', bucket2: 'D', bucket3: 'Y', metric1: 21, metric2: 22 }, |
| 47 | + { bucket1: 'B', bucket2: 'D', bucket3: 'Z', metric1: 23, metric2: 24 }, |
| 48 | + { bucket1: 'B', bucket2: 'E', bucket3: 'X', metric1: 25, metric2: 26 }, |
| 49 | + { bucket1: 'B', bucket2: 'E', bucket3: 'Y', metric1: 27, metric2: 28 }, |
| 50 | + { bucket1: 'B', bucket2: 'E', bucket3: 'Z', metric1: 29, metric2: 30 }, |
| 51 | + { bucket1: 'B', bucket2: 'F', bucket3: 'X', metric1: 31, metric2: 32 }, |
| 52 | + { bucket1: 'B', bucket2: 'F', bucket3: 'Y', metric1: 33, metric2: 34 }, |
| 53 | + { bucket1: 'B', bucket2: 'F', bucket3: 'Z', metric1: 35, metric2: 36 }, |
| 54 | + { bucket1: 'C', bucket2: 'D', bucket3: 'X', metric1: 37, metric2: 38 }, |
| 55 | + { bucket1: 'C', bucket2: 'D', bucket3: 'Y', metric1: 39, metric2: 40 }, |
| 56 | + { bucket1: 'C', bucket2: 'D', bucket3: 'Z', metric1: 41, metric2: 42 }, |
| 57 | + { bucket1: 'C', bucket2: 'E', bucket3: 'X', metric1: 43, metric2: 44 }, |
| 58 | + { bucket1: 'C', bucket2: 'E', bucket3: 'Y', metric1: 45, metric2: 46 }, |
| 59 | + { bucket1: 'C', bucket2: 'E', bucket3: 'Z', metric1: 47, metric2: 48 }, |
| 60 | + { bucket1: 'C', bucket2: 'F', bucket3: 'X', metric1: 49, metric2: 50 }, |
| 61 | + { bucket1: 'C', bucket2: 'F', bucket3: 'Y', metric1: 51, metric2: 52 }, |
| 62 | + { bucket1: 'C', bucket2: 'F', bucket3: 'Z', metric1: 53, metric2: 54 }, |
| 63 | + ], |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + function buildArgs(): DatatableArgs { |
| 68 | + return { |
| 69 | + title: 'Table', |
| 70 | + sortingColumnId: undefined, |
| 71 | + sortingDirection: 'none', |
| 72 | + columns: [ |
| 73 | + { |
| 74 | + type: 'lens_datatable_column', |
| 75 | + columnId: 'bucket1', |
| 76 | + isTransposed: false, |
| 77 | + transposable: false, |
| 78 | + }, |
| 79 | + { |
| 80 | + type: 'lens_datatable_column', |
| 81 | + columnId: 'bucket2', |
| 82 | + isTransposed: false, |
| 83 | + transposable: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + type: 'lens_datatable_column', |
| 87 | + columnId: 'bucket3', |
| 88 | + isTransposed: false, |
| 89 | + transposable: false, |
| 90 | + }, |
| 91 | + { |
| 92 | + type: 'lens_datatable_column', |
| 93 | + columnId: 'metric1', |
| 94 | + isTransposed: false, |
| 95 | + transposable: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + type: 'lens_datatable_column', |
| 99 | + columnId: 'metric2', |
| 100 | + isTransposed: false, |
| 101 | + transposable: true, |
| 102 | + }, |
| 103 | + ], |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + it('should correctly sort columns in table by order of args.columns', async () => { |
| 108 | + const table = buildTable(); |
| 109 | + const shuffledTable: Datatable = { |
| 110 | + ...table, |
| 111 | + columns: shuffle(table.columns), |
| 112 | + }; |
| 113 | + const args = buildArgs(); |
| 114 | + const result = await datatableFn(() => mockFormatFactory)(shuffledTable, args, context); |
| 115 | + |
| 116 | + const resultColumnIds = result.value.data.columns.map((c) => c.id); |
| 117 | + const expectedColumnIds = args.columns.map((c) => c.columnId); |
| 118 | + |
| 119 | + expect(resultColumnIds).toEqual(expectedColumnIds); |
| 120 | + expect(resultColumnIds).toEqual(['bucket1', 'bucket2', 'bucket3', 'metric1', 'metric2']); |
| 121 | + }); |
| 122 | +}); |
0 commit comments