-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #628 from systemli/Add-Filters-and-Sorting-for-Tic…
…kers ✨ Add Filters and Sorting for Tickers
- Loading branch information
Showing
17 changed files
with
551 additions
and
241 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,49 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render } from '@testing-library/react' | ||
import { MemoryRouter } from 'react-router' | ||
import { AuthProvider } from '../useAuth' | ||
import TickerList from './TickerList' | ||
import TickerListItems from './TickerListItems' | ||
|
||
describe('TickerList', function () { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
function setup() { | ||
const client = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}) | ||
return render( | ||
<QueryClientProvider client={client}> | ||
<MemoryRouter> | ||
<AuthProvider> | ||
<TickerList /> | ||
</AuthProvider> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
it('should render', async function () { | ||
vi.mock('./TickerListItems', () => { | ||
return { | ||
__esModule: true, | ||
default: vi.fn(() => <div></div>), | ||
} | ||
}) | ||
|
||
setup() | ||
|
||
expect(TickerListItems).toHaveBeenCalledTimes(4) | ||
}) | ||
}) |
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,76 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import TickerListFilter from './TickerListFilter' | ||
import { GetTickersQueryParams } from '../../api/Ticker' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
describe('TickerListFilter', async function () { | ||
it('should render', function () { | ||
const onTitleChange = vi.fn() | ||
const onDomainChange = vi.fn() | ||
const onActiveChange = vi.fn() | ||
const params = { title: '', domain: '', active: undefined } as GetTickersQueryParams | ||
|
||
render(<TickerListFilter params={params} onTitleChange={onTitleChange} onDomainChange={onDomainChange} onActiveChange={onActiveChange} />) | ||
|
||
expect(onTitleChange).not.toHaveBeenCalled() | ||
expect(onDomainChange).not.toHaveBeenCalled() | ||
expect(onActiveChange).not.toHaveBeenCalled() | ||
|
||
expect(screen.getByLabelText('Title')).toBeInTheDocument() | ||
expect(screen.getByLabelText('Domain')).toBeInTheDocument() | ||
expect(screen.getByText('All')).toBeInTheDocument() | ||
expect(screen.getByText('Active')).toBeInTheDocument() | ||
expect(screen.getByText('Inactive')).toBeInTheDocument() | ||
|
||
expect(screen.getByLabelText('Title')).toHaveValue('') | ||
expect(screen.getByLabelText('Domain')).toHaveValue('') | ||
expect(screen.getByText('All')).toHaveClass('Mui-selected') | ||
}) | ||
|
||
it('should call onTitleChange', async function () { | ||
const onTitleChange = vi.fn() | ||
const onDomainChange = vi.fn() | ||
const onActiveChange = vi.fn() | ||
const params = { title: '', domain: '', active: undefined } as GetTickersQueryParams | ||
|
||
render(<TickerListFilter params={params} onTitleChange={onTitleChange} onDomainChange={onDomainChange} onActiveChange={onActiveChange} />) | ||
|
||
await userEvent.type(screen.getByLabelText('Title'), 'foo') | ||
expect(onTitleChange).toHaveBeenCalledWith('title', 'f') | ||
expect(onTitleChange).toHaveBeenCalledWith('title', 'o') | ||
expect(onTitleChange).toHaveBeenCalledWith('title', 'o') | ||
}) | ||
|
||
it('should call onDomainChange', async function () { | ||
const onTitleChange = vi.fn() | ||
const onDomainChange = vi.fn() | ||
const onActiveChange = vi.fn() | ||
const params = { title: '', domain: '', active: undefined } as GetTickersQueryParams | ||
|
||
render(<TickerListFilter params={params} onTitleChange={onTitleChange} onDomainChange={onDomainChange} onActiveChange={onActiveChange} />) | ||
|
||
await userEvent.type(screen.getByLabelText('Domain'), 'foo') | ||
expect(onDomainChange).toHaveBeenCalledWith('domain', 'f') | ||
expect(onDomainChange).toHaveBeenCalledWith('domain', 'o') | ||
expect(onDomainChange).toHaveBeenCalledWith('domain', 'o') | ||
}) | ||
|
||
it('should call onActiveChange', async function () { | ||
const onTitleChange = vi.fn() | ||
const onDomainChange = vi.fn() | ||
const onActiveChange = vi.fn() | ||
const params = { title: '', domain: '', active: undefined } as GetTickersQueryParams | ||
|
||
render(<TickerListFilter params={params} onTitleChange={onTitleChange} onDomainChange={onDomainChange} onActiveChange={onActiveChange} />) | ||
|
||
await userEvent.click(screen.getByText('Active')) | ||
expect(onActiveChange).toHaveBeenCalledWith(expect.anything(), 'true') | ||
|
||
await userEvent.click(screen.getByText('Inactive')) | ||
expect(onActiveChange).toHaveBeenCalledWith(expect.anything(), 'false') | ||
|
||
await userEvent.click(screen.getByText('All')) | ||
expect(onActiveChange).toHaveBeenCalledWith(expect.anything(), '') | ||
expect(onActiveChange).toHaveBeenCalledTimes(3) | ||
}) | ||
}) |
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,50 @@ | ||
import { faFilter } from '@fortawesome/free-solid-svg-icons' | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' | ||
import { Box, Stack, TextField, ToggleButton, ToggleButtonGroup } from '@mui/material' | ||
import { FC } from 'react' | ||
import { GetTickersQueryParams } from '../../api/Ticker' | ||
|
||
interface Props { | ||
params: GetTickersQueryParams | ||
onTitleChange: (field: string, value: string) => void | ||
onDomainChange: (field: string, value: string) => void | ||
onActiveChange: (e: React.MouseEvent<HTMLElement, MouseEvent>, value: unknown) => void | ||
} | ||
|
||
const TickerListFilter: FC<Props> = ({ params, onTitleChange, onDomainChange, onActiveChange }) => { | ||
return ( | ||
<Stack direction="row" alignItems="center"> | ||
<Box sx={{ px: 1 }}> | ||
<FontAwesomeIcon icon={faFilter} /> | ||
</Box> | ||
<Box sx={{ px: 1 }}> | ||
<TextField | ||
label="Title" | ||
onChange={e => onTitleChange('title', e.target.value)} | ||
placeholder="Filter by title" | ||
size="small" | ||
value={params.title} | ||
variant="outlined" | ||
/> | ||
</Box> | ||
<Box sx={{ px: 1 }}> | ||
<TextField label="Domain" onChange={e => onDomainChange('domain', e.target.value)} placeholder="Filter by domain" size="small" value={params.domain} /> | ||
</Box> | ||
<Box sx={{ px: 1 }}> | ||
<ToggleButtonGroup size="small" value={params.active} exclusive onChange={onActiveChange}> | ||
<ToggleButton value="" selected={params.active === undefined}> | ||
All | ||
</ToggleButton> | ||
<ToggleButton value="true" selected={params.active === true}> | ||
Active | ||
</ToggleButton> | ||
<ToggleButton value="false" selected={params.active === false}> | ||
Inactive | ||
</ToggleButton> | ||
</ToggleButtonGroup> | ||
</Box> | ||
</Stack> | ||
) | ||
} | ||
|
||
export default TickerListFilter |
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
Oops, something went wrong.