-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Users can search for a transaction by id (#4)
* feat: adding search input * fix: transaction does not exist page * chore: fixing some lint issues * test: add unit tests * fix: fix a test and enter key for searching * test: fix a test name Co-authored-by: Neil Campbell <[email protected]> --------- Co-authored-by: Neil Campbell <[email protected]>
- Loading branch information
1 parent
1d25f11
commit ee32e8b
Showing
6 changed files
with
128 additions
and
3 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,63 @@ | ||
import { Search } from './search' | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { render, waitFor } from '@/tests/testing-library' | ||
import { executeComponentTest } from '@/tests/test-component' | ||
import { useNavigate } from 'react-router-dom' | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
...vi.importActual('react-router-dom'), | ||
useNavigate: vi.fn(), | ||
})) | ||
|
||
describe('search', () => { | ||
it('should render search input and button', () => { | ||
return executeComponentTest( | ||
() => render(<Search />), | ||
async (component) => { | ||
await waitFor(() => { | ||
expect(component.getByPlaceholderText('Search')).not.toBeNull() | ||
expect(component.getByRole('button', { name: 'search' })).not.toBeNull() | ||
}) | ||
} | ||
) | ||
}) | ||
|
||
it('should call navigate when search button is clicked', () => { | ||
const mockNavigate = vi.fn() | ||
vi.mocked(useNavigate).mockReturnValue(mockNavigate) | ||
|
||
return executeComponentTest( | ||
() => render(<Search />), | ||
async (component, user) => { | ||
await waitFor(async () => { | ||
const input = component.getByPlaceholderText('Search') | ||
const button = component.getByRole('button', { name: 'search' }) | ||
|
||
await user.type(input, '123456') | ||
await user.click(button) | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith('/explore/transaction/123456') | ||
expect(input).toHaveProperty('value', '') | ||
}) | ||
} | ||
) | ||
}) | ||
|
||
it('should not call navigate when search button is clicked and search query is empty', () => { | ||
const mockNavigate = vi.fn() | ||
vi.mocked(useNavigate).mockReturnValue(mockNavigate) | ||
|
||
return executeComponentTest( | ||
() => render(<Search />), | ||
async (component, user) => { | ||
await waitFor(async () => { | ||
const button = component.getByRole('button', { name: 'search' }) | ||
|
||
await user.click(button) | ||
|
||
expect(mockNavigate).not.toHaveBeenCalled() | ||
}) | ||
} | ||
) | ||
}) | ||
}) |
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,39 @@ | ||
import { Button } from '@/features/common/components/button' | ||
import { Input } from '@/features/common/components/input' | ||
import { cn } from '@/features/common/utils' | ||
import { Urls } from '@/routes/urls' | ||
import { useCallback, useState } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
|
||
export function Search() { | ||
const [searchQuery, setSearchQuery] = useState<string>('') | ||
const navigate = useNavigate() | ||
|
||
const handleInput = useCallback((event: React.ChangeEvent<HTMLInputElement>) => { | ||
setSearchQuery(event.target.value) | ||
}, []) | ||
|
||
const doSearch = useCallback(() => { | ||
if (!searchQuery) { | ||
return | ||
} | ||
navigate(Urls.Explore.Transaction.ById.build({ transactionId: searchQuery })) | ||
setSearchQuery('') | ||
}, [navigate, searchQuery]) | ||
|
||
const handleKeyDown = useCallback( | ||
(event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === 'Enter') { | ||
doSearch() | ||
} | ||
}, | ||
[doSearch] | ||
) | ||
|
||
return ( | ||
<div className={cn('flex gap-2')}> | ||
<Input className={cn('w-96')} placeholder="Search" value={searchQuery} onChange={handleInput} onKeyDown={handleKeyDown} /> | ||
<Button onClick={doSearch}>search</Button> | ||
</div> | ||
) | ||
} |
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,19 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { render } from '@/tests/testing-library' | ||
import { executeComponentTest } from '@/tests/test-component' | ||
import { TransactionPage } from '@/features/transactions/pages/transaction-page' | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
...vi.importActual('react-router-dom'), | ||
useParams: () => ({ transactionId: 'invalid-id' }), | ||
})) | ||
describe('transaction', () => { | ||
it('should show "Transaction does not exist" for an invalid transaction ID', () => { | ||
return executeComponentTest( | ||
() => render(<TransactionPage />), | ||
async (component) => { | ||
expect(component.getByText('Transaction does not exist')).toBeTruthy() | ||
} | ||
) | ||
}) | ||
}) |
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