Skip to content

Commit 2b90d83

Browse files
committed
fix(ga): issue #606 - update unit test and code
1 parent a1a48f5 commit 2b90d83

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

packages/webapp/src/webview/ui/components/Header/SearchField/SearchField.tsx

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import React, { useCallback, useEffect, useState } from 'react';
2-
import type { FC } from 'react';
3-
import type { ChangeEvent } from 'react';
4-
import { useSelector, useDispatch } from 'react-redux';
1+
import React, { useEffect, useState } from 'react';
52

63
import { UISearchBox } from '@sap-ux/ui-components';
74

@@ -17,20 +14,16 @@ import {
1714
} from '../../../../state/reducers';
1815
import { Filters } from '../Filters';
1916

20-
const SEARCH_TIMEOUT = 250;
21-
2217
/**
2318
*
2419
* @returns An input field
2520
*/
26-
export const SearchField: FC = (): JSX.Element => {
27-
const dispatch = useDispatch();
28-
21+
export const SearchField: React.FC = (): JSX.Element => {
2922
const networkStatus: string = useAppSelector(getNetworkStatus);
3023
const productFilters: string[] = useAppSelector(getProductFilters);
3124
const componentFilters: string[] = useAppSelector(getComponentFilters);
3225
const activeScreen: string = useAppSelector(getActiveScreen);
33-
const activeSearch: string = useSelector(getSearchQuery);
26+
const activeSearch: string = useAppSelector(getSearchQuery);
3427

3528
const [searchTerm, setSearchTerm] = useState<string>(activeSearch);
3629

@@ -40,21 +33,12 @@ export const SearchField: FC = (): JSX.Element => {
4033
}
4134
};
4235

43-
const onChange = (_?: ChangeEvent<HTMLInputElement> | undefined, newSearchTerm = ''): void => {
44-
if (!/\S/.test(newSearchTerm)) {
45-
newSearchTerm = '';
46-
}
47-
if (activeSearch !== newSearchTerm) {
48-
actions.setQueryValue(newSearchTerm);
49-
}
50-
};
51-
5236
const onSearch = (searchItem: string): void => {
5337
if (!/\S/.test(searchItem)) {
5438
searchItem = '';
5539
}
5640
if (activeSearch !== searchItem) {
57-
dispatch(actions.setQueryValue(searchItem));
41+
actions.setQueryValue(searchItem);
5842
}
5943
};
6044

@@ -69,7 +53,7 @@ export const SearchField: FC = (): JSX.Element => {
6953
component: componentFilters
7054
},
7155
{
72-
responseSize: 20, //appState.pageSize,
56+
responseSize: 20,
7357
offset: 0
7458
}
7559
);
@@ -85,7 +69,6 @@ export const SearchField: FC = (): JSX.Element => {
8569
placeholder="Search Guided Answers"
8670
id="search-field"
8771
onClear={onClear}
88-
onChange={onChange}
8972
onSearch={onSearch}></UISearchBox>
9073
{activeScreen === 'SEARCH' && <Filters />}
9174
</div>

packages/webapp/test/Header/SearchField.test.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('<SearchField />', () => {
2525
});
2626

2727
test('Should render a SearchField component, on home screen', () => {
28-
const { container } = renderSearch(Object.assign({}, appState, { activeScreen: 'HOME' }));
28+
const { container } = renderSearch({ ...appState, activeScreen: 'HOME' });
2929
expect(container).toMatchSnapshot();
3030
});
3131

@@ -37,9 +37,10 @@ describe('<SearchField />', () => {
3737
if (searchInput) {
3838
fireEvent.focus(searchInput);
3939
fireEvent.input(searchInput, { target: { value: 'test' } });
40-
fireEvent.blur(searchInput);
40+
fireEvent.keyDown(searchInput, { key: 'Enter', code: 'Enter', keyCode: 13 });
4141
}
4242

43+
// TODO: Need to fix the redux store
4344
// expect(spyOnSearch).toHaveBeenCalledWith('test');
4445
});
4546
});

0 commit comments

Comments
 (0)