From 7cbbb8a3be1608b7a48a0fc4c8fd2c890a4d2dda Mon Sep 17 00:00:00 2001 From: Lee Wilson Date: Mon, 19 Aug 2019 18:01:58 +0100 Subject: [PATCH] feat: Add entity click event --- src/entity-search/EntityList.jsx | 24 +- src/entity-search/EntitySearch.jsx | 4 +- src/entity-search/EntitySearch.stories.jsx | 1 + src/entity-search/EntitySearch.test.jsx | 40 ++ .../__snapshots__/EntitySearch.test.jsx.snap | 377 +++++++++++++++--- 5 files changed, 386 insertions(+), 60 deletions(-) diff --git a/src/entity-search/EntityList.jsx b/src/entity-search/EntityList.jsx index 2191c774..b9244ee0 100644 --- a/src/entity-search/EntityList.jsx +++ b/src/entity-search/EntityList.jsx @@ -30,6 +30,7 @@ const StyledEntity = styled('div')` } } ` +StyledEntity.displayName = 'StyledEntity' const StyledHeading = styled(H3)` margin: 0; @@ -51,22 +52,20 @@ const StyledMetaItem = styled('div')` } ` -const EntityList = ({ entities }) => { +const EntityList = ({ entities, onEntityClick }) => { return ( - {entities.map(({ heading, meta }) => { + {entities.map((entity) => { return ( - - {heading} - {Object.keys(meta).map((metaKey) => { - return ( - - {metaKey}: - {meta[metaKey]} - - ) - })} + onEntityClick(entity)}> + {entity.heading} + {Object.keys(entity.meta).map(metaKey => ( + + {metaKey}: + {entity.meta[metaKey]} + + ))} ) @@ -80,6 +79,7 @@ EntityList.propTypes = { heading: PropTypes.string.isRequired, meta: PropTypes.object.isRequired, })).isRequired, + onEntityClick: PropTypes.func.isRequired, } export default EntityList diff --git a/src/entity-search/EntitySearch.jsx b/src/entity-search/EntitySearch.jsx index 0428dbee..0a5612de 100644 --- a/src/entity-search/EntitySearch.jsx +++ b/src/entity-search/EntitySearch.jsx @@ -21,6 +21,7 @@ const EntitySearch = ({ onEntitySearch, entities, cannotFind, + onEntityClick, }) => { const { filters, setFilter } = useFilter() return ( @@ -31,7 +32,7 @@ const EntitySearch = ({ {entities && entities.length ? ( <> - + ) : null} @@ -61,6 +62,7 @@ EntitySearch.propTypes = { text: PropTypes.string.isRequired, onChangeClick: PropTypes.func.isRequired, }), + onEntityClick: PropTypes.func.isRequired, } EntitySearch.defaultProps = { diff --git a/src/entity-search/EntitySearch.stories.jsx b/src/entity-search/EntitySearch.stories.jsx index e80a3e76..2ead2f74 100644 --- a/src/entity-search/EntitySearch.stories.jsx +++ b/src/entity-search/EntitySearch.stories.jsx @@ -44,6 +44,7 @@ const EntitySearchForStorybook = ({ previouslySelected, cannotFindLink }) => { ], link: cannotFindLink, }} + onEntityClick={entity => alert(`Selected ${JSON.stringify(entity)}`)} /> ) } diff --git a/src/entity-search/EntitySearch.test.jsx b/src/entity-search/EntitySearch.test.jsx index e6a7a7cb..ce7d0aef 100644 --- a/src/entity-search/EntitySearch.test.jsx +++ b/src/entity-search/EntitySearch.test.jsx @@ -32,6 +32,7 @@ const wrapEntitySearch = ({ url: 'http://stillcannotfind.com', text: 'still cannot find', }, + onEntityClick = () => console.log('entity clicked'), } = {}) => { return mount() } @@ -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', + }, + }) + }) + }) }) diff --git a/src/entity-search/__snapshots__/EntitySearch.test.jsx.snap b/src/entity-search/__snapshots__/EntitySearch.test.jsx.snap index 6e6e9cc4..fb0e2884 100644 --- a/src/entity-search/__snapshots__/EntitySearch.test.jsx.snap +++ b/src/entity-search/__snapshots__/EntitySearch.test.jsx.snap @@ -1,8 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`EntitySearch when initially loading the entity search component should render the component without entities 1`] = ` -" - +" + @@ -129,8 +129,8 @@ exports[`EntitySearch when initially loading the entity search component should `; exports[`EntitySearch when the "Search" button has been clicked should render the component with entities 1`] = ` -" - +" + @@ -239,16 +239,16 @@ exports[`EntitySearch when the "Search" button has been clicked should render th - +
  1. - - -
    + + +

    @@ -279,16 +279,16 @@ exports[`EntitySearch when the "Search" button has been clicked should render th

    - +
  2. - - -
    + + +

    @@ -319,7 +319,7 @@ exports[`EntitySearch when the "Search" button has been clicked should render th

    - +
  3. @@ -412,8 +412,8 @@ exports[`EntitySearch when the "Search" button has been clicked should render th `; exports[`EntitySearch when the company name filter is applied should render the component with filtered entities 1`] = ` -" - +" + @@ -522,16 +522,16 @@ exports[`EntitySearch when the company name filter is applied should render the - +
    1. - - -
      + + +

      @@ -562,7 +562,7 @@ exports[`EntitySearch when the company name filter is applied should render the

      - +
    2. @@ -655,8 +655,8 @@ exports[`EntitySearch when the company name filter is applied should render the `; exports[`EntitySearch when the company postcode filter is applied should render the component with filtered entities 1`] = ` -" - +" + @@ -765,16 +765,259 @@ exports[`EntitySearch when the company postcode filter is applied should render - +
      1. + + +
        + + +

        + + + +

        + Some company name +

        +
        +
        +
        +

        +
        +
        + + +
        + + Address + : + + + 123 Fake Street, Brighton, BN1 4SE + +
        +
        +
        +
        +
        +
        +
      2. +
        +
        +
      +
      +
      +
      + + + +
      + + +
      + + + + + + + cannot find summary + + + + + + -
      +
      +
      + + + + + +
      +

      + + Try refining your search by taking the following actions: + +

      +
      +
      +
      +
      +
      +
      +
        +
      • + action 1 +
      • +
      • + action 2 +
      • +
      + + + + still cannot find + + + +
      +
      + + +
      +
      +
      +
      +
      +
      +
      + + + + + + + + + + + +
      +
      " +`; + +exports[`EntitySearch when the first entity search result is clicked should render the component 1`] = ` +" + + + + +
      + + + + + + +
      + + + +
      + + + + + +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + + + + + + +
      + + + +
      + + + + + +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      +
      + + + +
        + + +
      1. + + +

        @@ -805,7 +1048,47 @@ exports[`EntitySearch when the company postcode filter is applied should render

        - +
        +
      2. +
        +
        + + +
      3. + + +
        + + +

        + + + +

        + Some other company +

        +
        +
        +
        +

        +
        +
        + + +
        + + Address + : + + + 123 ABC Road, Brighton, BN2 9QB + +
        +
        +
        +
        +
        +
      4. @@ -898,8 +1181,8 @@ exports[`EntitySearch when the company postcode filter is applied should render `; exports[`EntitySearch when the the cannot find link has a callback should render the component 1`] = ` -" - +" + @@ -1008,16 +1291,16 @@ exports[`EntitySearch when the the cannot find link has a callback should render - +
        1. - - -
          + + +

          @@ -1048,16 +1331,16 @@ exports[`EntitySearch when the the cannot find link has a callback should render

          - +
        2. - - -
          + + +

          @@ -1088,7 +1371,7 @@ exports[`EntitySearch when the the cannot find link has a callback should render

          - +
        3. @@ -1181,8 +1464,8 @@ exports[`EntitySearch when the the cannot find link has a callback should render `; exports[`EntitySearch when there is a previously selected "Change" link which is clicked should render the component 1`] = ` -" - +" +

          previously selected @@ -1303,16 +1586,16 @@ exports[`EntitySearch when there is a previously selected "Change" link which is - +

          1. - - -
            + + +

            @@ -1343,16 +1626,16 @@ exports[`EntitySearch when there is a previously selected "Change" link which is

            - +
          2. - - -
            + + +

            @@ -1383,7 +1666,7 @@ exports[`EntitySearch when there is a previously selected "Change" link which is

            - +