From 4df18f71185f49bfd93a434b7a3b245b55a46470 Mon Sep 17 00:00:00 2001 From: Partha Aji Date: Thu, 19 Oct 2023 14:59:12 -0400 Subject: [PATCH] Refs #36793 - Added TableHooks test and reorganized SelectAll to its own directory --- .../ActionKebab.js | 0 .../react_app/components/HostsIndex/index.js | 2 +- .../SelectAllCheckbox.scss | 0 .../index.js} | 4 +- .../PF4/TableIndexPage/Table/TableHooks.js | 2 +- .../TableIndexPage/Table/TableHooks.test.js | 99 +++++++++++++++++++ 6 files changed, 103 insertions(+), 4 deletions(-) rename webpack/assets/javascripts/react_app/components/{PF4/TableIndexPage => HostsIndex}/ActionKebab.js (100%) rename webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/{ => SelectAllCheckbox}/SelectAllCheckbox.scss (100%) rename webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/{SelectAllCheckbox.js => SelectAllCheckbox/index.js} (97%) create mode 100644 webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.test.js diff --git a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/ActionKebab.js b/webpack/assets/javascripts/react_app/components/HostsIndex/ActionKebab.js similarity index 100% rename from webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/ActionKebab.js rename to webpack/assets/javascripts/react_app/components/HostsIndex/ActionKebab.js diff --git a/webpack/assets/javascripts/react_app/components/HostsIndex/index.js b/webpack/assets/javascripts/react_app/components/HostsIndex/index.js index 292bb105b5d4..e857076f9558 100644 --- a/webpack/assets/javascripts/react_app/components/HostsIndex/index.js +++ b/webpack/assets/javascripts/react_app/components/HostsIndex/index.js @@ -5,7 +5,7 @@ import { Td } from '@patternfly/react-table'; import { ToolbarItem } from '@patternfly/react-core'; import { translate as __ } from '../../common/I18n'; import TableIndexPage from '../PF4/TableIndexPage/TableIndexPage'; -import { ActionKebab } from '../PF4/TableIndexPage/ActionKebab'; +import { ActionKebab } from './ActionKebab'; import { HOSTS_API_PATH, API_REQUEST_KEY } from '../../routes/Hosts/constants'; import { selectKebabItems } from './Selectors'; import { useAPI } from '../../common/hooks/API/APIHooks'; diff --git a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox.scss b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox/SelectAllCheckbox.scss similarity index 100% rename from webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox.scss rename to webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox/SelectAllCheckbox.scss diff --git a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox.js b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox/index.js similarity index 97% rename from webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox.js rename to webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox/index.js index b63f23695993..9b7221bcdd7f 100644 --- a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox.js +++ b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/SelectAllCheckbox/index.js @@ -6,8 +6,8 @@ import { DropdownToggleCheckbox, DropdownItem, } from '@patternfly/react-core'; -import { translate as __ } from '../../../../common/I18n'; -import { noop } from '../../../../common/helpers'; +import { translate as __ } from '../../../../../common/I18n'; +import { noop } from '../../../../../common/helpers'; import './SelectAllCheckbox.scss'; diff --git a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.js b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.js index 3a322f1953c7..359b76f1a39d 100644 --- a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.js +++ b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.js @@ -93,7 +93,7 @@ export const useSelectionSet = ({ const selectPage = () => { const selectablePageIds = pageIds.filter(canSelect); selectionSet.addAll(selectablePageIds); - selectableResults.forEach (result => { + selectableResults.forEach(result => { selectedResults.current[result[idColumn]] = result; }); }; diff --git a/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.test.js b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.test.js new file mode 100644 index 000000000000..7271946e4689 --- /dev/null +++ b/webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.test.js @@ -0,0 +1,99 @@ +import { act, renderHook } from '@testing-library/react-hooks'; +import { useBulkSelect } from './TableHooks'; + +const isSelectable = () => true; +const idColumn = 'errata_id'; +const metadata = { + error: null, selectable: 2, subtotal: 2, total: 2, +}; +const results = [ + { + errata_id: 'RHSA-2022:2031', + id: 311, + severity: 'Low', + type: 'security', + }, + { + errata_id: 'RHSA-2022:2110', + id: 17, + severity: 'Low', + type: 'security', + }, +]; + +it('returns a scoped search string based on inclusionSet', () => { + const { result } = renderHook(() => useBulkSelect({ + results, + metadata, + idColumn, + isSelectable, + })); + + act(() => { + result.current.selectOne(true, 'RHSA-2022:2031'); + }); + + expect(result.current.fetchBulkParams()).toBe('errata_id ^ (RHSA-2022:2031)'); +}); + +it('returns a scoped search string based on exclusionSet', () => { + const { result } = renderHook(() => useBulkSelect({ + results, + metadata, + idColumn, + isSelectable, + })); + + act(() => { + result.current.selectAll(true); + }); + + act(() => { + result.current.selectOne(false, 'RHSA-2022:2031'); + }); + + expect(result.current.fetchBulkParams()).toBe('errata_id !^ (RHSA-2022:2031)'); +}); + +it('adds search query to scoped search string based on exclusionSet', () => { + const { result } = renderHook(() => useBulkSelect({ + results, + metadata, + idColumn, + isSelectable, + })); + + act(() => { + result.current.updateSearchQuery('type=security'); + }); + + act(() => { + result.current.selectAll(true); + }); + + act(() => { + result.current.selectOne(false, 'RHSA-2022:2031'); + }); + + expect(result.current.fetchBulkParams()).toBe('type=security and errata_id !^ (RHSA-2022:2031)'); +}); + +it('adds filter dropdown query to scoped search string', () => { + const { result } = renderHook(() => useBulkSelect({ + results, + metadata, + idColumn, + isSelectable, + filtersQuery: 'severity=Low', + })); + + act(() => { + result.current.selectAll(true); + }); + + act(() => { + result.current.selectOne(false, 'RHSA-2022:2031'); + }); + + expect(result.current.fetchBulkParams()).toBe('severity=Low and errata_id !^ (RHSA-2022:2031)'); +});