mirror of
https://github.com/kremalicious/blog.git
synced 2024-11-22 09:56:51 +01:00
refactor, more unit tests
This commit is contained in:
parent
3ca3076dd8
commit
10dc808882
19
src/features/Web3/components/Success/ExplorerLink.tsx
Normal file
19
src/features/Web3/components/Success/ExplorerLink.tsx
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { $txHash } from '@features/Web3/stores'
|
||||||
|
import { useStore } from '@nanostores/react'
|
||||||
|
|
||||||
|
export function ExplorerLink({
|
||||||
|
url,
|
||||||
|
children
|
||||||
|
}: {
|
||||||
|
url: string | undefined
|
||||||
|
children: React.ReactNode
|
||||||
|
}) {
|
||||||
|
const txHash = useStore($txHash)
|
||||||
|
const explorerLink = `${url}/tx/${txHash}`
|
||||||
|
|
||||||
|
return (
|
||||||
|
<a href={explorerLink} target="_blank" rel="noopener noreferrer">
|
||||||
|
{children}
|
||||||
|
</a>
|
||||||
|
)
|
||||||
|
}
|
@ -2,29 +2,30 @@ import { $txHash, $isInitSend } from '@features/Web3/stores'
|
|||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import styles from './Success.module.css'
|
import styles from './Success.module.css'
|
||||||
import { useNetwork } from 'wagmi'
|
import { useNetwork } from 'wagmi'
|
||||||
|
import { ExplorerLink } from './ExplorerLink'
|
||||||
|
|
||||||
|
const title = `You're amazing, thanks!`
|
||||||
|
const description = `Your transaction is on its way. You can check the status on`
|
||||||
|
|
||||||
export function Success() {
|
export function Success() {
|
||||||
const { chain } = useNetwork()
|
const { chain } = useNetwork()
|
||||||
const txHash = useStore($txHash)
|
const txHash = useStore($txHash)
|
||||||
|
|
||||||
const explorerName = chain?.blockExplorers?.default.name
|
const explorerName = chain?.blockExplorers?.default.name
|
||||||
const explorerLink = `${chain?.blockExplorers?.default.url}/tx/${txHash}`
|
const explorerUrl = chain?.blockExplorers?.default.url
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.success}>
|
<div className={styles.success}>
|
||||||
<h5 className={styles.title}>You're amazing, thanks!</h5>
|
<h5 className={styles.title}>{title}</h5>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Your transaction is on its way. You can check the status on{' '}
|
{description}{' '}
|
||||||
<a href={explorerLink} target="_blank" rel="noopener noreferrer">
|
<ExplorerLink url={explorerUrl}>{explorerName}</ExplorerLink>.
|
||||||
{explorerName}
|
|
||||||
</a>
|
|
||||||
.
|
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<a href={explorerLink} target="_blank" rel="noopener noreferrer">
|
<ExplorerLink url={explorerUrl}>
|
||||||
<code>{txHash}</code>
|
<code>{txHash}</code>
|
||||||
</a>
|
</ExplorerLink>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<footer className={styles.actions}>
|
<footer className={styles.actions}>
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
import { test, expect, vi } from 'vitest'
|
||||||
|
import { render, screen } from '@testing-library/react'
|
||||||
|
import { useFetchTokens } from './useFetchTokens'
|
||||||
|
|
||||||
|
test('useFetchTokens does not fetch anything when no chain or address are present', async () => {
|
||||||
|
vi.mock('wagmi', () => ({
|
||||||
|
useNetwork: () => ({ chain: undefined }),
|
||||||
|
useAccount: () => ({ address: undefined })
|
||||||
|
}))
|
||||||
|
|
||||||
|
function TestComponent() {
|
||||||
|
const fetchResults = useFetchTokens()
|
||||||
|
return <div>{fetchResults?.data ? 'Fetched' : 'Not fetched'}</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
render(<TestComponent />)
|
||||||
|
|
||||||
|
expect(screen.queryByText('Fetched')).toBeNull()
|
||||||
|
})
|
@ -0,0 +1,12 @@
|
|||||||
|
import { test, expect } from 'vitest'
|
||||||
|
import { isUnhelpfulErrorMessage } from './isUnhelpfulErrorMessage'
|
||||||
|
|
||||||
|
test('returns true for unhelpful error messages', () => {
|
||||||
|
const unhelpfulMessage = 'User rejected the request'
|
||||||
|
expect(isUnhelpfulErrorMessage(unhelpfulMessage)).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns false for helpful error messages', () => {
|
||||||
|
const helpfulMessage = 'Error: Transaction has been reverted by the EVM'
|
||||||
|
expect(isUnhelpfulErrorMessage(helpfulMessage)).toBe(false)
|
||||||
|
})
|
11
src/features/Web3/hooks/useSend/isUnhelpfulErrorMessage.ts
Normal file
11
src/features/Web3/hooks/useSend/isUnhelpfulErrorMessage.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const terribleErrorMessages = [
|
||||||
|
'User rejected the request',
|
||||||
|
'User denied transaction signature',
|
||||||
|
'Cannot read properties of undefined'
|
||||||
|
]
|
||||||
|
|
||||||
|
export function isUnhelpfulErrorMessage(message: string) {
|
||||||
|
return terribleErrorMessages.some((terribleMessage) =>
|
||||||
|
message.includes(terribleMessage)
|
||||||
|
)
|
||||||
|
}
|
@ -6,6 +6,7 @@ import type {
|
|||||||
WriteContractPreparedArgs
|
WriteContractPreparedArgs
|
||||||
} from 'wagmi/actions'
|
} from 'wagmi/actions'
|
||||||
import { send } from './send'
|
import { send } from './send'
|
||||||
|
import { isUnhelpfulErrorMessage } from './isUnhelpfulErrorMessage'
|
||||||
|
|
||||||
export function useSend({
|
export function useSend({
|
||||||
txConfig
|
txConfig
|
||||||
@ -30,17 +31,7 @@ export function useSend({
|
|||||||
console.error(errorMessage)
|
console.error(errorMessage)
|
||||||
|
|
||||||
// only expose useful errors in UI
|
// only expose useful errors in UI
|
||||||
const terribleErrorMessages = [
|
if (isUnhelpfulErrorMessage(errorMessage)) {
|
||||||
'User rejected the request',
|
|
||||||
'User denied transaction signature',
|
|
||||||
'Cannot read properties of undefined'
|
|
||||||
]
|
|
||||||
|
|
||||||
if (
|
|
||||||
terribleErrorMessages.some((terribleMessage) =>
|
|
||||||
errorMessage.includes(terribleMessage)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
setError(undefined)
|
setError(undefined)
|
||||||
} else {
|
} else {
|
||||||
setError((error as Error).message)
|
setError((error as Error).message)
|
||||||
|
Loading…
Reference in New Issue
Block a user