forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
“yauheni-kryzhyk-deriv”
committed
Oct 19, 2022
1 parent
1dbb867
commit 9887813
Showing
336 changed files
with
4,421 additions
and
2,571 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/cashier/src/components/cashier-search-box/__tests__/cashier-search-box.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { fireEvent, screen, render, waitFor } from '@testing-library/react'; | ||
import CashierSearchBox from '../cashier-search-box'; | ||
|
||
describe('<CashierSearchBox />', () => { | ||
const props = { | ||
onClear: jest.fn(), | ||
onSearch: jest.fn(), | ||
placeholder: 'Search placeholder', | ||
setIsSearchLoading: jest.fn(), | ||
}; | ||
|
||
it('should have close icon if there is a text in input field', () => { | ||
render(<CashierSearchBox {...props} />); | ||
|
||
const el_search_input = screen.getByPlaceholderText('Search placeholder'); | ||
fireEvent.change(el_search_input, { target: { value: 'hello' } }); | ||
|
||
expect(screen.getByTestId('dt_close_icon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should trigger onClear callback when the user clicks on close icon', () => { | ||
render(<CashierSearchBox {...props} />); | ||
|
||
const el_search_input = screen.getByPlaceholderText('Search placeholder'); | ||
fireEvent.change(el_search_input, { target: { value: 'hello' } }); | ||
const el_close_icon = screen.getByTestId('dt_close_icon'); | ||
fireEvent.click(el_close_icon); | ||
|
||
expect(props.onClear).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not trigger setTimeout callback (formSubmit) when the user presses backspace button on empty search input', () => { | ||
jest.useFakeTimers(); | ||
jest.spyOn(global, 'setTimeout'); | ||
render(<CashierSearchBox {...props} />); | ||
|
||
const el_search_input = screen.getByPlaceholderText('Search placeholder'); | ||
fireEvent.keyDown(el_search_input); | ||
|
||
expect(props.setIsSearchLoading).not.toHaveBeenCalled(); | ||
expect(setTimeout).not.toHaveBeenCalled(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should trigger setIsSearchLoading, onSearch and setTimeout callbacks when the user enters the search term', async () => { | ||
jest.useFakeTimers(); | ||
jest.spyOn(global, 'setTimeout'); | ||
|
||
render(<CashierSearchBox {...props} />); | ||
|
||
const el_search_input = screen.getByPlaceholderText('Search placeholder'); | ||
await waitFor(() => { | ||
fireEvent.change(el_search_input, { target: { value: 'hello' } }); | ||
fireEvent.keyUp(el_search_input); | ||
|
||
expect(props.setIsSearchLoading).toHaveBeenCalled(); | ||
expect(setTimeout).toHaveBeenCalled(); | ||
expect(props.onSearch).toHaveBeenCalled(); | ||
}); | ||
jest.useRealTimers(); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
packages/cashier/src/components/cashier-search-box/cashier-search-box.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import classNames from 'classnames'; | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { Field as FormField, Formik, Form } from 'formik'; | ||
import { Icon, Input } from '@deriv/components'; | ||
import './cashier-search-box.scss'; | ||
|
||
const CashierSearchBox = ({ className, onClear, onSearch, placeholder, search_term, setIsSearchLoading }) => { | ||
React.useEffect(() => { | ||
return onClear; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const onSearchClear = setFieldValue => { | ||
setFieldValue('search', ''); | ||
onClear(); | ||
}; | ||
|
||
const onSearchKeyUpDown = (submitForm, search) => { | ||
if (!search.trim() && search_term) { | ||
onClear(); | ||
} else if (!search.trim()) return; | ||
|
||
setIsSearchLoading(true); | ||
submitForm(); | ||
}; | ||
|
||
const onSearchSubmit = ({ search }) => { | ||
onSearch(search); | ||
}; | ||
|
||
return ( | ||
<div className={classNames('cashier-search-box', className)}> | ||
<Formik initialValues={{ search: '' }} onSubmit={onSearchSubmit}> | ||
{({ submitForm, values: { search }, setFieldValue }) => ( | ||
<Form> | ||
<FormField name='search'> | ||
{({ field }) => ( | ||
<Input | ||
{...field} | ||
type='text' | ||
name='search' | ||
placeholder={placeholder} | ||
onKeyUp={() => onSearchKeyUpDown(submitForm, search)} | ||
onKeyDown={() => onSearchKeyUpDown(submitForm, search)} | ||
leading_icon={<Icon icon='IcSearch' data_testid='dt_search_icon' />} | ||
trailing_icon={ | ||
search && ( | ||
<Icon | ||
color='general' | ||
data_testid='dt_close_icon' | ||
icon='IcCloseCircle' | ||
onClick={() => onSearchClear(setFieldValue)} | ||
/> | ||
) | ||
} | ||
/> | ||
)} | ||
</FormField> | ||
</Form> | ||
)} | ||
</Formik> | ||
</div> | ||
); | ||
}; | ||
|
||
CashierSearchBox.propTypes = { | ||
className: PropTypes.string, | ||
onClear: PropTypes.func, | ||
onSearch: PropTypes.func, | ||
placeholder: PropTypes.string, | ||
search_term: PropTypes.string, | ||
setIsSearchLoading: PropTypes.func, | ||
}; | ||
|
||
export default CashierSearchBox; |
20 changes: 20 additions & 0 deletions
20
packages/cashier/src/components/cashier-search-box/cashier-search-box.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.cashier-search-box { | ||
width: 100%; | ||
margin-right: 1.6rem; | ||
.dc-input { | ||
margin-bottom: 0; | ||
height: 3.2rem; | ||
overflow: hidden; | ||
&__field { | ||
padding: 0.6rem 3.2rem; | ||
height: 3.2rem; | ||
} | ||
&__leading-icon { | ||
margin-left: 0.8rem; | ||
top: 0.8rem; | ||
} | ||
&__trailing-icon { | ||
cursor: pointer; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import CashierSearchBox from './cashier-search-box.jsx'; | ||
|
||
export default CashierSearchBox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.