From f210cb29abdd956336924180576a03dba73d0c54 Mon Sep 17 00:00:00 2001 From: "Christiane (Tina) Heiligers" Date: Fri, 13 Dec 2024 10:39:48 -0700 Subject: [PATCH] Fix delete disabled state (#204154) fix https://github.com/elastic/kibana/issues/204095 Disables delete when no saved objects are selected or all objects selected are hidden and delete is allowed. ![Screenshot 2024-12-12 at 18 02 10](https://github.com/user-attachments/assets/e894a087-7906-4905-bd2e-2325a90d37c0) ## Summary Summarize your PR. If it involves visual changes include a screenshot or gif. - [x] [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 4ee52227bce2a1ffffaf930d41513bb0c644c199) --- .../__snapshots__/table.test.tsx.snap | 235 +----------------- .../objects_table/components/table.test.tsx | 30 ++- .../objects_table/components/table.tsx | 11 +- 3 files changed, 36 insertions(+), 240 deletions(-) diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/components/__snapshots__/table.test.tsx.snap b/src/plugins/saved_objects_management/public/management_section/objects_table/components/__snapshots__/table.test.tsx.snap index bc0eda7e6d5a5..a3bcf4da52544 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/components/__snapshots__/table.test.tsx.snap +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/components/__snapshots__/table.test.tsx.snap @@ -1,239 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Table prevents hidden saved objects from being deleted 1`] = ` - - - } - delay="regular" - display="inlineBlock" - position="top" - > - - - - , - - - - } - closePopover={[Function]} - display="inline-block" - hasArrow={true} - isOpen={false} - ownFocus={true} - panelPaddingSize="m" - repositionToCrossAxis={true} - > - - } - > - - } - name="includeReferencesDeep" - onChange={[Function]} - /> - - - - - - - , - ] - } - /> - -
- - } - onChange={[Function]} - pagination={ - Object { - "pageIndex": 1, - "pageSize": 2, - "pageSizeOptions": Array [ - 5, - 10, - 20, - 50, - ], - "totalItemCount": 3, - } - } - rowProps={[Function]} - selection={ - Object { - "onSelectionChange": [Function], - } - } - sorting={ - Object { - "sort": Object { - "direction": "desc", - "field": "updated_at", - }, - } - } - tableLayout="fixed" - /> -
-
-`; - exports[`Table should render normally 1`] = ` { expect(component.state().isSearchTextValid).toBe(true); }); - it(`prevents hidden saved objects from being deleted`, () => { + it(`prevents hidden saved objects from being deleted`, async () => { const selectedSavedObjects = [ { type: 'visualization', meta: { hiddenType: true } }, { type: 'search', meta: { hiddenType: true } }, @@ -124,9 +124,33 @@ describe('Table', () => { selectedSavedObjects, capabilities: { savedObjectsManagement: { delete: false } } as any, }; - const component = shallowWithI18nProvider(); + render( + +
+ + ); - expect(component).toMatchSnapshot(); + await waitFor(() => { + expect(screen.getByTestId('savedObjectsManagementDelete')).toBeDisabled(); + }); + }); + + it(`disables delete when no objects are selected `, async () => { + const selectedSavedObjects = [] as any; + const customizedProps = { + ...defaultProps, + selectedSavedObjects, + capabilities: { savedObjectsManagement: { delete: true } } as any, + }; + render( + +
+ + ); + + await waitFor(() => { + expect(screen.getByTestId('savedObjectsManagementDelete')).toBeDisabled(); + }); }); it(`allows for automatic refreshing after an action`, () => { diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/components/table.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/components/table.tsx index a32a1e9e958e1..73e6e2978429b 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/components/table.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/components/table.tsx @@ -386,8 +386,9 @@ export class Table extends PureComponent { const activeActionContents = this.state.activeAction?.render() ?? null; const exceededResultCount = totalItemCount > MAX_PAGINATED_ITEM; - const allHidden = selectedSavedObjects.every(({ meta: { hiddenType } }) => hiddenType); - + const anySelected = selectedSavedObjects.length > 0; + const allHidden = + anySelected && selectedSavedObjects.every(({ meta: { hiddenType } }) => hiddenType); return ( {activeActionContents} @@ -403,6 +404,8 @@ export class Table extends PureComponent { defaultQuery={this.props.initialQuery} toolsRight={[ { iconType="trash" color="danger" onClick={onDelete} - isDisabled={allHidden || !capabilities.savedObjectsManagement.delete} + isDisabled={ + !anySelected || allHidden || !capabilities.savedObjectsManagement.delete + } title={ capabilities.savedObjectsManagement.delete ? undefined