-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathSearchPanel.test.jsx
More file actions
85 lines (66 loc) · 2.76 KB
/
SearchPanel.test.jsx
File metadata and controls
85 lines (66 loc) · 2.76 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
import { render, screen } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { t } from 'i18next';
import { SearchPanel } from '../../../src/components/SearchPanel';
/**
* Helper function to create a shallow wrapper around SearchPanel
*/
function createWrapper(props) {
return render(
<SearchPanel
id="xyz"
fetchSearch={() => {}}
searchService={{ id: 'http://example.com/search' }}
windowId="window"
{...props}
/>,
{ preloadedState: { companionWindows: { xyz: { content: 'search' } } } },
);
}
describe('SearchPanel', () => {
it('renders a CompanionWindow', () => {
createWrapper();
expect(screen.getByRole('complementary')).toBeInTheDocument();
expect(screen.getByRole('heading', { name: 'Search' })).toBeInTheDocument();
});
it('passes a Clear chip as the CompanionWindow title prop', () => {
createWrapper({ query: 'Wolpertinger' });
expect(screen.getByRole('heading', { name: /Search/ })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'clear' })).toBeInTheDocument();
});
it('the Clear chip calls the removeSearch prop', async () => {
const user = userEvent.setup();
const removeSearch = vi.fn();
createWrapper({ query: 'Wolpertinger', removeSearch });
await user.click(screen.getByRole('button', { name: 'clear' }));
expect(removeSearch).toHaveBeenCalled();
});
it('does not render a Clear chip if there is no search query to be cleared', () => {
createWrapper();
expect(screen.queryByRole('button', { name: 'clear' })).not.toBeInTheDocument();
});
it('has the SearchPanelControls component', () => {
createWrapper();
expect(screen.getByRole('combobox', { name: 'search terms' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Submit search' })).toBeInTheDocument();
});
it('has the SearchResults list', () => {
createWrapper();
expect(screen.getByRole('list')).toBeInTheDocument();
});
it('suggests searches', async () => {
const user = userEvent.setup();
const fetchSearch = vi.fn();
createWrapper({
fetchSearch, query: '', suggestedSearches: ['abc'], t,
});
expect(screen.getByRole('button', { name: 'Search this document for "abc"' })).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Search this document for "abc"' }));
expect(fetchSearch).toHaveBeenCalledWith('http://example.com/search?q=abc', 'abc');
});
it('does not suggest searches if the user has made a query', () => {
const fetchSearch = vi.fn();
createWrapper({ fetchSearch, query: 'blah', suggestedSearches: ['abc'] });
expect(screen.queryByRole('button', { name: 'Search this document for "abc"' })).not.toBeInTheDocument();
});
});