-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathsearch-suggestions.test.jsx
More file actions
149 lines (136 loc) · 5.3 KB
/
search-suggestions.test.jsx
File metadata and controls
149 lines (136 loc) · 5.3 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
147
148
149
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {screen} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import SearchSuggestions from '@salesforce/retail-react-app/app/components/search/partials/search-suggestions'
jest.mock(
'@salesforce/retail-react-app/app/components/search/partials/search-suggestions-section',
() => {
/* eslint-disable react/prop-types -- mock component; cannot reference PropTypes inside jest.mock factory */
const MockSuggestionSection = (props) => (
<div data-testid="suggestion-section">
{props.showAskAssistantBanner && props.onAskAssistantClick && (
<button
type="button"
onClick={props.onAskAssistantClick}
aria-label="Ask Shopping Agent - Discover, compare and shop smarter with your personal shopping assistant"
>
Ask Shopping Agent
</button>
)}
</div>
)
/* eslint-enable react/prop-types */
return MockSuggestionSection
}
)
test('when no suggestions: renders RecentSearches and shows Ask Shopping Agent banner when enabled with click handler', () => {
renderWithProviders(
<SearchSuggestions
recentSearches={['shoes', 'dress']}
searchSuggestions={{}}
closeAndNavigate={jest.fn()}
enableAgentFromSearchSuggestions={true}
onAskAssistantClick={jest.fn()}
/>
)
expect(screen.getByTestId('sf-suggestion-recent')).toBeInTheDocument()
expect(
screen.getByRole('button', {
name: /Ask Shopping Agent.*discover, compare,? and shop smarter/i
})
).toBeInTheDocument()
})
test('when no suggestions and enableAgentFromSearchSuggestions false: does not show Ask Shopping Agent banner', () => {
renderWithProviders(
<SearchSuggestions
recentSearches={['shoes']}
searchSuggestions={{}}
closeAndNavigate={jest.fn()}
enableAgentFromSearchSuggestions={false}
onAskAssistantClick={jest.fn()}
/>
)
expect(screen.getByTestId('sf-suggestion-recent')).toBeInTheDocument()
expect(
screen.queryByRole('button', {
name: /Ask Shopping Agent.*discover, compare,? and shop smarter/i
})
).not.toBeInTheDocument()
})
test('when no suggestions and onAskAssistantClick not provided: does not show Ask Shopping Agent banner', () => {
renderWithProviders(
<SearchSuggestions
recentSearches={['shoes']}
searchSuggestions={{}}
closeAndNavigate={jest.fn()}
enableAgentFromSearchSuggestions={true}
/>
)
expect(
screen.queryByRole('button', {
name: /Ask Shopping Agent.*discover, compare,? and shop smarter/i
})
).not.toBeInTheDocument()
})
test('enableAgentFromSearchSuggestions string "true" shows banner when no suggestions', () => {
renderWithProviders(
<SearchSuggestions
recentSearches={['shoes']}
searchSuggestions={{}}
closeAndNavigate={jest.fn()}
enableAgentFromSearchSuggestions="true"
onAskAssistantClick={jest.fn()}
/>
)
expect(
screen.getByRole('button', {
name: /Ask Shopping Agent.*discover, compare,? and shop smarter/i
})
).toBeInTheDocument()
})
test('when has suggestions: renders SuggestionSection with showAskAssistantBanner and onAskAssistantClick', () => {
const onAskAssistantClick = jest.fn()
renderWithProviders(
<SearchSuggestions
recentSearches={[]}
searchSuggestions={{
categorySuggestions: [{type: 'category', name: 'Women', link: '/women'}]
}}
closeAndNavigate={jest.fn()}
enableAgentFromSearchSuggestions={true}
onAskAssistantClick={onAskAssistantClick}
/>
)
expect(screen.getByTestId('suggestion-section')).toBeInTheDocument()
const banner = screen.getByRole('button', {
name: /Ask Shopping Agent.*discover, compare,? and shop smarter/i
})
expect(banner).toBeInTheDocument()
})
test('when has suggestions and banner enabled: clicking banner calls onAskAssistantClick', async () => {
const user = userEvent.setup()
const onAskAssistantClick = jest.fn()
renderWithProviders(
<SearchSuggestions
recentSearches={[]}
searchSuggestions={{
recentSearchSuggestions: [{type: 'recent', name: 'shoes', link: '/search?q=shoes'}]
}}
closeAndNavigate={jest.fn()}
enableAgentFromSearchSuggestions={true}
onAskAssistantClick={onAskAssistantClick}
/>
)
const banner = screen.getByRole('button', {
name: /Ask Shopping Agent.*discover, compare,? and shop smarter/i
})
await user.click(banner)
expect(onAskAssistantClick).toHaveBeenCalledTimes(1)
})