diff --git a/awx/ui_next/src/components/Search/Search.jsx b/awx/ui_next/src/components/Search/Search.jsx index 10f75c4ab8..6d8d599322 100644 --- a/awx/ui_next/src/components/Search/Search.jsx +++ b/awx/ui_next/src/components/Search/Search.jsx @@ -80,17 +80,19 @@ class Search extends React.Component { const { searchKey, searchValue } = this.state; const { onSearch, qsConfig } = this.props; - const isNonStringField = - qsConfig.integerFields.find(field => field === searchKey) || - qsConfig.dateFields.find(field => field === searchKey); + if (searchValue) { + const isNonStringField = + qsConfig.integerFields.find(field => field === searchKey) || + qsConfig.dateFields.find(field => field === searchKey); - const actualSearchKey = isNonStringField - ? searchKey - : `${searchKey}__icontains`; + const actualSearchKey = isNonStringField + ? searchKey + : `${searchKey}__icontains`; - onSearch(actualSearchKey, searchValue); + onSearch(actualSearchKey, searchValue); - this.setState({ searchValue: '' }); + this.setState({ searchValue: '' }); + } } handleSearchInputChange(searchValue) { @@ -276,13 +278,16 @@ class Search extends React.Component { onChange={this.handleSearchInputChange} onKeyDown={this.handleTextKeyDown} /> - +
+ +
)} diff --git a/awx/ui_next/src/components/Search/Search.test.jsx b/awx/ui_next/src/components/Search/Search.test.jsx index 6a14148120..7ff46b9108 100644 --- a/awx/ui_next/src/components/Search/Search.test.jsx +++ b/awx/ui_next/src/components/Search/Search.test.jsx @@ -92,4 +92,53 @@ describe('', () => { .handleDropdownSelect({ target: { innerText: 'Description' } }); expect(wrapper.state('searchKey')).toEqual('description'); }); + + test('attempt to search with empty string', () => { + const searchButton = 'button[aria-label="Search submit button"]'; + const searchTextInput = 'input[aria-label="Search text input"]'; + const columns = [{ name: 'Name', key: 'name', isDefault: true }]; + const onSearch = jest.fn(); + const wrapper = mountWithContexts( + {}} + collapseListedFiltersBreakpoint="md" + > + + + + + ); + + wrapper.find(searchTextInput).instance().value = ''; + wrapper.find(searchTextInput).simulate('change'); + wrapper.find(searchButton).simulate('click'); + + expect(onSearch).toHaveBeenCalledTimes(0); + }); + + test('search with a valid string', () => { + const searchButton = 'button[aria-label="Search submit button"]'; + const searchTextInput = 'input[aria-label="Search text input"]'; + const columns = [{ name: 'Name', key: 'name', isDefault: true }]; + const onSearch = jest.fn(); + const wrapper = mountWithContexts( + {}} + collapseListedFiltersBreakpoint="md" + > + + + + + ); + + wrapper.find(searchTextInput).instance().value = 'test-321'; + wrapper.find(searchTextInput).simulate('change'); + wrapper.find(searchButton).simulate('click'); + + expect(onSearch).toHaveBeenCalledTimes(1); + expect(onSearch).toBeCalledWith('name__icontains', 'test-321'); + }); });