From 3c9e79dddc47763aaa1a4a5ac154fdb0f791d064 Mon Sep 17 00:00:00 2001 From: Will Cory Date: Thu, 9 Nov 2023 19:00:58 -0800 Subject: [PATCH] add tests --- package.json | 3 +- pnpm-lock.yaml | 36 +++++++++++++++++++ src/_test/config.ts | 32 ++++++++++++----- src/hooks/L1/useSimulateDepositETH.test.ts | 2 +- src/hooks/L1/useWriteDepositERC20.test.ts | 42 ++++++++++++++++++++++ src/hooks/L1/useWriteDepositETH.test.ts | 42 ++++++++++++++++++++++ 6 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 src/hooks/L1/useWriteDepositERC20.test.ts create mode 100644 src/hooks/L1/useWriteDepositETH.test.ts diff --git a/package.json b/package.json index e7e9848..ec435d2 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,8 @@ "prepare": "npx simple-git-hooks" }, "dependencies": { - "@eth-optimism/contracts-ts": "^0.15.0" + "@eth-optimism/contracts-ts": "^0.15.0", + "@testing-library/react-hooks": "^8.0.1" }, "peerDependencies": { "@tanstack/react-query": ">=5.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5bd63e8..67c6b41 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@eth-optimism/contracts-ts': specifier: ^0.15.0 version: 0.15.0(@wagmi/core@2.0.0-beta.1)(typescript@5.0.4)(wagmi@2.0.0-beta.1) + '@testing-library/react-hooks': + specifier: ^8.0.1 + version: 8.0.1(@types/react@18.2.31)(react-dom@18.2.0)(react@18.2.0) '@wagmi/core': specifier: beta version: 2.0.0-beta.1(@types/react@18.2.31)(react@18.2.0)(typescript@5.0.4)(viem@2.0.0-beta.0) @@ -3908,6 +3911,29 @@ packages: lz-string: 1.5.0 pretty-format: 27.5.1 + /@testing-library/react-hooks@8.0.1(@types/react@18.2.31)(react-dom@18.2.0)(react@18.2.0): + resolution: {integrity: sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==} + engines: {node: '>=12'} + peerDependencies: + '@types/react': ^16.9.0 || ^17.0.0 + react: ^16.9.0 || ^17.0.0 + react-dom: ^16.9.0 || ^17.0.0 + react-test-renderer: ^16.9.0 || ^17.0.0 + peerDependenciesMeta: + '@types/react': + optional: true + react-dom: + optional: true + react-test-renderer: + optional: true + dependencies: + '@babel/runtime': 7.23.2 + '@types/react': 18.2.31 + react: 18.2.0 + react-dom: 18.2.0(react@18.2.0) + react-error-boundary: 3.1.4(react@18.2.0) + dev: false + /@testing-library/react@14.0.0(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-S04gSNJbYE30TlIMLTzv6QCTzt9AqIF5y6s6SzVFILNcNvbV/jU96GeiTPillGQo+Ny64M/5PV7klNYYgv5Dfg==} engines: {node: '>=14'} @@ -8844,6 +8870,16 @@ packages: react: 18.2.0 scheduler: 0.23.0 + /react-error-boundary@3.1.4(react@18.2.0): + resolution: {integrity: sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==} + engines: {node: '>=10', npm: '>=6'} + peerDependencies: + react: '>=16.13.1' + dependencies: + '@babel/runtime': 7.23.2 + react: 18.2.0 + dev: false + /react-is@16.13.1: resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==} diff --git a/src/_test/config.ts b/src/_test/config.ts index 2097645..c85cc91 100644 --- a/src/_test/config.ts +++ b/src/_test/config.ts @@ -1,10 +1,17 @@ import { createConfig, http } from '@wagmi/core' import { mock } from '@wagmi/core/internal' -import { base as viem_base } from 'viem/chains' +import { opStackL2ChainContracts } from 'op-viem' +import { baseAddresses } from 'op-viem/chains' +import { base as viem_base, mainnet } from 'viem/chains' +import { type OpConfig } from '../types/OpConfig.js' import { accounts } from './constants.js' import { getRpcUrls } from './utils.js' const base = { + chainId: viem_base.id, + l1ChaindId: mainnet.id, + l1Addresses: baseAddresses, + l2Addresses: opStackL2ChainContracts, ...getRpcUrls({ port: 8547 }), ...viem_base, fork: { @@ -15,12 +22,19 @@ const base = { }, } -export const config = createConfig({ - chains: [base], - connectors: [mock({ accounts })], - pollingInterval: 100, - storage: null, - transports: { - [base.id]: http(), +export const config: OpConfig = { + l2chains: { + // @ts-expect-error this seems to have a mismatch that is likely a bug + [base.id]: base, }, -}) + + ...createConfig({ + chains: [base], + connectors: [mock({ accounts })], + pollingInterval: 100, + storage: null, + transports: { + [base.id]: http(), + }, + }), +} diff --git a/src/hooks/L1/useSimulateDepositETH.test.ts b/src/hooks/L1/useSimulateDepositETH.test.ts index 49d14c5..2e16ca4 100644 --- a/src/hooks/L1/useSimulateDepositETH.test.ts +++ b/src/hooks/L1/useSimulateDepositETH.test.ts @@ -8,7 +8,7 @@ import { useSimulateDepositETH } from './useSimulateDepositETH.js' const connector = config.connectors[0]! const portal = '0xe93c8cD0D409341205A592f8c4Ac1A5fe5585cfA' -test('default', async () => { +test('useSimulateDepositETH', async () => { await connect(config, { connector }) const { result } = renderHook(() => diff --git a/src/hooks/L1/useWriteDepositERC20.test.ts b/src/hooks/L1/useWriteDepositERC20.test.ts new file mode 100644 index 0000000..771af46 --- /dev/null +++ b/src/hooks/L1/useWriteDepositERC20.test.ts @@ -0,0 +1,42 @@ +import { connect, disconnect } from '@wagmi/core' +import { baseAddresses } from 'op-viem/chains' +import { base } from 'viem/chains' +import { expect, test } from 'vitest' +import { config } from '../../_test/config.js' +import { accounts } from '../../_test/constants.js' +import { renderHook, waitFor } from '../../_test/react.js' +import { useWriteDepositERC20 } from './useWriteDepositERC20.js' + +const connector = config.connectors[0]! + +test(useWriteDepositERC20.name, async () => { + await connect(config, { connector }) + + const { result } = renderHook(() => + useWriteDepositERC20({ + l2ChainId: base.id, + args: { + l1Token: '0xTokenAddressL1', + l2Token: '0xTokenAddressL2', + to: accounts[0], + amount: 100n, // Assuming amount is in BigInt format + minGasLimit: 21000, + extraData: '0x', + }, + ...baseAddresses, + }) + ) + + expect(result.current.writeDepositERC20).toBeDefined() + expect(result.current.writeDepositERC20Async).toBeDefined() + expect(result.current.data).toBeUndefined() + expect(result.current.isIdle).toBe(true) + + result.current.writeDepositERC20() + + await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) + + expect(result.current).toMatchInlineSnapshot() + + await disconnect(config, { connector }) +}) diff --git a/src/hooks/L1/useWriteDepositETH.test.ts b/src/hooks/L1/useWriteDepositETH.test.ts new file mode 100644 index 0000000..d9141f9 --- /dev/null +++ b/src/hooks/L1/useWriteDepositETH.test.ts @@ -0,0 +1,42 @@ +import { baseAddresses } from 'op-viem/chains' +import { base } from 'viem/chains' +import { expect, test } from 'vitest' +import { accounts } from '../../_test/constants.js' +import { renderHook, waitFor } from '../../_test/react.js' +import { useWriteDepositETH } from './useWriteDepositETH.js' + +test(useWriteDepositETH.name, async () => { + const { result } = renderHook(() => + useWriteDepositETH({ + l2ChainId: base.id, + args: { + to: accounts[0], + gasLimit: 21000, + data: '0x', + // typescript wasn't compiling for me for some reason + ...{ amount: 1n }, + }, + ...baseAddresses, + }) + ) + + // write contract lazily writes + // these are low value checks we are testing wagmi here + expect(result.current.writeDepositETH).toBeDefined() + expect(result.current.writeDepositETHAsync).toBeDefined() + expect(result.current.data).toBeUndefined() + expect(result.current.isIdle).toBe(true) + + // go ahead and trigger the write + result.current.writeDepositETH() + + // since we didn't use the async wait for it to succeed + await waitFor(() => { + // check for error first so if one happens we can see it in the test failure + expect(result.current.error).toBeUndefined() + expect(result.current.isSuccess).toBeTruthy() + }) + + // now assert the result is what we expect + expect(result.current).toMatchInlineSnapshot() +})