Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #118 from uktrade/bug/search-button-prevent-default
Browse files Browse the repository at this point in the history
fix: Fix "Search" button firing default event
  • Loading branch information
yeahfro authored Aug 27, 2019
2 parents 2643c3d + 23486dc commit 471f099
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 58 deletions.
7 changes: 6 additions & 1 deletion src/entity-search/EntitySearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const EntitySearch = ({
error,
}) => {
const { filters, setFilter } = useFilter()
const onSearchClick = (e) => {
e.preventDefault()
onEntitySearch(filters)
}

return (
<>
{previouslySelected && <PreviouslySelected {...previouslySelected} />}
Expand All @@ -44,7 +49,7 @@ const EntitySearch = ({

{error && <p>{error}</p>}

<StyledButton onClick={() => onEntitySearch(filters)}>Search</StyledButton>
<StyledButton onClick={onSearchClick}>Search</StyledButton>
</>
)
}
Expand Down
18 changes: 15 additions & 3 deletions src/entity-search/EntitySearch.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,27 @@ describe('EntitySearch', () => {
})

describe('when the "Search" button has been clicked', () => {
test('should render the component with entities', async () => {
const wrappedEntitySearch = wrapEntitySearch()
let wrappedEntitySearch
let preventDefaultMock

wrappedEntitySearch.find('Search').simulate('click')
beforeAll(async () => {
wrappedEntitySearch = wrapEntitySearch()
preventDefaultMock = jest.fn()

wrappedEntitySearch
.find('Search')
.simulate('click', { preventDefault: preventDefaultMock })

await act(flushPromises)

wrappedEntitySearch.update()
})

test('should prevent the default button action', () => {
expect(preventDefaultMock.mock.calls.length).toEqual(1)
})

test('should render the component with entities', () => {
expect(wrappedEntitySearch.debug()).toMatchSnapshot()
})
})
Expand Down
Loading

0 comments on commit 471f099

Please sign in to comment.