-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathSearchResults.test.jsx
More file actions
146 lines (129 loc) · 4.07 KB
/
SearchResults.test.jsx
File metadata and controls
146 lines (129 loc) · 4.07 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { render, screen } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { SearchResults } from '../../../src/components/SearchResults';
/**
* Helper function to create a shallow wrapper around SearchResults
*/
function createWrapper(props) {
return render(
<SearchResults
companionWindowId="cwid"
windowId="window"
query="query"
selectedContentSearchAnnotation={['foo']}
searchHits={[
{
after: ', and start the chainsaw',
annotations: ['foo'],
before: 'Light up the',
match: 'moose',
},
]}
{...props}
/>,
{
preloadedState: {
companionWindows: {
cwid: {},
},
searches: {
window: {
cwid: {
data: {
'http://example.com/contentsearch': {
id: 'http://example.com/contentsearch',
isFetching: false,
json: {
resources: [
{
'@id': 'foo',
'@type': 'oa:Annotation',
motivation: 'sc:painting',
},
{
'@id': 'x',
label: 'The Anno Label',
},
{
'@id': 'y',
label: 'Annother Anno Label',
},
],
},
},
},
query: '',
selectedContentSearchAnnotation: null,
selectedContentSearchAnnotationIds: [],
},
},
},
windows: {
window: {},
},
},
},
);
}
describe('SearchResults', () => {
it('renders a SearchHit for each hit', () => {
createWrapper({});
expect(screen.getByRole('button', { name: /Light up the moose/ })).toBeInTheDocument();
});
it('can focus on a single item', async () => {
const user = userEvent.setup();
createWrapper({});
await user.click(screen.getByRole('button', { name: 'more...' }));
expect(screen.getByRole('listitem')).toHaveTextContent(/start the chainsaw/);
expect(screen.queryByRole('button', { name: 'more...' })).not.toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Back to results' }));
expect(screen.getByRole('button', { name: 'more...' })).toBeInTheDocument();
});
describe('annotation-only search results', () => {
it('renders a SearchHit for each annotation', () => {
createWrapper({
searchAnnotations: [{ id: 'x' }, { id: 'y' }],
searchHits: [],
});
expect(screen.getByRole('heading', { level: 4, name: 'The Anno Label' })).toBeInTheDocument();
expect(screen.getByRole('heading', { level: 4, name: 'Annother Anno Label' })).toBeInTheDocument();
});
});
describe('no search results', () => {
it('shows no results', () => {
createWrapper({
isFetching: false,
query: 'nope',
searchHits: [],
});
expect(screen.getByText('No results found')).toHaveClass('MuiTypography-body1');
});
it('while fetching', () => {
createWrapper({
isFetching: true,
query: 'nope',
searchHits: [],
});
expect(screen.queryByText('No results found')).not.toBeInTheDocument();
});
it('without a query', () => {
createWrapper({
isFetching: false,
query: '',
});
expect(screen.queryByText('No results found')).not.toBeInTheDocument();
});
});
describe('multi-page search results', () => {
it('shows a button to request the next page', async () => {
const user = userEvent.setup();
const fetchSearch = vi.fn();
createWrapper({
fetchSearch,
nextSearch: 'search?page=2',
});
await user.click(screen.getByRole('button', { name: /More results/ }));
expect(fetchSearch).toHaveBeenCalledWith('window', 'cwid', 'search?page=2', 'query');
});
});
});