Skip to content

Commit

Permalink
STCOM-1242: Doesn't replace an unsupported search option if the advan…
Browse files Browse the repository at this point in the history
…ced search modal is not open. (#2189)
  • Loading branch information
Dmytro-Melnyshyn authored Dec 29, 2023
1 parent 4b8de30 commit f45e806
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
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
1 change: 1 addition & 0 deletions lib/AdvancedSearch/AdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const AdvancedSearch = ({
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');
});
});
});
11 changes: 8 additions & 3 deletions lib/AdvancedSearch/hooks/useAdvancedSearch/useAdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const createInitialRowState = (firstRowInitialSearch, defaultSearchOptionValue)
}));
};

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

const hasSupportedSearchOption = searchOptions.some(option => option.value === row.searchOption);

if (hasQueryOption || hasSupportedSearchOption) {
Expand All @@ -56,6 +60,7 @@ const useAdvancedSearch = ({
queryToRow = defaultQueryToRow,
searchOptions = [],
hasQueryOption,
open,
}) => {
const initialRowState = useMemo(() => {
const initialRows = createInitialRowState(firstRowInitialSearch, defaultSearchOptionValue);
Expand All @@ -68,10 +73,10 @@ const useAdvancedSearch = ({
const [prevFirstRowInitialSearch, setPrevFirstRowInitialSearch] = useState(firstRowInitialSearch);
const filledRows = useMemo(() => {
const rows = splitQueryRows(rowState, queryToRow)
.map(row => replaceUnsupportedOptions(row, searchOptions, defaultSearchOptionValue, hasQueryOption));
.map(row => replaceUnsupportedOptions(row, searchOptions, defaultSearchOptionValue, hasQueryOption, open));

return filterFilledRows(rows)
}, [rowState, queryToRow, searchOptions, defaultSearchOptionValue, hasQueryOption]);
}, [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';
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 f45e806

Please sign in to comment.