Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Cory authored and Will Cory committed Nov 10, 2023
1 parent 6d0be56 commit 17799b6
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 30 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@
"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/query-core": ">=5.0.0",
"@tanstack/react-query": "beta",
"@wagmi/core": "alpha",
"@wagmi/core": "2.0.0-alpha.11",
"op-viem": "1.3.0-alpha",
"typescript": ">=5.0.4",
"viem": "2.0.0-alpha.17",
Expand Down
96 changes: 80 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions src/_test/config.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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(),
},
}),
}
2 changes: 1 addition & 1 deletion src/hooks/L1/useSimulateDepositETH.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Check failure on line 12 in src/hooks/L1/useSimulateDepositETH.test.ts

View workflow job for this annotation

GitHub Actions / Typecheck

Argument of type 'OpConfig' is not assignable to parameter of type 'Config'.

const { result } = renderHook(() =>
Expand Down
42 changes: 42 additions & 0 deletions src/hooks/L1/useWriteDepositERC20.test.ts
Original file line number Diff line number Diff line change
@@ -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 })

Check failure on line 13 in src/hooks/L1/useWriteDepositERC20.test.ts

View workflow job for this annotation

GitHub Actions / Typecheck

Argument of type 'OpConfig' is not assignable to parameter of type 'Config'.

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

Check failure on line 41 in src/hooks/L1/useWriteDepositERC20.test.ts

View workflow job for this annotation

GitHub Actions / Typecheck

Argument of type 'OpConfig' is not assignable to parameter of type 'Config'.
})
3 changes: 2 additions & 1 deletion src/hooks/L1/useWriteDepositERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export function useWriteDepositERC20(
) {
const config = useOpConfig(rest)

Check failure on line 21 in src/hooks/L1/useWriteDepositERC20.ts

View workflow job for this annotation

GitHub Actions / Typecheck

Argument of type '{ config?: Config | Config | undefined; mutation?: { retryDelay?: RetryDelayValue<WriteContractErrorType> | undefined; ... 7 more ...; onSettled?: ((data: `0x${string}` | undefined, error: WriteContractErrorType | null, variables: { ...; } | ... 1 more ... | { ...; }, context: unknown) => unknown) | undefined; } | u...' is not assignable to parameter of type 'UseConfigParameters<Config>'.

Check failure on line 21 in src/hooks/L1/useWriteDepositERC20.ts

View workflow job for this annotation

GitHub Actions / Build

Argument of type '{ config?: Config | Config | undefined; mutation?: { retryDelay?: RetryDelayValue<WriteContractErrorType> | undefined; ... 7 more ...; onSettled?: ((data: `0x${string}` | undefined, error: WriteContractErrorType | null, variables: { ...; } | ... 1 more ... | { ...; }, context: unknown) => unknown) | undefined; } | u...' is not assignable to parameter of type 'UseConfigParameters<Config>'.
const l2Chain = config.l2chains[l2ChainId]
const { writeContract, writeContractAsync } = useWriteContract()
const { writeContract, writeContractAsync, ...result } = useWriteContract()

return {
...result,
writeDepositERC20: () =>
writeContract({
chainId: l2Chain.l1ChaindId,
Expand Down
42 changes: 42 additions & 0 deletions src/hooks/L1/useWriteDepositETH.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
Loading

0 comments on commit 17799b6

Please sign in to comment.