Skip to content

Commit

Permalink
Merge branch 'master' into dayjs-refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnC-80 authored Jan 3, 2024
2 parents 0aa10a8 + f45e806 commit ce5073a
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 2 deletions.
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 index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export {
BOOLEAN_OPERATORS as ADVANCED_SEARCH_BOOLEAN_OPERATORS,
MATCH_OPTIONS as ADVANCED_SEARCH_MATCH_OPTIONS,
FIELD_NAMES as ADVANCED_SEARCH_FIELD_NAMES,
DEFAULT_SEARCH_OPTION as ADVANCED_SEARCH_DEFAULT_SEARCH_OPTION,
} from './lib/AdvancedSearch';

/* specific use */
Expand Down
2 changes: 2 additions & 0 deletions lib/AdvancedSearch/AdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const AdvancedSearch = ({
rowFormatter,
searchOptions,
queryToRow,
hasQueryOption,
open,
});

const renderRows = () => {
Expand Down
1 change: 1 addition & 0 deletions lib/AdvancedSearch/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const BOOLEAN_OPERATORS = {
NOT: 'not',
};
export const QUERY_OPTION_VALUE = 'advancedSearch';
export const DEFAULT_SEARCH_OPTION = 'keyword';
export const MATCH_OPTIONS = {
EXACT_PHRASE: 'exactPhrase',
CONTAINS_ALL: 'containsAll',
Expand Down
48 changes: 48 additions & 0 deletions lib/AdvancedSearch/hooks/tests/useAdvancedSearch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
describe,
it,
} from 'mocha';
import { expect } from 'chai';

import getHookExecutionResult from "../../../../tests/helpers/getHookExecutionResult";
import useAdvancedSearch from '../useAdvancedSearch';

describe('useAdvancedSearch', () => {
describe('when advanced search modal is closed', () => {
it('should not replace the search option with default one', async () => {
const firstRowInitialSearch = {
query: "identifiers.value==n83169267",
option: "advancedSearch",
};

const args = {
defaultSearchOptionValue: 'keyword',
firstRowInitialSearch,
open: false,
};

const hookResult = await getHookExecutionResult(useAdvancedSearch, args);

expect(hookResult.filledRows[0].searchOption).to.equal('identifiers.value');
});
});

describe('when advanced search modal is open', () => {
it('should replace the search option with default one', async () => {
const firstRowInitialSearch = {
query: "identifiers.value==n83169267",
option: "advancedSearch",
};

const args = {
defaultSearchOptionValue: 'keyword',
firstRowInitialSearch,
open: true,
};

const hookResult = await getHookExecutionResult(useAdvancedSearch, args);

expect(hookResult.filledRows[0].searchOption).to.equal('keyword');
});
});
});
26 changes: 25 additions & 1 deletion lib/AdvancedSearch/hooks/useAdvancedSearch/useAdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ const createInitialRowState = (firstRowInitialSearch, defaultSearchOptionValue)
}));
};

const replaceUnsupportedOptions = (row, searchOptions, defaultSearchOptionValue, hasQueryOption, open) => {
if (!open) {
return row;
}

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 +59,8 @@ const useAdvancedSearch = ({
rowFormatter = defaultRowFormatter,
queryToRow = defaultQueryToRow,
searchOptions = [],
hasQueryOption,
open,
}) => {
const initialRowState = useMemo(() => {
const initialRows = createInitialRowState(firstRowInitialSearch, defaultSearchOptionValue);
Expand All @@ -52,7 +71,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, open));

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

const searchOptionsWithQuery = useMemo(() => (
[{
Expand Down
1 change: 1 addition & 0 deletions lib/AdvancedSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export {
FIELD_NAMES,
BOOLEAN_OPERATORS,
MATCH_OPTIONS,
DEFAULT_SEARCH_OPTION,
} from './constants';
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
4 changes: 3 additions & 1 deletion tests/helpers/getHookExecutionResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { mountWithContext } from '../helpers';
const getHookExecutionResult = (hook, hookArguments = []) => {
let result = {};
const TestComponent = () => {
const hookResult = hook(...hookArguments);
const hookResult = Array.isArray(hookArguments)
? hook(...hookArguments)
: hook(hookArguments);
if (typeof hookResult === 'function') {
result = hookResult;
} else {
Expand Down

0 comments on commit ce5073a

Please sign in to comment.