-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(SearchInput): added test & updated jest config
- Loading branch information
Showing
10 changed files
with
160 additions
and
15 deletions.
There are no files selected for viewing
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
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,106 @@ | ||
import { useState } from 'react' | ||
import { fireEvent, screen, waitFor } from '@testing-library/react' | ||
import { SearchInput } from '.' | ||
import { render } from '../../../test/test-utils' | ||
|
||
jest.useFakeTimers() | ||
|
||
describe('SearchInput', () => { | ||
const debounceTimeout = 300 | ||
const onSearch = jest.fn() | ||
const onChange = jest.fn() | ||
|
||
const ControlledSearchInput = () => { | ||
const [value, setValue] = useState('') | ||
return ( | ||
<SearchInput | ||
debounceTimeout={debounceTimeout} | ||
onSearch={onSearch} | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
placeholder="Search" | ||
/> | ||
) | ||
} | ||
const UncontrolledSearchInput = () => ( | ||
<SearchInput onSearch={onSearch} onChange={onChange} placeholder="Search" /> | ||
) | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('renders the Search Input component', () => { | ||
render(<SearchInput />) | ||
}) | ||
|
||
test('should call search function after specified debounce timeout', async () => { | ||
render(<UncontrolledSearchInput />) | ||
const input = screen.getByPlaceholderText('Search') | ||
fireEvent.change(input, { target: { value: 'te' } }) | ||
fireEvent.change(input, { target: { value: 'testing...' } }) | ||
expect(onSearch).not.toHaveBeenCalled() | ||
expect(onChange).toHaveBeenCalledTimes(2) | ||
|
||
jest.advanceTimersByTime(debounceTimeout) | ||
await waitFor(() => expect(onSearch).toHaveBeenCalledWith('testing...')) | ||
expect(onSearch).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('should call onChange handler when input value changes', () => { | ||
render(<UncontrolledSearchInput />) | ||
|
||
const input = screen.getByPlaceholderText('Search') | ||
fireEvent.change(input, { target: { value: 't' } }) | ||
expect(onChange).toHaveBeenCalledTimes(1) | ||
fireEvent.change(input, { target: { value: 'te' } }) | ||
expect(onChange).toHaveBeenCalledTimes(2) | ||
fireEvent.change(input, { target: { value: 'test' } }) | ||
expect(onChange).toHaveBeenCalledTimes(3) | ||
}) | ||
|
||
test('should call debounced search function with new value after reset', async () => { | ||
render(<ControlledSearchInput />) | ||
|
||
const input = screen.getByPlaceholderText('Search') | ||
fireEvent.change(input, { target: { value: 'test' } }) | ||
|
||
jest.advanceTimersByTime(debounceTimeout) | ||
await waitFor(() => expect(onSearch).toHaveBeenCalledWith('test')) | ||
|
||
fireEvent.change(input, { target: { value: '' } }) | ||
|
||
jest.advanceTimersByTime(debounceTimeout) | ||
await waitFor(() => expect(onSearch).toHaveBeenCalledWith('')) | ||
}) | ||
|
||
test('should not call search function on initial render when value is provided', () => { | ||
render(<ControlledSearchInput />) | ||
|
||
jest.advanceTimersByTime(debounceTimeout) | ||
expect(onChange).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should call search function when controlled value changes', async () => { | ||
const { rerender } = render( | ||
<SearchInput | ||
debounceTimeout={debounceTimeout} | ||
onSearch={onSearch} | ||
value="initial" | ||
onChange={onChange} | ||
/> | ||
) | ||
|
||
rerender( | ||
<SearchInput | ||
debounceTimeout={debounceTimeout} | ||
onSearch={onSearch} | ||
value="updated" | ||
onChange={onChange} | ||
/> | ||
) | ||
|
||
jest.advanceTimersByTime(debounceTimeout) | ||
await waitFor(() => expect(onSearch).toHaveBeenCalledWith('updated')) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { type Palette } from '@mui/material' | ||
const createPalette: (palette: Palette) => Palette = (palette) => palette | ||
export default createPalette |
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,11 @@ | ||
import { type Palette } from '@mui/material' | ||
import { type TypographyOptions } from '@mui/material/styles/createTypography' | ||
|
||
const createTypography: ( | ||
palette: Palette, | ||
typography: TypographyOptions | ||
) => TypographyOptions = (palette, typography) => ({ | ||
...palette, | ||
...typography, | ||
}) | ||
export default createTypography |
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,3 @@ | ||
module.exports = { | ||
default: 'default-mocking-for-file', | ||
} |
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 @@ | ||
module.exports = {} |
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,9 @@ | ||
window.matchMedia = | ||
window.matchMedia || | ||
function () { | ||
return { | ||
matches: false, | ||
addListener: function () {}, | ||
removeListener: function () {}, | ||
} | ||
} |
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,12 @@ | ||
import React, { type ReactElement } from 'react' | ||
import { SharedThemeProvider } from '../components' | ||
import { render as rtlRender, type RenderOptions } from '@testing-library/react' | ||
|
||
const AllTheProviders = ({ children }: { children: React.ReactNode }) => { | ||
return <SharedThemeProvider>{children}</SharedThemeProvider> | ||
} | ||
|
||
export const render = ( | ||
ui: ReactElement, | ||
options?: Omit<RenderOptions, 'wrapper'> | ||
) => rtlRender(ui, { wrapper: AllTheProviders, ...options }) |