From 3545ce4531ed4ddd96be9e3bde8a3da6b3685a85 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 17 Jul 2023 06:27:44 -0400 Subject: [PATCH] [7.17] [Lens] Fixes error on table when there is no data (#161953) (#162018) # Backport This will backport the following commits from `main` to `7.17`: - [[Lens] Fixes error on table when there is no data (#161953)](https://github.com/elastic/kibana/pull/161953) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Stratoula Kalafateli --- .../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]); });