-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputSuggestions.test.tsx
179 lines (137 loc) · 4.98 KB
/
InputSuggestions.test.tsx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import InputSuggestions from '../InputSuggestions'
import * as elementText from '../elementText'
const suggestions = ['reddit', 'facebook', 'twitter'].map(word => (
<a key={word} href={`https://${word}.com`}>
{word}
</a>
))
describe('InputSuggestions', () => {
const mockGetElementText = jest.spyOn(elementText, 'getElementText')
const mockWrapElementText = jest.spyOn(elementText, 'wrapElementText')
beforeEach(jest.clearAllMocks)
describe('renders correctly with default options', () => {
beforeEach(() => {
render(<InputSuggestions suggestions={suggestions} />)
})
it('has the correct type', () => {
expect(screen.getByRole('searchbox')).toHaveAttribute('type', 'search')
})
it('sets spellCheck off', () => {
expect(screen.getByRole('searchbox')).toHaveAttribute(
'spellcheck',
'false'
)
})
it('sets autoCapitalize off', () => {
expect(screen.getByRole('searchbox')).toHaveAttribute(
'autoCapitalize',
'off'
)
})
it('sets autoComplete off', () => {
expect(screen.getByRole('searchbox')).toHaveAttribute(
'autoComplete',
'off'
)
})
it('sets the name attribute', () => {
expect(screen.getByRole('searchbox')).toHaveAttribute('name', 'q')
})
it('sets the placeholder attribute', () => {
expect(screen.getByRole('searchbox')).toHaveAttribute(
'placeholder',
'Search'
)
})
it('does not set autoFocus', () => {
expect(screen.getByRole('searchbox')).not.toHaveFocus()
})
it('does not show suggestions if no input has been entered', () => {
expect(screen.queryByRole('list')).not.toBeInTheDocument()
})
it('does not wrap search suggestions', () => {
userEvent.type(screen.getByRole('searchbox'), 't')
expect(mockWrapElementText).not.toHaveBeenCalled()
})
})
describe('renders correctly with custom options', () => {
it('has the correct type', () => {
render(<InputSuggestions suggestions={suggestions} type="text" />)
expect(screen.getByRole('textbox')).toHaveAttribute('type', 'text')
})
it('sets the name attribute', () => {
render(<InputSuggestions suggestions={suggestions} name="search" />)
expect(screen.getByRole('searchbox')).toHaveAttribute('name', 'search')
})
it('sets the placeholder attribute', () => {
render(
<InputSuggestions
suggestions={suggestions}
placeholder="Enter keywords"
/>
)
expect(screen.getByRole('searchbox')).toHaveAttribute(
'placeholder',
'Enter keywords'
)
})
it('sets autoFocus', () => {
render(<InputSuggestions suggestions={suggestions} autoFocus />)
expect(screen.getByRole('searchbox')).toHaveFocus()
})
it('sets an ID', () => {
const { container } = render(
<InputSuggestions suggestions={suggestions} id="react-search" />
)
// eslint-disable-next-line testing-library/no-node-access
expect(container.firstChild).toHaveAttribute('id', 'react-search')
})
it('sets a className', () => {
const { container } = render(
<InputSuggestions suggestions={suggestions} className="react-search" />
)
// eslint-disable-next-line testing-library/no-node-access
expect(container.firstChild).toHaveClass('react-search')
})
it('calls wrapElementText when highlightKeywords provided', async () => {
render(<InputSuggestions suggestions={suggestions} highlightKeywords />)
userEvent.type(screen.getByRole('searchbox'), 't')
await waitFor(() => expect(mockWrapElementText).toHaveBeenCalledTimes(2))
})
})
it('fires an onChange event if provided', async () => {
const mockOnChange = jest.fn()
render(
<InputSuggestions suggestions={suggestions} onChange={mockOnChange} />
)
expect(mockOnChange).not.toHaveBeenCalled()
userEvent.type(screen.getByRole('searchbox'), 't')
await waitFor(() => expect(mockOnChange).toHaveBeenCalled())
await waitFor(() => expect(mockGetElementText).toHaveBeenCalled())
})
it('shows filtered search suggestions based on input entered', async () => {
render(<InputSuggestions suggestions={suggestions} />)
expect(screen.queryByRole('list')).not.toBeInTheDocument()
userEvent.type(screen.getByRole('searchbox'), 't')
expect(await screen.findByRole('list')).toBeInTheDocument()
await waitFor(() =>
expect(screen.getByRole('link', { name: 'reddit' })).toHaveAttribute(
'href',
'https://reddit.com'
)
)
await waitFor(() =>
expect(screen.getByRole('link', { name: 'twitter' })).toHaveAttribute(
'href',
'https://twitter.com'
)
)
await waitFor(() =>
expect(
screen.queryByRole('link', { name: 'facebook' })
).not.toBeInTheDocument()
)
})
})