-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CoursewareSearchForm component (#1214)
* feat: Add CoursewareSearchBar component * fix: lint * fix: Clarified component names and i18n description * test: Add more tests * fix: lint * fix: Made props in CoursewareSearchForm optional
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
src/course-home/courseware-search/CoursewareSearchForm.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,37 @@ | ||
import React from 'react'; | ||
import { SearchField } from '@edx/paragon'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const CoursewareSearchForm = ({ | ||
onSubmit, | ||
onChange, | ||
placeholder, | ||
}) => ( | ||
<SearchField.Advanced | ||
onSubmit={onSubmit} | ||
onChange={onChange} | ||
submitButtonLocation="external" | ||
className="courseware-search-form" | ||
> | ||
<div className="pgn__searchfield_wrapper" data-testid="courseware-search-form"> | ||
<SearchField.Label /> | ||
<SearchField.Input placeholder={placeholder} /> | ||
<SearchField.ClearButton /> | ||
</div> | ||
<SearchField.SubmitButton submitButtonLocation="external" /> | ||
</SearchField.Advanced> | ||
); | ||
|
||
CoursewareSearchForm.propTypes = { | ||
onSubmit: PropTypes.func, | ||
onChange: PropTypes.func, | ||
placeholder: PropTypes.string, | ||
}; | ||
|
||
CoursewareSearchForm.defaultProps = { | ||
onSubmit: undefined, | ||
onChange: undefined, | ||
placeholder: undefined, | ||
}; | ||
|
||
export default CoursewareSearchForm; |
60 changes: 60 additions & 0 deletions
60
src/course-home/courseware-search/CoursewareSearchForm.test.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,60 @@ | ||
import React from 'react'; | ||
import { | ||
act, | ||
initializeMockApp, | ||
render, | ||
screen, | ||
waitFor, | ||
fireEvent, | ||
} from '../../setupTest'; | ||
import CoursewareSearchForm from './CoursewareSearchForm'; | ||
|
||
function renderComponent(placeholder, onSubmit, onChange) { | ||
const { container } = render(<CoursewareSearchForm | ||
placeholder={placeholder} | ||
onSubmit={onSubmit} | ||
onChange={onChange} | ||
/>); | ||
return container; | ||
} | ||
|
||
describe('CoursewareSearchToggle', () => { | ||
const placeholderText = 'Search for courseware'; | ||
let onSubmitHandlerMock; | ||
let onChangeHandlerMock; | ||
|
||
beforeAll(async () => { | ||
onChangeHandlerMock = jest.fn(); | ||
onSubmitHandlerMock = jest.fn(); | ||
initializeMockApp(); | ||
}); | ||
|
||
it('should render', async () => { | ||
await act(async () => renderComponent(placeholderText, onSubmitHandlerMock, onChangeHandlerMock)); | ||
await waitFor(() => { | ||
expect(screen.queryByTestId('courseware-search-form')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should call onChange handler when input changes', async () => { | ||
await act(async () => renderComponent(placeholderText, onSubmitHandlerMock, onChangeHandlerMock)); | ||
await waitFor(() => { | ||
const element = screen.queryByPlaceholderText(placeholderText); | ||
fireEvent.change(element, { target: { value: 'test' } }); | ||
expect(onChangeHandlerMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
it('should call onSubmit handler when submit is clicked', async () => { | ||
await act(async () => renderComponent(placeholderText, onSubmitHandlerMock, onChangeHandlerMock)); | ||
await waitFor(() => { | ||
const element = screen.queryAllByText('Search')[0]; | ||
fireEvent.click(element); | ||
expect(onSubmitHandlerMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
}); |
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