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

add some unit tests

This commit is contained in:
Matthias Kretschmann 2023-11-05 20:11:13 +00:00
parent d31feb15c3
commit 3afe980562
Signed by: m
GPG Key ID: 606EEEF3C479A91F
13 changed files with 128 additions and 8 deletions

View File

@ -15,6 +15,7 @@
"typecheck": "npm run typecheck:astro && npm run typecheck:tsc",
"prebuild": "run-p --silent --continue-on-error create:symlinks create:icons move:downloads",
"test:unit": "vitest run --config './test/vitest.config.ts' --coverage",
"test:unit:watch": "vitest watch --config './test/vitest.config.ts' --coverage",
"test:e2e": "playwright test --config './test/playwright.config.ts'",
"lint": "run-p --silent lint:js lint:css lint:md",
"lint:js": "eslint --ignore-path .gitignore './{src,test,scripts}/**/*.{ts,tsx,astro,mjs,js,cjs}'",

View File

@ -0,0 +1,10 @@
import { describe, test, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { Preview } from './Preview'
describe('Preview component', () => {
test('renders without crashing', () => {
render(<Preview />)
expect(screen.getByText('You are')).toBeInTheDocument()
})
})

View File

@ -0,0 +1,10 @@
import { describe, test, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { Success } from './Success'
describe('Success component', () => {
test('renders without crashing', () => {
render(<Success />)
expect(screen.getByText(`You're amazing, thanks!`)).toBeInTheDocument()
})
})

View File

@ -6,7 +6,6 @@ import { useNetwork } from 'wagmi'
export function Success() {
const { chain } = useNetwork()
const txHash = useStore($txHash)
console.log(chain)
const explorerName = chain?.blockExplorers?.default.name
const explorerLink = `${chain?.blockExplorers?.default.url}/tx/${txHash}`

View File

@ -0,0 +1,43 @@
import { test, expect, vi } from 'vitest'
import { prepare } from './prepare'
import * as wagmiActionsMock from '../../../../../test/__mocks__/wagmi/actions'
test('prepare with undefined params', async () => {
try {
await prepare(undefined, undefined, undefined, undefined)
expect(true).toBe(false)
} catch (e) {
expect(true).toBe(true)
}
})
test('prepare with isNative true uses correct method', async () => {
const selectedToken = {
address: '0x0',
decimals: 18
// Add other required properties here
}
const amount = '1'
const to = '0xabcdef1234567890'
const chainId = 1
const spy = vi.spyOn(wagmiActionsMock, 'prepareSendTransaction')
await prepare(selectedToken as any, amount, to, chainId)
expect(spy).toHaveBeenCalledOnce()
})
test('prepare with isNative false uses correct method', async () => {
const selectedToken = {
address: '0xabcdef1234567890',
decimals: 18
}
const amount = '1'
const to = '0xabcdef1234567890'
const chainId = 1
const spy = vi.spyOn(wagmiActionsMock, 'prepareWriteContract')
await prepare(selectedToken as any, amount, to, chainId)
expect(spy).toHaveBeenCalledOnce()
})

View File

@ -0,0 +1,30 @@
import { test, expect, vi } from 'vitest'
import { send } from './send'
import * as wagmiActionsMock from '../../../../../test/__mocks__/wagmi/actions'
test('with undefined params', async () => {
const result = await send(undefined, undefined)
expect(result).toBeUndefined()
})
test('with isNative true uses correct method', async () => {
const selectedToken = {
address: '0x0',
decimals: 18
}
const spy = vi.spyOn(wagmiActionsMock, 'sendTransaction')
await send(selectedToken as any, {} as any)
expect(spy).toHaveBeenCalledOnce()
})
test('with isNative false uses correct method', async () => {
const selectedToken = {
address: '0xabcdef1234567890',
decimals: 18
}
const spy = vi.spyOn(wagmiActionsMock, 'writeContract')
await send(selectedToken as any, {} as any)
expect(spy).toHaveBeenCalledOnce()
})

View File

@ -1,10 +1,10 @@
import type { GetToken } from '@features/Web3/stores/tokens'
import {
sendTransaction as sendNative,
writeContract,
type SendTransactionArgs,
type WriteContractPreparedArgs
} from 'wagmi/actions'
import type { GetToken } from '../useFetchTokens'
export async function send(
selectedToken: GetToken | undefined,

View File

@ -0,0 +1 @@
export * from './truncateAddress'

View File

@ -0,0 +1,11 @@
import { test, expect } from 'vitest'
import { truncateAddress } from './truncateAddress'
test('truncateAddress', () => {
const address = '0x1234567890abcdef'
const truncated = truncateAddress(address)
expect(truncated.startsWith('0x1234')).toBe(true)
expect(truncated.endsWith('cdef')).toBe(true)
expect(truncated).toBe('0x1234...cdef')
})

View File

@ -0,0 +1,15 @@
export const prepareSendTransaction = async () => {
return {}
}
export const prepareWriteContract = async () => {
return {}
}
export const sendTransaction = async () => {
return {}
}
export const writeContract = async () => {
return {}
}

View File

@ -1,6 +1,7 @@
import { vi, afterEach } from 'vitest'
import { cleanup } from '@testing-library/react'
import * as wagmiMock from './__mocks__/wagmi'
import * as wagmiActionsMock from './__mocks__/wagmi/actions'
import * as rainbowkitMock from './__mocks__/@rainbow-me/rainbowkit'
import balanceMock from './__fixtures__/balance.json'
import '@testing-library/jest-dom'
@ -19,16 +20,15 @@ Object.defineProperty(window, 'localStorage', {
})
vi.mock('wagmi', () => wagmiMock)
vi.mock('wagmi/actions', () => wagmiActionsMock)
vi.mock('@rainbow-me/rainbowkit', () => rainbowkitMock)
vi.mock('@features/Web3/hooks/useFetchTokens', () => ({
useFetchTokens: () => ({
isLoading: false,
data: balanceMock
})
useFetchTokens: () => ({ isLoading: false, data: balanceMock })
}))
// vi.mock('@features/Web3/stores/selectedToken', () => ({
// $selectedToken: balanceMock[0]
// vi.mock('@features/Web3/stores', () => ({
// $selectedToken: balanceMock[0],
// $amount: '1'
// }))
afterEach(() => {