Skip to content

Commit

Permalink
add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Nov 5, 2023
1 parent d31feb1 commit 3afe980
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}'",
Expand Down
10 changes: 10 additions & 0 deletions src/features/Web3/components/Preview/Preview.test.tsx
Original file line number Diff line number Diff line change
@@ -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()
})
})
10 changes: 10 additions & 0 deletions src/features/Web3/components/Success/Success.test.tsx
Original file line number Diff line number Diff line change
@@ -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()
})
})
1 change: 0 additions & 1 deletion src/features/Web3/components/Success/Success.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
43 changes: 43 additions & 0 deletions src/features/Web3/hooks/usePrepareSend/prepare.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
30 changes: 30 additions & 0 deletions src/features/Web3/hooks/useSend/send.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
2 changes: 1 addition & 1 deletion src/features/Web3/hooks/useSend/send.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/features/Web3/lib/truncateAddress/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './truncateAddress'
11 changes: 11 additions & 0 deletions src/features/Web3/lib/truncateAddress/truncateAddress.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
File renamed without changes.
15 changes: 15 additions & 0 deletions test/__mocks__/wagmi/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const prepareSendTransaction = async () => {
return {}
}

export const prepareWriteContract = async () => {
return {}
}

export const sendTransaction = async () => {
return {}
}

export const writeContract = async () => {
return {}
}
File renamed without changes.
12 changes: 6 additions & 6 deletions test/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(() => {
Expand Down

0 comments on commit 3afe980

Please sign in to comment.