-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
189 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Component, ErrorInfo, PropsWithChildren } from 'react' | ||
|
||
type ErrorBoundaryProps = PropsWithChildren | ||
|
||
function ErrorBoundary(props: ErrorBoundaryProps) { | ||
return <ErrorBoundaryImpl {...{ ...props }} /> | ||
} | ||
|
||
type ErrorBoundaryImplProps = ErrorBoundaryProps | ||
type ErrorBoundaryImplState = { | ||
errorType: ErrorType | ||
} | ||
|
||
enum ErrorType { | ||
None, | ||
Other, | ||
} | ||
|
||
class ErrorBoundaryImpl extends Component<ErrorBoundaryImplProps, ErrorBoundaryImplState> { | ||
constructor(props: ErrorBoundaryImplProps) { | ||
super(props) | ||
|
||
this.state = { | ||
errorType: ErrorType.None, | ||
} | ||
} | ||
|
||
public static getDerivedStateFromError(_e: unknown): ErrorBoundaryImplState { | ||
return { | ||
errorType: ErrorType.Other, | ||
} | ||
} | ||
|
||
public async componentDidCatch(_e: unknown, _errorInfo: ErrorInfo) {} | ||
|
||
public componentDidUpdate() {} | ||
|
||
public render() { | ||
if (this.state.errorType === ErrorType.None) { | ||
return this.props.children | ||
} | ||
|
||
return <p>Error</p> | ||
} | ||
} | ||
|
||
export default ErrorBoundary |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
export const themeConstants = { | ||
toggleButtonName: 'Toggle theme', | ||
} | ||
|
||
export const transactionPageConstants = { | ||
transactionNotFound: 'Transaction not found', | ||
genericError: 'Error loading transaction', | ||
} |
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,21 @@ | ||
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' | ||
import { transactionPageConstants } from '@/features/theme/constant' | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
...vi.importActual('react-router-dom'), | ||
useParams: () => ({ transactionId: 'invalid-id' }), | ||
})) | ||
|
||
describe('transaction', () => { | ||
it.skip('should show "Transaction does not exist" for an invalid transaction ID', () => { | ||
return executeComponentTest( | ||
() => render(<TransactionPage />), | ||
async (component) => { | ||
expect(component.getByText(transactionPageConstants.transactionNotFound)).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