1
0
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:
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 { 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}>

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