From 052e41e5bf0102694c9efb4084f802c6501f123d Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Mon, 17 Jul 2023 10:45:22 +0300 Subject: [PATCH] [Lens] Fixes error on table when there is no data (#161953) ## Summary Closes https://github.com/elastic/kibana/issues/161948 Fixes the bug described in the issue. The bug is happening only for the table viz. image ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios (cherry picked from commit 6550793b4fe793ad53072bddc51118e43414e7ad) --- .../datatable/transpose_helpers.test.ts | 24 ++++++++++++++++++- .../datatable/transpose_helpers.ts | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.test.ts b/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.test.ts index ae6e20d976b5e..5d5bfc3135b5e 100644 --- a/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.test.ts +++ b/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.test.ts @@ -11,7 +11,7 @@ import { DatatableArgs } from './datatable'; import { transposeTable } from './transpose_helpers'; -describe('transpose_helpes', () => { +describe('transpose_helpers', () => { function buildTable(): Datatable { // 3 buckets, 2 metrics // first bucket goes A/B/C @@ -290,4 +290,26 @@ describe('transpose_helpes', () => { 1 + 18 - 2 ); }); + + it('should not fail for no data', () => { + const table = buildTable(); + const updatedTable = { + ...table, + rows: [], + }; + const args = buildArgs(); + const updatedArgs = { + ...args, + columns: [ + { + type: 'lens_datatable_column', + columnId: 'bucket1', + isTransposed: true, + transposable: false, + }, + ], + } as DatatableArgs; + transposeTable(updatedArgs, updatedTable, buildFormatters()); + expect(args.columns.length).toEqual(5); + }); }); diff --git a/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.ts b/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.ts index e2d928fda24ed..d31080566dfbe 100644 --- a/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.ts +++ b/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.ts @@ -121,7 +121,7 @@ function updateColumnArgs( ) { args.columns = [...bucketsColumnArgs]; // add first column from each group, then add second column for each group, ... - transposedColumnGroups[0].forEach((_, index) => { + transposedColumnGroups[0]?.forEach((_, index) => { transposedColumnGroups.forEach((transposedColumnGroup) => { args.columns.push(transposedColumnGroup[index]); });