-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display search query in the input field if present on mount
- Loading branch information
1 parent
5187d12
commit fa25e78
Showing
3 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/components/App/SideBar/RegularView/__tests__/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters