From 8246d4a298b209a57378d124a721c06f230d406e Mon Sep 17 00:00:00 2001 From: mabashian Date: Mon, 21 Jun 2021 13:03:31 -0400 Subject: [PATCH] Remove smart inventories from host form inv lookup --- .../src/components/HostForm/HostForm.jsx | 1 + .../src/components/Lookup/InventoryLookup.jsx | 21 +++++++++-- .../Lookup/InventoryLookup.test.jsx | 36 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/awx/ui_next/src/components/HostForm/HostForm.jsx b/awx/ui_next/src/components/HostForm/HostForm.jsx index 6df19164d5..fb8f231262 100644 --- a/awx/ui_next/src/components/HostForm/HostForm.jsx +++ b/awx/ui_next/src/components/HostForm/HostForm.jsx @@ -39,6 +39,7 @@ const InventoryLookupField = ({ isDisabled }) => { error={inventoryMeta.error} validate={required(t`Select a value for this field`)} isDisabled={isDisabled} + hideSmartInventories /> ); diff --git a/awx/ui_next/src/components/Lookup/InventoryLookup.jsx b/awx/ui_next/src/components/Lookup/InventoryLookup.jsx index f4653321e9..29c667660d 100644 --- a/awx/ui_next/src/components/Lookup/InventoryLookup.jsx +++ b/awx/ui_next/src/components/Lookup/InventoryLookup.jsx @@ -7,7 +7,7 @@ import { Inventory } from '../../types'; import Lookup from './Lookup'; import OptionsList from '../OptionsList'; import useRequest from '../../util/useRequest'; -import { getQSConfig, parseQueryString } from '../../util/qs'; +import { getQSConfig, parseQueryString, mergeParams } from '../../util/qs'; import LookupErrorMessage from './shared/LookupErrorMessage'; import FieldWithPrompt from '../FieldWithPrompt'; @@ -32,6 +32,7 @@ function InventoryLookup({ validate, fieldName, isDisabled, + hideSmartInventories, }) { const { result: { @@ -47,8 +48,15 @@ function InventoryLookup({ } = useRequest( useCallback(async () => { const params = parseQueryString(QS_CONFIG, history.location.search); + const inventoryKindParams = hideSmartInventories + ? { not__kind: 'smart' } + : {}; const [{ data }, actionsResponse] = await Promise.all([ - InventoriesAPI.read(params), + InventoriesAPI.read( + mergeParams(params, { + ...inventoryKindParams, + }) + ), InventoriesAPI.readOptions(), ]); @@ -60,7 +68,12 @@ function InventoryLookup({ ).map(val => val.slice(0, -8)), searchableKeys: Object.keys( actionsResponse.data.actions?.GET || {} - ).filter(key => actionsResponse.data.actions?.GET[key].filterable), + ).filter(key => { + if (key === 'kind' && hideSmartInventories) { + return false; + } + return actionsResponse.data.actions?.GET[key].filterable; + }), canEdit: Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled, }; @@ -230,6 +243,7 @@ InventoryLookup.propTypes = { validate: func, fieldName: string, isDisabled: bool, + hideSmartInventories: bool, }; InventoryLookup.defaultProps = { @@ -239,6 +253,7 @@ InventoryLookup.defaultProps = { validate: () => {}, fieldName: 'inventory', isDisabled: false, + hideSmartInventories: false, }; export default withRouter(InventoryLookup); diff --git a/awx/ui_next/src/components/Lookup/InventoryLookup.test.jsx b/awx/ui_next/src/components/Lookup/InventoryLookup.test.jsx index 019bf50616..ac0662ad30 100644 --- a/awx/ui_next/src/components/Lookup/InventoryLookup.test.jsx +++ b/awx/ui_next/src/components/Lookup/InventoryLookup.test.jsx @@ -48,6 +48,42 @@ describe('InventoryLookup', () => { }); wrapper.update(); expect(InventoriesAPI.read).toHaveBeenCalledTimes(1); + expect(InventoriesAPI.read).toHaveBeenCalledWith({ + order_by: 'name', + page: 1, + page_size: 5, + role_level: 'use_role', + }); + expect(wrapper.find('InventoryLookup')).toHaveLength(1); + expect(wrapper.find('Lookup').prop('isDisabled')).toBe(false); + }); + + test('should fetch only regular inventories when hideSmartInventories is true', async () => { + InventoriesAPI.readOptions.mockReturnValue({ + data: { + actions: { + GET: {}, + POST: {}, + }, + related_search_fields: [], + }, + }); + await act(async () => { + wrapper = mountWithContexts( + + {}} hideSmartInventories /> + + ); + }); + wrapper.update(); + expect(InventoriesAPI.read).toHaveBeenCalledTimes(1); + expect(InventoriesAPI.read).toHaveBeenCalledWith({ + not__kind: 'smart', + order_by: 'name', + page: 1, + page_size: 5, + role_level: 'use_role', + }); expect(wrapper.find('InventoryLookup')).toHaveLength(1); expect(wrapper.find('Lookup').prop('isDisabled')).toBe(false); });