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

feat: display search query in the input field when present #2387

Merged
merged 1 commit into from
Oct 27, 2024
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
77 changes: 77 additions & 0 deletions src/components/App/SideBar/RegularView/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ThemeProvider } from '@mui/material'
import { render, renderHook, RenderResult } from '@testing-library/react'
import React, { ReactElement } from 'react'
import { FormProvider, useForm, useFormContext } from 'react-hook-form'
import { MemoryRouter } from 'react-router-dom'
import { ThemeProvider as StyleThemeProvider } from 'styled-components'
import { RegularView } from '..'
import { appTheme } from '../../../Providers'

const QUERY_SEARCH = 'satoshi'

jest.mock('react-hook-form', () => ({
...jest.requireActual('react-hook-form'),
useFormContext: jest.fn(),
}))

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useSearchParams: () => [new URLSearchParams({ q: QUERY_SEARCH })],
}))

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(() => ({
matches: false,
addListener: jest.fn(),
removeListener: jest.fn(),
})),
})

const renderWithProviders = (ui: ReactElement): RenderResult => {
const { result } = renderHook(() =>
useForm<{ search: string }>({
defaultValues: {
search: QUERY_SEARCH,
},
}),
)

return render(
<MemoryRouter>
<ThemeProvider theme={appTheme}>
<StyleThemeProvider theme={appTheme}>
<FormProvider {...result.current}>{ui}</FormProvider>
</StyleThemeProvider>
</ThemeProvider>
</MemoryRouter>,
)
}

describe('RegularView Component', () => {
let setValueMock: jest.Mock

beforeEach(() => {
setValueMock = jest.fn()

const useFormContextMock = useFormContext as jest.Mock

useFormContextMock.mockReturnValue({
setValue: setValueMock,
register: jest.fn(() => ({ name: 'search', value: QUERY_SEARCH })),
watch: jest.fn((field: string) => (field === 'search' ? QUERY_SEARCH : undefined)),
})
})

it('should call setValue with "search" and the correct query value on mount', () => {
renderWithProviders(<RegularView />)
expect(setValueMock).toHaveBeenCalledWith('search', QUERY_SEARCH)
})

it('should display the correct search query in the input field', () => {
const { queryByTestId } = renderWithProviders(<RegularView />)
const searchInput = queryByTestId('search_input') as HTMLInputElement

expect(searchInput.value).toBe(QUERY_SEARCH)
})
})
9 changes: 5 additions & 4 deletions src/components/App/SideBar/RegularView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clsx from 'clsx'
import React, { useEffect, useRef, useState } from 'react'
import { useFormContext } from 'react-hook-form'
import { useNavigate } from 'react-router-dom'
import { useNavigate, useSearchParams } from 'react-router-dom'
import { ClipLoader } from 'react-spinners'
import styled from 'styled-components'
import { SelectWithPopover } from '~/components/App/SideBar/Dropdown'
Expand All @@ -25,7 +25,6 @@ import { Trending } from '../Trending'

export const MENU_WIDTH = 390

// eslint-disable-next-line react/display-name
export const RegularView = () => {
const { isFetching: isLoading, setSidebarFilter } = useDataStore((s) => s)

Expand All @@ -42,10 +41,12 @@ export const RegularView = () => {
const [isScrolled, setIsScrolled] = useState(false)
const [isFilterOpen, setIsFilterOpen] = useState(false)
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)
const [searchParams] = useSearchParams()
const query = searchParams.get('q') ?? ''

useEffect(() => {
setValue('search', searchFormValue)
}, [setValue, searchFormValue])
setValue('search', query || searchFormValue)
}, [setValue, searchFormValue, query])

useEffect(() => {
const component = componentRef.current
Expand Down
1 change: 1 addition & 0 deletions src/components/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const SearchBar = ({ loading, placeholder = 'Search', onSubmit }: Props)
return (
<Input
{...register('search')}
data-testid="search_input"
disabled={loading}
id="main-search"
onKeyPress={(event) => {
Expand Down
Loading