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 #106 from uktrade/feature/entity-search-entity-click
Browse files Browse the repository at this point in the history
Add search result onClick event to entity search component
  • Loading branch information
yeahfro committed Aug 21, 2019
2 parents f3061e4 + 7cbbb8a commit df59b25
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 60 deletions.
24 changes: 12 additions & 12 deletions src/entity-search/EntityList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const StyledEntity = styled('div')`
}
}
`
StyledEntity.displayName = 'StyledEntity'

const StyledHeading = styled(H3)`
margin: 0;
Expand All @@ -51,22 +52,20 @@ const StyledMetaItem = styled('div')`
}
`

const EntityList = ({ entities }) => {
const EntityList = ({ entities, onEntityClick }) => {
return (
<StyledEntityList>
{entities.map(({ heading, meta }) => {
{entities.map((entity) => {
return (
<StyledEntityListItem key={uniqueId()}>
<StyledEntity key={uniqueId()}>
<StyledHeading>{heading}</StyledHeading>
{Object.keys(meta).map((metaKey) => {
return (
<StyledMetaItem key={uniqueId()}>
<span>{metaKey}:</span>
<span>{meta[metaKey]}</span>
</StyledMetaItem>
)
})}
<StyledEntity key={uniqueId()} onClick={() => onEntityClick(entity)}>
<StyledHeading>{entity.heading}</StyledHeading>
{Object.keys(entity.meta).map(metaKey => (
<StyledMetaItem key={uniqueId()}>
<span>{metaKey}:</span>
<span>{entity.meta[metaKey]}</span>
</StyledMetaItem>
))}
</StyledEntity>
</StyledEntityListItem>
)
Expand All @@ -80,6 +79,7 @@ EntityList.propTypes = {
heading: PropTypes.string.isRequired,
meta: PropTypes.object.isRequired,
})).isRequired,
onEntityClick: PropTypes.func.isRequired,
}

export default EntityList
4 changes: 3 additions & 1 deletion src/entity-search/EntitySearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const EntitySearch = ({
onEntitySearch,
entities,
cannotFind,
onEntityClick,
}) => {
const { filters, setFilter } = useFilter()
return (
Expand All @@ -31,7 +32,7 @@ const EntitySearch = ({

{entities && entities.length ? (
<>
<EntityList entities={entities} />
<EntityList entities={entities} onEntityClick={onEntityClick} />
<CannotFindDetails {...cannotFind} />
</>
) : null}
Expand Down Expand Up @@ -61,6 +62,7 @@ EntitySearch.propTypes = {
text: PropTypes.string.isRequired,
onChangeClick: PropTypes.func.isRequired,
}),
onEntityClick: PropTypes.func.isRequired,
}

EntitySearch.defaultProps = {
Expand Down
1 change: 1 addition & 0 deletions src/entity-search/EntitySearch.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const EntitySearchForStorybook = ({ previouslySelected, cannotFindLink }) => {
],
link: cannotFindLink,
}}
onEntityClick={entity => alert(`Selected ${JSON.stringify(entity)}`)}
/>
)
}
Expand Down
40 changes: 40 additions & 0 deletions src/entity-search/EntitySearch.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const wrapEntitySearch = ({
url: 'http://stillcannotfind.com',
text: 'still cannot find',
},
onEntityClick = () => console.log('entity clicked'),
} = {}) => {
return mount(<EntitySearchWithDataProvider
previouslySelected={previouslySelected}
Expand All @@ -52,6 +53,7 @@ const wrapEntitySearch = ({
],
link: cannotFindLink,
}}
onEntityClick={onEntityClick}
/>)
}

Expand Down Expand Up @@ -200,4 +202,42 @@ describe('EntitySearch', () => {
expect(onChangeClick.mock.calls.length).toEqual(1)
})
})

describe('when the first entity search result is clicked', () => {
let wrappedEntitySearch
let onEntityClick

beforeAll(async () => {
onEntityClick = jest.fn()

wrappedEntitySearch = wrapEntitySearch({
onEntityClick,
})

wrappedEntitySearch.find('Search').simulate('click')

await act(flushPromises)

wrappedEntitySearch.update()

wrappedEntitySearch
.find('StyledEntity')
.at(0)
.simulate('click')
})

test('should render the component', async () => {
expect(wrappedEntitySearch.debug()).toMatchSnapshot()
})

test('should call the onEntityClick event', async () => {
expect(onEntityClick.mock.calls.length).toEqual(1)
expect(onEntityClick.mock.calls[0][0]).toEqual({
heading: 'Some company name',
meta: {
Address: '123 Fake Street, Brighton, BN1 4SE',
},
})
})
})
})
Loading

0 comments on commit df59b25

Please sign in to comment.