Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(retail-react-app): recent searches #969

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Search = (props) => {

// Check if term is already in the saved searches
searches = searches.filter((savedSearchTerm) => {
searchText.toLowerCase() !== savedSearchTerm.toLowerCase()
return searchText.toLowerCase() !== savedSearchTerm.toLowerCase()
})

// Create a new array consisting of the search text and up to 4 other resent searches.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
import React from 'react'
import {renderWithProviders, createPathWithDefaults} from '../../utils/test-utils'
import user from '@testing-library/user-event'
import {screen, waitFor} from '@testing-library/react'
import {screen, waitFor, within} from '@testing-library/react'
import SearchInput from './index'
import Suggestions from './partials/suggestions'
import {noop} from '../../utils/utils'
import {clearSessionJSONItem, getSessionJSONItem, setSessionJSONItem, noop} from '../../utils/utils'
import {RECENT_SEARCH_KEY, RECENT_SEARCH_LIMIT} from '../../constants'
import mockSearchResults from '../../commerce-api/mocks/searchResults'
import mockConfig from '../../../config/mocks/default'
import {rest} from 'msw'

beforeEach(() => {
clearSessionJSONItem(RECENT_SEARCH_KEY)
jest.resetModules()
global.server.use(
rest.get('*/search-suggestions', (req, res, ctx) => {
Expand All @@ -30,15 +32,6 @@ test('renders SearchInput', () => {
expect(searchInput).toBeInTheDocument()
})

test('renders Popover if recent searches populated', async () => {
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
await user.type(searchInput, 'Dresses')
expect(await screen.findByTestId('sf-suggestion')).toBeInTheDocument()
const countOfSuggestions = await screen.findAllByText('Dresses')
expect(countOfSuggestions.length).toEqual(2)
})
Comment on lines -33 to -40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this test as it seems it's sort of a duplicate of "shows previously searched items when focused" (L45).


test('changes url when enter is pressed', async () => {
renderWithProviders(<SearchInput />, {
wrapperProps: {siteAlias: 'uk', appConfig: mockConfig.app}
Expand All @@ -50,29 +43,52 @@ test('changes url when enter is pressed', async () => {
})

test('shows previously searched items when focused', async () => {
setSessionJSONItem(RECENT_SEARCH_KEY, ['Dresses', 'Suits', 'Tops'])
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
user.clear(searchInput)
await searchInput.focus()
const countOfSuggestions = await screen.findAllByText('Recent Searches')
expect(countOfSuggestions.length).toEqual(2)
const suggestionPopoverEl = await screen.findByTestId('sf-suggestion-popover')
const recentSearchesEl = await within(suggestionPopoverEl).findByTestId('sf-suggestion-recent')
expect(recentSearchesEl).toBeInTheDocument()
expect(
document.querySelectorAll('[data-testid=sf-suggestion-popover] button[name=recent-search]')
).toHaveLength(3)
})

test('saves recent searches on submit', async () => {
setSessionJSONItem(RECENT_SEARCH_KEY, ['Dresses', 'Suits', 'Tops'])
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
await user.type(searchInput, 'Gloves{enter}')
expect(getSessionJSONItem(RECENT_SEARCH_KEY)).toHaveLength(4)
})

test('limits number of saved recent searches', async () => {
setSessionJSONItem(RECENT_SEARCH_KEY, ['Dresses', 'Suits', 'Tops', 'Gloves', 'Bracelets'])
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
await user.type(searchInput, 'Ties{enter}')
expect(getSessionJSONItem(RECENT_SEARCH_KEY)).toHaveLength(RECENT_SEARCH_LIMIT)
})

test('suggestions render when there are some', async () => {
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
await user.type(searchInput, 'Dress')
const countOfSuggestions = await screen.findAllByText('Dress')
expect(countOfSuggestions.length).toEqual(2)
const suggestionPopoverEl = await screen.findByTestId('sf-suggestion-popover')
const suggestionsEl = await within(suggestionPopoverEl).findByTestId('sf-suggestion')
expect(suggestionsEl.querySelector('button').textContent).toBe('Dresses')
})

test('clicking clear searches clears searches', async () => {
test('clicking clear searches clears recent searches', async () => {
setSessionJSONItem(RECENT_SEARCH_KEY, ['Dresses', 'Suits', 'Tops'])
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
await searchInput.focus()
const clearSearch = document.getElementById('clear-search')
await user.click(clearSearch)
expect(await screen.findByTestId('sf-suggestion-popover')).toBeInTheDocument()
expect(getSessionJSONItem(RECENT_SEARCH_KEY)).not.toBeDefined()
})

test('passing undefined to Suggestions returns undefined', async () => {
Expand Down