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]); });