From 57dc42e2372182222ea942a8504be7a5f1215ee0 Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Thu, 5 Nov 2020 14:55:13 -0500 Subject: [PATCH] [Lens] Fix bug in terms formatting --- .../operations/definitions/terms/index.tsx | 13 +++++++ .../definitions/terms/terms.test.tsx | 34 ++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx index 85deb2bac25ca..dcb4646816e13 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx +++ b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx @@ -34,6 +34,13 @@ export interface TermsIndexPatternColumn extends FieldBasedIndexPatternColumn { size: number; orderBy: { type: 'alphabetical' } | { type: 'column'; columnId: string }; orderDirection: 'asc' | 'desc'; + // Terms on numeric fields can be formatted + format?: { + id: string; + params?: { + decimals: number; + }; + }; }; } @@ -105,10 +112,16 @@ export const termsOperation: OperationDefinition { + const newParams = { ...oldColumn.params }; + if ('format' in newParams && field.type !== 'number') { + delete newParams.format; + } return { ...oldColumn, + dataType: field.type as DataType, label: ofName(field.displayName), sourceField: field.name, + params: newParams, }; }, onOtherColumnChanged: (currentColumn, columns) => { diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/terms.test.tsx b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/terms.test.tsx index 2c4e67ef0d9b9..1341ca0587c75 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/terms.test.tsx +++ b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/terms.test.tsx @@ -103,14 +103,40 @@ describe('terms', () => { }, }; const indexPattern = createMockedIndexPattern(); - const newDateField = indexPattern.fields.find((i) => i.name === 'dest')!; + const newNumberField = indexPattern.fields.find((i) => i.name === 'bytes')!; - const column = termsOperation.onFieldChange(oldColumn, indexPattern, newDateField); - expect(column).toHaveProperty('sourceField', 'dest'); + const column = termsOperation.onFieldChange(oldColumn, indexPattern, newNumberField); + expect(column).toHaveProperty('dataType', 'number'); + expect(column).toHaveProperty('sourceField', 'bytes'); expect(column).toHaveProperty('params.size', 5); expect(column).toHaveProperty('params.orderBy.type', 'alphabetical'); expect(column).toHaveProperty('params.orderDirection', 'asc'); - expect(column.label).toContain('dest'); + expect(column.label).toContain('bytes'); + }); + + it('should remove numeric parameters when changing away from number', () => { + const oldColumn: TermsIndexPatternColumn = { + operationType: 'terms', + sourceField: 'bytes', + label: 'Top values of bytes', + isBucketed: true, + dataType: 'number', + params: { + size: 5, + orderBy: { + type: 'alphabetical', + }, + orderDirection: 'asc', + format: { id: 'number', params: { decimals: 0 } }, + }, + }; + const indexPattern = createMockedIndexPattern(); + const newStringField = indexPattern.fields.find((i) => i.name === 'source')!; + + const column = termsOperation.onFieldChange(oldColumn, indexPattern, newStringField); + expect(column).toHaveProperty('dataType', 'string'); + expect(column).toHaveProperty('sourceField', 'source'); + expect(column.params.format).toBeUndefined(); }); });