Skip to content

Commit

Permalink
STCOM-1242: Use the default search option instead of an unsupported o…
Browse files Browse the repository at this point in the history
…ne in Advanced search. (#2188)
  • Loading branch information
Dmytro-Melnyshyn committed Dec 21, 2023
1 parent c81bc42 commit 4b8de30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Accessibility Issues - stripes components. Refs STCOM-1222.
* Enable spinner on Datepicker year input. Refs STCOM-1225.
* TextLink - underline showing up on nested spans with 'display: inline-flex'. Refs STCOM-1226.
* Use the default search option instead of an unsupported one in Advanced search. Refs STCOM-1242.

## [12.0.0](https://github.com/folio-org/stripes-components/tree/v12.0.0) (2023-10-11)
[Full Changelog](https://github.com/folio-org/stripes-components/compare/v11.0.0...v12.0.0)
Expand Down
1 change: 1 addition & 0 deletions lib/AdvancedSearch/AdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const AdvancedSearch = ({
rowFormatter,
searchOptions,
queryToRow,
hasQueryOption,
});

const renderRows = () => {
Expand Down
21 changes: 20 additions & 1 deletion lib/AdvancedSearch/hooks/useAdvancedSearch/useAdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ const createInitialRowState = (firstRowInitialSearch, defaultSearchOptionValue)
}));
};

const replaceUnsupportedOptions = (row, searchOptions, defaultSearchOptionValue, hasQueryOption) => {
const hasSupportedSearchOption = searchOptions.some(option => option.value === row.searchOption);

if (hasQueryOption || hasSupportedSearchOption) {
return row;
}

return {
...row,
searchOption: defaultSearchOptionValue,
};
};

const useAdvancedSearch = ({
defaultSearchOptionValue,
firstRowInitialSearch,
Expand All @@ -42,6 +55,7 @@ const useAdvancedSearch = ({
rowFormatter = defaultRowFormatter,
queryToRow = defaultQueryToRow,
searchOptions = [],
hasQueryOption,
}) => {
const initialRowState = useMemo(() => {
const initialRows = createInitialRowState(firstRowInitialSearch, defaultSearchOptionValue);
Expand All @@ -52,7 +66,12 @@ const useAdvancedSearch = ({
const [rowState, setRowState] = useState(initialRowState);
const [showEmptyFirstRowMessage, setShowEmptyFirstRowMessage] = useState(false);
const [prevFirstRowInitialSearch, setPrevFirstRowInitialSearch] = useState(firstRowInitialSearch);
const filledRows = useMemo(() => filterFilledRows(splitQueryRows(rowState, queryToRow)), [rowState, queryToRow]);
const filledRows = useMemo(() => {
const rows = splitQueryRows(rowState, queryToRow)
.map(row => replaceUnsupportedOptions(row, searchOptions, defaultSearchOptionValue, hasQueryOption));

return filterFilledRows(rows)
}, [rowState, queryToRow, searchOptions, defaultSearchOptionValue, hasQueryOption]);

const searchOptionsWithQuery = useMemo(() => (
[{
Expand Down
18 changes: 18 additions & 0 deletions lib/AdvancedSearch/tests/AdvancedSearch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ describe('AdvancedSearch', () => {
});
});

describe('when there is an unsupported search option', () => {
beforeEach(async () => {
await renderComponent({
hasQueryOption: false,
firstRowInitialSearch: {
query: 'test',
option: 'querySearch',
},
});

await advancedSearch.search();
});

it('should be replaced with the default one', () => {
expect(onSearchSpy.calledOnceWith('keyword==test')).to.be.true;
});
});

describe('when searching with query in first row', () => {
beforeEach(async () => {
await RowInteractor({ index: 0 }).selectSearchOption(0, 'Query');
Expand Down

0 comments on commit 4b8de30

Please sign in to comment.