diff --git a/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.test.ts b/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.test.ts index 58e06a1b6e6bf..209dc2704d0d7 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.test.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.test.ts @@ -6,7 +6,7 @@ */ import type { SecurityAppStore } from '../../../../common/store/types'; -import { TableId, dataTableActions } from '@kbn/securitysolution-data-table'; +import { dataTableActions, TableId } from '@kbn/securitysolution-data-table'; import type { CellActionExecutionContext } from '@kbn/cell-actions'; import { createToggleColumnCellActionFactory } from './toggle_column'; @@ -79,6 +79,7 @@ describe('createToggleColumnCellActionFactory', () => { afterEach(() => { mockToggleColumn.mockClear(); }); + it('should remove column', async () => { await toggleColumnAction.execute(context); expect(mockDispatch).toHaveBeenCalledWith( @@ -108,32 +109,46 @@ describe('createToggleColumnCellActionFactory', () => { ); }); - it('should call toggleColumn on the visible alerts table to add a column in alert', async () => { - const name = 'fake-field-name'; - await toggleColumnAction.execute({ - ...context, - data: [{ ...context.data[0], field: { ...context.data[0].field, name } }], - metadata: { - scopeId: TableId.alertsOnAlertsPage, - alertsTableRef: { - current: { toggleColumn: mockToggleColumn } as unknown as AlertsTableImperativeApi, + [ + TableId.alertsOnAlertsPage, + TableId.alertsOnCasePage, + TableId.alertsOnRuleDetailsPage, + TableId.alertsRiskInputs, + ].forEach((scopeId) => { + it(`should call toggleColumn on the visible alerts table to add a column in alert for TableId ${scopeId}`, async () => { + const name = 'fake-field-name'; + await toggleColumnAction.execute({ + ...context, + data: [{ ...context.data[0], field: { ...context.data[0].field, name } }], + metadata: { + scopeId, + alertsTableRef: { + current: { toggleColumn: mockToggleColumn } as unknown as AlertsTableImperativeApi, + }, }, - }, + }); + expect(mockToggleColumn).toHaveBeenCalledWith(name); }); - expect(mockToggleColumn).toHaveBeenCalledWith(name); }); - it('should call toggleColumn on the visible alerts table to remove a column in alert', async () => { - await toggleColumnAction.execute({ - ...context, - metadata: { - scopeId: TableId.alertsOnAlertsPage, - alertsTableRef: { - current: { toggleColumn: mockToggleColumn } as unknown as AlertsTableImperativeApi, + [ + TableId.alertsOnAlertsPage, + TableId.alertsOnCasePage, + TableId.alertsOnRuleDetailsPage, + TableId.alertsRiskInputs, + ].forEach((scopeId) => { + it(`should call toggleColumn on the visible alerts table to remove a column in alert for TableId ${scopeId}`, async () => { + await toggleColumnAction.execute({ + ...context, + metadata: { + scopeId: TableId.alertsOnAlertsPage, + alertsTableRef: { + current: { toggleColumn: mockToggleColumn } as unknown as AlertsTableImperativeApi, + }, }, - }, + }); + expect(mockToggleColumn).toHaveBeenCalledWith(fieldName); }); - expect(mockToggleColumn).toHaveBeenCalledWith(fieldName); }); }); }); diff --git a/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.ts b/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.ts index b41866fded5b6..6f2f4e87d36ae 100644 --- a/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.ts +++ b/x-pack/solutions/security/plugins/security_solution/public/app/actions/toggle_column/cell_action/toggle_column.ts @@ -6,12 +6,11 @@ */ import { i18n } from '@kbn/i18n'; -import { createCellActionFactory, type CellActionTemplate } from '@kbn/cell-actions/actions'; +import { type CellActionTemplate, createCellActionFactory } from '@kbn/cell-actions/actions'; import { + dataTableSelectors, defaultColumnHeaderType, tableDefaults, - dataTableSelectors, - TableId, } from '@kbn/securitysolution-data-table'; import { fieldHasCellActions } from '../../utils'; import type { SecurityAppStore } from '../../../../common/store'; @@ -69,11 +68,12 @@ export const createToggleColumnCellActionFactory = createCellActionFactory( } // When the flyout was initiated from an alerts table, use its toggleColumn action - if (metadata.alertsTableRef?.current && scopeId === TableId.alertsOnAlertsPage) { + // We can safely do this now as we do not have a page with multiple alerts tables. If that were the case we'd have problem with the ref + if (metadata.alertsTableRef?.current) { metadata.alertsTableRef.current.toggleColumn(field.name); return; } - + // Otherwise, use the store to dispatch the action, either for Timeline or the events table in the Explore pages const selector = isTimelineScope(scopeId) ? timelineSelectors.getTimelineByIdSelector() : dataTableSelectors.getTableByIdSelector();