diff --git a/package.json b/package.json index 2332ebc5..f4df32f9 100644 --- a/package.json +++ b/package.json @@ -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}'", diff --git a/src/features/Web3/components/Preview/Preview.test.tsx b/src/features/Web3/components/Preview/Preview.test.tsx new file mode 100644 index 00000000..ed547e85 --- /dev/null +++ b/src/features/Web3/components/Preview/Preview.test.tsx @@ -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() + expect(screen.getByText('You are')).toBeInTheDocument() + }) +}) diff --git a/src/features/Web3/components/Success/Success.test.tsx b/src/features/Web3/components/Success/Success.test.tsx new file mode 100644 index 00000000..de141992 --- /dev/null +++ b/src/features/Web3/components/Success/Success.test.tsx @@ -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() + expect(screen.getByText(`You're amazing, thanks!`)).toBeInTheDocument() + }) +}) diff --git a/src/features/Web3/components/Success/Success.tsx b/src/features/Web3/components/Success/Success.tsx index f91e0401..a2251027 100644 --- a/src/features/Web3/components/Success/Success.tsx +++ b/src/features/Web3/components/Success/Success.tsx @@ -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}` diff --git a/src/features/Web3/hooks/usePrepareSend/prepare.test.ts b/src/features/Web3/hooks/usePrepareSend/prepare.test.ts new file mode 100644 index 00000000..758ed402 --- /dev/null +++ b/src/features/Web3/hooks/usePrepareSend/prepare.test.ts @@ -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() +}) diff --git a/src/features/Web3/hooks/useSend/send.test.ts b/src/features/Web3/hooks/useSend/send.test.ts new file mode 100644 index 00000000..2d69ee43 --- /dev/null +++ b/src/features/Web3/hooks/useSend/send.test.ts @@ -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() +}) diff --git a/src/features/Web3/hooks/useSend/send.ts b/src/features/Web3/hooks/useSend/send.ts index 96840cd5..b08b2880 100644 --- a/src/features/Web3/hooks/useSend/send.ts +++ b/src/features/Web3/hooks/useSend/send.ts @@ -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, diff --git a/src/features/Web3/lib/truncateAddress/index.ts b/src/features/Web3/lib/truncateAddress/index.ts new file mode 100644 index 00000000..4456e3fa --- /dev/null +++ b/src/features/Web3/lib/truncateAddress/index.ts @@ -0,0 +1 @@ +export * from './truncateAddress' diff --git a/src/features/Web3/lib/truncateAddress/truncateAddress.test.ts b/src/features/Web3/lib/truncateAddress/truncateAddress.test.ts new file mode 100644 index 00000000..729e3695 --- /dev/null +++ b/src/features/Web3/lib/truncateAddress/truncateAddress.test.ts @@ -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') +}) diff --git a/src/features/Web3/lib/truncateAddress.ts b/src/features/Web3/lib/truncateAddress/truncateAddress.ts similarity index 100% rename from src/features/Web3/lib/truncateAddress.ts rename to src/features/Web3/lib/truncateAddress/truncateAddress.ts diff --git a/test/__mocks__/wagmi/actions.ts b/test/__mocks__/wagmi/actions.ts new file mode 100644 index 00000000..66bbbdc4 --- /dev/null +++ b/test/__mocks__/wagmi/actions.ts @@ -0,0 +1,15 @@ +export const prepareSendTransaction = async () => { + return {} +} + +export const prepareWriteContract = async () => { + return {} +} + +export const sendTransaction = async () => { + return {} +} + +export const writeContract = async () => { + return {} +} diff --git a/test/__mocks__/wagmi.ts b/test/__mocks__/wagmi/index.ts similarity index 100% rename from test/__mocks__/wagmi.ts rename to test/__mocks__/wagmi/index.ts diff --git a/test/vitest.setup.ts b/test/vitest.setup.ts index 78a9614e..224badc9 100644 --- a/test/vitest.setup.ts +++ b/test/vitest.setup.ts @@ -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(() => {