TwoStoryRobot / react-search-lunr

Lunr, as a React component
MIT License
1 stars 4 forks source link

Allow other fields to work as IDs and unit test #47

Closed joao-conde closed 4 years ago

joao-conde commented 4 years ago

This PR is a re-opening of #39.

The ID key of each entry may not be id, so looking for

        ...
        item: this.state.documents.find(m => m.id == ref),
        ...

will fail if the key is for example named name or something else.

Instead, using

        ...
        item: this.state.documents.find(m => m[this.props.id] == ref),
        ...

will work even if the id field is named anything other then id

A unit test was also added, testing if the results are correct when using as id the name field:

test('should render results using a selected field as ID', async () => {
  const { container, queryByText } = render(
    <ReactSearchLunr
      id="name"
      documents={documents}
      fields={['body']}
      filter="test">
      {mapResults}
    </ReactSearchLunr>
  )

  expect(queryByText('test a')).toBeInTheDocument()
  expect(queryByText('test b')).toBeInTheDocument()
  expect(queryByText('ignore c')).not.toBeInTheDocument()

  expect(container.firstChild).toMatchSnapshot()
})
joao-conde commented 4 years ago

@jonotron unit test added

jonotron commented 4 years ago

@joao-conde Thanks for the fix and the test! Good luck with the rest of Hacktoberfest!

joao-conde commented 4 years ago

Thanks @jonotron. I opened the first one roughly a year ago when I was actually building something with ReactSearchLunr and at the time fixed it for myself like this. Glad to contribute to open source, even outside of Hacktoberfest!

jonotron commented 4 years ago

@joao-conde Yes, I noticed that first PR was from 2019. Are you still building that project? Are you able to share? Would love to see where this little project is getting used.

joao-conde commented 4 years ago

@jonotron sadly it is closed source and belongs to my old company :/