1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-22 09:13:35 +01:00

refactor, more unit tests

This commit is contained in:
Matthias Kretschmann 2023-11-05 22:07:34 +00:00
parent 3ca3076dd8
commit 10dc808882
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 73 additions and 20 deletions

View 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>
)
}

View File

@ -2,29 +2,30 @@ import { $txHash, $isInitSend } from '@features/Web3/stores'
import { useStore } from '@nanostores/react'
import styles from './Success.module.css'
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() {
const { chain } = useNetwork()
const txHash = useStore($txHash)
const explorerName = chain?.blockExplorers?.default.name
const explorerLink = `${chain?.blockExplorers?.default.url}/tx/${txHash}`
const explorerUrl = chain?.blockExplorers?.default.url
return (
<div className={styles.success}>
<h5 className={styles.title}>You're amazing, thanks!</h5>
<h5 className={styles.title}>{title}</h5>
<p>
Your transaction is on its way. You can check the status on{' '}
<a href={explorerLink} target="_blank" rel="noopener noreferrer">
{explorerName}
</a>
.
{description}{' '}
<ExplorerLink url={explorerUrl}>{explorerName}</ExplorerLink>.
</p>
<p>
<a href={explorerLink} target="_blank" rel="noopener noreferrer">
<ExplorerLink url={explorerUrl}>
<code>{txHash}</code>
</a>
</ExplorerLink>
</p>
<footer className={styles.actions}>

View File

@ -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()
})

View File

@ -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)
})

View 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)
)
}

View File

@ -6,6 +6,7 @@ import type {
WriteContractPreparedArgs
} from 'wagmi/actions'
import { send } from './send'
import { isUnhelpfulErrorMessage } from './isUnhelpfulErrorMessage'
export function useSend({
txConfig
@ -30,17 +31,7 @@ export function useSend({
console.error(errorMessage)
// only expose useful errors in UI
const terribleErrorMessages = [
'User rejected the request',
'User denied transaction signature',
'Cannot read properties of undefined'
]
if (
terribleErrorMessages.some((terribleMessage) =>
errorMessage.includes(terribleMessage)
)
) {
if (isUnhelpfulErrorMessage(errorMessage)) {
setError(undefined)
} else {
setError((error as Error).message)