-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathSearchHit.test.jsx
More file actions
118 lines (95 loc) · 3.55 KB
/
SearchHit.test.jsx
File metadata and controls
118 lines (95 loc) · 3.55 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
import { render, screen } from '@tests/utils/test-utils';
import userEvent from '@testing-library/user-event';
import { SearchHit } from '../../../src/components/SearchHit';
vi.mock(
'../../../src/components/ScrollTo',
() => ({
ScrollTo: ({ children }) => (<div data-testid="scrollto">{children}</div>), // eslint-disable-line react/prop-types
}),
);
/**
* Helper function to create a shallow wrapper around SearchResults
*/
const Subject = (props) => (
<SearchHit
announcer={() => {}}
annotation={{ targetId: 'x' }}
annotationId="foo"
hit={{
after: ', and start the chainsaw',
annotations: ['foo'],
before: 'Light up the',
match: 'moose',
}}
windowId="window"
selected
index={0}
windowSelected
{...props}
/>
);
describe('SearchHit', () => {
it('renders a ListItem for each hit', async () => {
const user = userEvent.setup();
const selectAnnotation = vi.fn();
render(<Subject selectAnnotation={selectAnnotation} />);
expect(screen.getByRole('listitem')).toHaveClass('windowSelected');
expect(screen.getByRole('listitem')).toHaveTextContent('1Light up the moose , and start the chai more');
await user.click(screen.getByRole('button'));
expect(selectAnnotation).toHaveBeenCalledWith('foo');
});
it('renders the annotation char if the hit is not available', () => {
render(<Subject annotation={{ chars: 'xyz' }} hit={undefined} />);
expect(screen.getByRole('listitem')).toHaveTextContent('1xyz');
});
it('renders a ScrollTo', () => {
render(<Subject containerRef="ref" />);
expect(screen.getByTestId('scrollto')).toBeInTheDocument();
});
describe('Annotation Labels', () => {
it('renders the annotationLabel if present', () => {
render(<Subject annotationLabel="The Anno Label" />);
expect(screen.getByRole('heading', { level: 4, name: 'The Anno Label' })).toBeInTheDocument();
});
it('does not render the typography if no annotation label is present', () => {
render(<Subject />);
expect(screen.getByRole('heading', { level: 4 })).toBeInTheDocument();
});
});
describe('announcer', () => {
it('sends information about the annotation when selected', () => {
const announcer = vi.fn();
const props = {
annotationLabel: 'The Annotation Label',
announcer,
canvasLabel: 'The Canvas Label',
selected: false,
total: 9,
};
const { rerender } = render(<Subject {...props} />);
expect(announcer).not.toHaveBeenCalled();
rerender(<Subject {...props} selected />);
expect(announcer).toHaveBeenCalledWith(
'1 of 9 The Canvas Label The Annotation Label Light up the moose , and start the chai',
'polite',
);
});
it('calls the announcer when initially rendered as selected', () => {
const announcer = vi.fn();
render(<Subject announcer={announcer} selected />);
expect(announcer).toHaveBeenCalled();
});
it('does not call the announcer when initially rendered as unselected', () => {
const announcer = vi.fn();
render(<Subject announcer={announcer} selected={false} />);
expect(announcer).not.toHaveBeenCalled();
});
it('does not send information about annotations that are not being deselected', () => {
const announcer = vi.fn();
const { rerender } = render(<Subject announcer={announcer} selected />);
announcer.mockClear();
rerender(<Subject announcer={announcer} selected={false} />);
expect(announcer).not.toHaveBeenCalled();
});
});
});