Skip to content
Snippets Groups Projects
Commit b95403b3 authored by Tadeusz Miesiąc's avatar Tadeusz Miesiąc
Browse files

feat(search bar): open content drawer search on search query

parent 9718c63c
No related branches found
No related tags found
2 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!30Resolve MIN-96 "Feature/ open drawer after search"
import searchReducer from '@/redux/search/search.slice'; import { getReduxWrapperWithStore } from '@/utils/testing/getReduxWrapperWithStore';
import type { SearchState } from '@/redux/search/search.types'; import { StoreType } from '@/redux/store';
import { ToolkitStoreWithSingleSlice } from '@/utils/createStoreInstanceUsingSliceReducer';
import { getReduxWrapperUsingSliceReducer } from '@/utils/testing/getReduxWrapperUsingSliceReducer';
import { fireEvent, render, screen } from '@testing-library/react'; import { fireEvent, render, screen } from '@testing-library/react';
import { SearchBar } from './SearchBar.component'; import { SearchBar } from './SearchBar.component';
const renderComponent = (): { store: ToolkitStoreWithSingleSlice<SearchState> } => { const renderComponent = (): { store: StoreType } => {
const { Wrapper, store } = getReduxWrapperUsingSliceReducer('search', searchReducer); const { Wrapper, store } = getReduxWrapperWithStore();
return ( return (
render( render(
...@@ -23,8 +21,8 @@ const renderComponent = (): { store: ToolkitStoreWithSingleSlice<SearchState> } ...@@ -23,8 +21,8 @@ const renderComponent = (): { store: ToolkitStoreWithSingleSlice<SearchState> }
describe('SearchBar - component', () => { describe('SearchBar - component', () => {
it('should let user type text', () => { it('should let user type text', () => {
renderComponent(); renderComponent();
const input = screen.getByTestId<HTMLInputElement>('search-input'); const input = screen.getByTestId<HTMLInputElement>('search-input');
fireEvent.change(input, { target: { value: 'test value' } }); fireEvent.change(input, { target: { value: 'test value' } });
expect(input.value).toBe('test value'); expect(input.value).toBe('test value');
...@@ -32,13 +30,12 @@ describe('SearchBar - component', () => { ...@@ -32,13 +30,12 @@ describe('SearchBar - component', () => {
it('should disable button when the user clicks the lens button', () => { it('should disable button when the user clicks the lens button', () => {
renderComponent(); renderComponent();
const input = screen.getByTestId<HTMLInputElement>('search-input'); const input = screen.getByTestId<HTMLInputElement>('search-input');
fireEvent.change(input, { target: { value: 'park7' } });
expect(input.value).toBe('park7'); fireEvent.change(input, { target: { value: 'park7' } });
const button = screen.getByRole('button'); const button = screen.getByRole('button');
fireEvent.click(button); fireEvent.click(button);
expect(button).toBeDisabled(); expect(button).toBeDisabled();
...@@ -46,12 +43,9 @@ describe('SearchBar - component', () => { ...@@ -46,12 +43,9 @@ describe('SearchBar - component', () => {
it('should disable input when the user clicks the Enter', () => { it('should disable input when the user clicks the Enter', () => {
renderComponent(); renderComponent();
const input = screen.getByTestId<HTMLInputElement>('search-input'); const input = screen.getByTestId<HTMLInputElement>('search-input');
fireEvent.change(input, { target: { value: 'park7' } });
expect(input.value).toBe('park7');
fireEvent.change(input, { target: { value: 'park7' } });
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 }); fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 });
expect(input).toBeDisabled(); expect(input).toBeDisabled();
......
import lensIcon from '@/assets/vectors/icons/lens.svg'; import lensIcon from '@/assets/vectors/icons/lens.svg';
import { isDrawerOpenSelector } from '@/redux/drawer/drawer.selectors';
import { openDrawer } from '@/redux/drawer/drawer.slice';
import { useAppDispatch } from '@/redux/hooks/useAppDispatch'; import { useAppDispatch } from '@/redux/hooks/useAppDispatch';
import { import {
isPendingSearchStatusSelector, isPendingSearchStatusSelector,
...@@ -16,17 +18,31 @@ export const SearchBar = (): JSX.Element => { ...@@ -16,17 +18,31 @@ export const SearchBar = (): JSX.Element => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const isPendingSearchStatus = useSelector(isPendingSearchStatusSelector); const isPendingSearchStatus = useSelector(isPendingSearchStatusSelector);
const prevSearchValue = useSelector(searchValueSelector); const prevSearchValue = useSelector(searchValueSelector);
const isDrawerOpen = useSelector(isDrawerOpenSelector);
const isSameSearchValue = prevSearchValue === searchValue; const isSameSearchValue = prevSearchValue === searchValue;
const openSearchDrawerIfClosed = (): void => {
if (!isDrawerOpen) {
dispatch(openDrawer('search'));
}
};
const onSearchChange = (event: ChangeEvent<HTMLInputElement>): void => const onSearchChange = (event: ChangeEvent<HTMLInputElement>): void =>
setSearchValue(event.target.value); setSearchValue(event.target.value);
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type const onSearchClick = (): void => {
const onSearchClick = () => !isSameSearchValue && dispatch(getSearchData(searchValue)); if (!isSameSearchValue) {
dispatch(getSearchData(searchValue));
openSearchDrawerIfClosed();
}
};
const handleKeyPress = (event: KeyboardEvent<HTMLInputElement>): void => { const handleKeyPress = (event: KeyboardEvent<HTMLInputElement>): void => {
if (!isSameSearchValue && event.code === ENTER_KEY_CODE) dispatch(getSearchData(searchValue)); if (!isSameSearchValue && event.code === ENTER_KEY_CODE) {
dispatch(getSearchData(searchValue));
openSearchDrawerIfClosed();
}
}; };
return ( return (
......
import searchReducer from '@/redux/search/search.slice'; import { StoreType } from '@/redux/store';
import type { SearchState } from '@/redux/search/search.types'; import { getReduxWrapperWithStore } from '@/utils/testing/getReduxWrapperWithStore';
import { ToolkitStoreWithSingleSlice } from '@/utils/createStoreInstanceUsingSliceReducer';
import { getReduxWrapperUsingSliceReducer } from '@/utils/testing/getReduxWrapperUsingSliceReducer';
import { render, screen } from '@testing-library/react'; import { render, screen } from '@testing-library/react';
import { TopBar } from './TopBar.component'; import { TopBar } from './TopBar.component';
const renderComponent = (): { store: ToolkitStoreWithSingleSlice<SearchState> } => { const renderComponent = (): { store: StoreType } => {
const { Wrapper, store } = getReduxWrapperUsingSliceReducer('search', searchReducer); const { Wrapper, store } = getReduxWrapperWithStore();
return ( return (
render( render(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment