From 516c49402cf68fe2e0e13e07c72c8236a879ee5c 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 2a7461187c7716..35a96a5fd15244 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 00e0de71395d36..ef07a87cfb9e10 100644 --- a/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.ts +++ b/x-pack/plugins/lens/common/expressions/datatable/transpose_helpers.ts @@ -118,7 +118,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]); });