|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import React from 'react' |
| 8 | +import PropTypes from 'prop-types' |
| 9 | +import {screen} from '@testing-library/react' |
| 10 | +import userEvent from '@testing-library/user-event' |
| 11 | +import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils' |
| 12 | +import SearchSuggestions from '@salesforce/retail-react-app/app/components/search/partials/search-suggestions' |
| 13 | + |
| 14 | +jest.mock( |
| 15 | + '@salesforce/retail-react-app/app/components/search/partials/search-suggestions-section', |
| 16 | + () => { |
| 17 | + const MockSuggestionSection = (props) => ( |
| 18 | + <div data-testid="suggestion-section"> |
| 19 | + {props.showAskAssistantBanner && props.onAskAssistantClick && ( |
| 20 | + <button |
| 21 | + type="button" |
| 22 | + onClick={props.onAskAssistantClick} |
| 23 | + aria-label="Ask Assistant - Discover, compare and shop smarter with your personal shopping assistant" |
| 24 | + > |
| 25 | + Ask Assistant |
| 26 | + </button> |
| 27 | + )} |
| 28 | + </div> |
| 29 | + ) |
| 30 | + MockSuggestionSection.propTypes = { |
| 31 | + showAskAssistantBanner: PropTypes.bool, |
| 32 | + onAskAssistantClick: PropTypes.func |
| 33 | + } |
| 34 | + return MockSuggestionSection |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | +test('when no suggestions: renders RecentSearches and shows Ask Assistant banner when enabled with click handler', () => { |
| 39 | + renderWithProviders( |
| 40 | + <SearchSuggestions |
| 41 | + recentSearches={['shoes', 'dress']} |
| 42 | + searchSuggestions={{}} |
| 43 | + closeAndNavigate={jest.fn()} |
| 44 | + enableAgentFromSearchSuggestions={true} |
| 45 | + onAskAssistantClick={jest.fn()} |
| 46 | + /> |
| 47 | + ) |
| 48 | + |
| 49 | + expect(screen.getByTestId('sf-suggestion-recent')).toBeInTheDocument() |
| 50 | + expect( |
| 51 | + screen.getByRole('button', { |
| 52 | + name: /ask assistant.*discover, compare and shop smarter/i |
| 53 | + }) |
| 54 | + ).toBeInTheDocument() |
| 55 | +}) |
| 56 | + |
| 57 | +test('when no suggestions and enableAgentFromSearchSuggestions false: does not show Ask Assistant banner', () => { |
| 58 | + renderWithProviders( |
| 59 | + <SearchSuggestions |
| 60 | + recentSearches={['shoes']} |
| 61 | + searchSuggestions={{}} |
| 62 | + closeAndNavigate={jest.fn()} |
| 63 | + enableAgentFromSearchSuggestions={false} |
| 64 | + onAskAssistantClick={jest.fn()} |
| 65 | + /> |
| 66 | + ) |
| 67 | + |
| 68 | + expect(screen.getByTestId('sf-suggestion-recent')).toBeInTheDocument() |
| 69 | + expect( |
| 70 | + screen.queryByRole('button', { |
| 71 | + name: /ask assistant.*discover, compare and shop smarter/i |
| 72 | + }) |
| 73 | + ).not.toBeInTheDocument() |
| 74 | +}) |
| 75 | + |
| 76 | +test('when no suggestions and onAskAssistantClick not provided: does not show Ask Assistant banner', () => { |
| 77 | + renderWithProviders( |
| 78 | + <SearchSuggestions |
| 79 | + recentSearches={['shoes']} |
| 80 | + searchSuggestions={{}} |
| 81 | + closeAndNavigate={jest.fn()} |
| 82 | + enableAgentFromSearchSuggestions={true} |
| 83 | + /> |
| 84 | + ) |
| 85 | + |
| 86 | + expect( |
| 87 | + screen.queryByRole('button', { |
| 88 | + name: /ask assistant.*discover, compare and shop smarter/i |
| 89 | + }) |
| 90 | + ).not.toBeInTheDocument() |
| 91 | +}) |
| 92 | + |
| 93 | +test('enableAgentFromSearchSuggestions string "true" shows banner when no suggestions', () => { |
| 94 | + renderWithProviders( |
| 95 | + <SearchSuggestions |
| 96 | + recentSearches={['shoes']} |
| 97 | + searchSuggestions={{}} |
| 98 | + closeAndNavigate={jest.fn()} |
| 99 | + enableAgentFromSearchSuggestions="true" |
| 100 | + onAskAssistantClick={jest.fn()} |
| 101 | + /> |
| 102 | + ) |
| 103 | + |
| 104 | + expect( |
| 105 | + screen.getByRole('button', { |
| 106 | + name: /ask assistant.*discover, compare and shop smarter/i |
| 107 | + }) |
| 108 | + ).toBeInTheDocument() |
| 109 | +}) |
| 110 | + |
| 111 | +test('when has suggestions: renders SuggestionSection with showAskAssistantBanner and onAskAssistantClick', () => { |
| 112 | + const onAskAssistantClick = jest.fn() |
| 113 | + renderWithProviders( |
| 114 | + <SearchSuggestions |
| 115 | + recentSearches={[]} |
| 116 | + searchSuggestions={{ |
| 117 | + categorySuggestions: [{type: 'category', name: 'Women', link: '/women'}] |
| 118 | + }} |
| 119 | + closeAndNavigate={jest.fn()} |
| 120 | + enableAgentFromSearchSuggestions={true} |
| 121 | + onAskAssistantClick={onAskAssistantClick} |
| 122 | + /> |
| 123 | + ) |
| 124 | + |
| 125 | + expect(screen.getByTestId('suggestion-section')).toBeInTheDocument() |
| 126 | + const banner = screen.getByRole('button', { |
| 127 | + name: /ask assistant.*discover, compare and shop smarter/i |
| 128 | + }) |
| 129 | + expect(banner).toBeInTheDocument() |
| 130 | +}) |
| 131 | + |
| 132 | +test('when has suggestions and banner enabled: clicking banner calls onAskAssistantClick', async () => { |
| 133 | + const user = userEvent.setup() |
| 134 | + const onAskAssistantClick = jest.fn() |
| 135 | + renderWithProviders( |
| 136 | + <SearchSuggestions |
| 137 | + recentSearches={[]} |
| 138 | + searchSuggestions={{ |
| 139 | + recentSearchSuggestions: [{type: 'recent', name: 'shoes', link: '/search?q=shoes'}] |
| 140 | + }} |
| 141 | + closeAndNavigate={jest.fn()} |
| 142 | + enableAgentFromSearchSuggestions={true} |
| 143 | + onAskAssistantClick={onAskAssistantClick} |
| 144 | + /> |
| 145 | + ) |
| 146 | + |
| 147 | + const banner = screen.getByRole('button', { |
| 148 | + name: /ask assistant.*discover, compare and shop smarter/i |
| 149 | + }) |
| 150 | + await user.click(banner) |
| 151 | + expect(onAskAssistantClick).toHaveBeenCalledTimes(1) |
| 152 | +}) |
0 commit comments