-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-search-fuse.test.js
79 lines (65 loc) · 2.22 KB
/
react-search-fuse.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from 'react'
import { render, cleanup } from 'react-testing-library'
import 'jest-dom/extend-expect'
import ReactSearchFuse from './react-search-fuse'
afterEach(cleanup)
const documents = [
{ id: 'a', character: 'Wolverine', name: 'Logan', team: 'X-Men' },
{ id: 'b', character: 'Hulk', name: 'Bruce Banner', team: 'Avengers' },
{
id: 'c',
character: 'Black Widow',
name: 'Natasha Romanoff',
team: 'Avengers'
},
{ id: 'd', character: 'Rogue', name: 'Anna Marie', team: 'X-Men' }
]
const mapResults = results =>
results.map(result => (
<div key={result.id}>
<h1>{result.character}</h1>
<p>{result.name}</p>
</div>
))
test('should render only results matching initial filter', async () => {
const options = {
keys: ['name', 'character', 'team']
}
const { container, queryByText } = render(
<ReactSearchFuse documents={documents} options={options} filter="Avengers">
{mapResults}
</ReactSearchFuse>
)
expect(queryByText('Hulk')).toBeInTheDocument()
expect(queryByText('Black Widow')).toBeInTheDocument()
expect(queryByText('Wolverine')).not.toBeInTheDocument()
expect(container.firstChild).toMatchSnapshot()
})
test('should only index specified fields', async () => {
const options = { keys: ['name'] }
const { queryByText } = render(
<ReactSearchFuse documents={documents} options={options} filter="Avengers">
{mapResults}
</ReactSearchFuse>
)
expect(queryByText('Wolverine')).not.toBeInTheDocument()
expect(queryByText('Black Widow')).not.toBeInTheDocument()
})
test('updating filter will rerender with new results', async () => {
const options = { keys: ['name', 'character', 'team'] }
const { queryByText, rerender } = render(
<ReactSearchFuse documents={documents} options={options} filter="Avengers">
{mapResults}
</ReactSearchFuse>
)
expect(queryByText('Hulk')).toBeInTheDocument()
expect(queryByText('Rogue')).not.toBeInTheDocument()
// change the filter, rerender
rerender(
<ReactSearchFuse documents={documents} options={options} filter="X">
{mapResults}
</ReactSearchFuse>
)
expect(queryByText('Hulk')).not.toBeInTheDocument()
expect(queryByText('Rogue')).toBeInTheDocument()
})