Skip to content

Commit

Permalink
feat: useTransactionReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed May 14, 2024
1 parent 223797a commit b1ee64c
Show file tree
Hide file tree
Showing 25 changed files with 529 additions and 30 deletions.
41 changes: 41 additions & 0 deletions packages/register-tests/vue/src/useTransactionReceipt.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { config } from '@wagmi/test'
import { useTransactionReceipt } from '@wagmi/vue'
import { zkSync } from '@wagmi/vue/chains'
import { type ZkSyncL2ToL1Log, type ZkSyncLog } from 'viem/zksync'
import { expectTypeOf, test } from 'vitest'

test('chain formatters', () => {
const result = useTransactionReceipt()

if (result.data?.value?.chainId === zkSync.id) {
expectTypeOf(result.data.value.l1BatchNumber).toEqualTypeOf<bigint | null>()
expectTypeOf(result.data.value.l1BatchTxIndex).toEqualTypeOf<
bigint | null
>()
expectTypeOf(result.data.value.logs).toEqualTypeOf<ZkSyncLog[]>()
expectTypeOf(result.data.value.l2ToL1Logs).toEqualTypeOf<
ZkSyncL2ToL1Log[]
>()
}

const result2 = useTransactionReceipt({ chainId: zkSync.id })
if (result2.data.value) {
expectTypeOf(result2.data.value.l1BatchNumber).toEqualTypeOf<
bigint | null
>()
expectTypeOf(result2.data.value.l1BatchTxIndex).toEqualTypeOf<
bigint | null
>()
expectTypeOf(result2.data.value.logs).toEqualTypeOf<ZkSyncLog[]>()
expectTypeOf(result2.data.value.l2ToL1Logs).toEqualTypeOf<
ZkSyncL2ToL1Log[]
>()
}
})

test('parameters: config', async () => {
const result = useTransactionReceipt({ config })

if (result.data && 'l1BatchNumber' in result.data)
expectTypeOf(result.data.l1BatchNumber).toEqualTypeOf<unknown>()
})
2 changes: 1 addition & 1 deletion packages/test/src/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type WaitForOptions = {

export function waitFor<ref extends Ref>(
ref: ref,
predicate: (value: ref['value']) => boolean,
predicate: (value: ref['value']) => boolean = (value) => value,
options: WaitForOptions = {},
) {
const { timeout = 10_000 } = options
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useAccountEffect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ test('behavior: connect and disconnect called once', async () => {
connect.connect({
connector: connect.connectors[0]!,
})
await waitFor(connect.isSuccess, (isSuccess) => isSuccess)
await waitFor(connect.isSuccess)
connect.connect({
connector: connect.connectors[0]!,
})

disconnect.disconnect()
await waitFor(disconnect.isSuccess, (isSuccess) => isSuccess)
await waitFor(disconnect.isSuccess)
disconnect.disconnect()

expect(onConnect).toBeCalledTimes(1)
Expand Down
10 changes: 5 additions & 5 deletions packages/vue/src/composables/useBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ beforeEach(async () => {
test('default', async () => {
const [query] = renderComposable(() => useBalance({ address }))

await waitFor(query.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(query.isSuccess)

expect(query.data.value).toMatchInlineSnapshot(`
{
Expand All @@ -35,7 +35,7 @@ test('parameters: chainId', async () => {
useBalance({ address, chainId: chain.mainnet2.id }),
)

await waitFor(query.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(query.isSuccess)

expect(query.data.value).toMatchInlineSnapshot(`
{
Expand All @@ -55,7 +55,7 @@ test('parameters: token', async () => {
}),
)

await waitFor(query.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(query.isSuccess)

expect(query.data.value).toMatchInlineSnapshot(`
{
Expand All @@ -76,7 +76,7 @@ test('parameters: unit', async () => {
}),
)

await waitFor(query.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(query.isSuccess)

expect(query.data.value).toMatchInlineSnapshot(`
{
Expand All @@ -98,7 +98,7 @@ test('behavior: address: undefined -> defined', async () => {

address.value = accounts[0]

await waitFor(query.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(query.isSuccess)

expect(query.data.value).toMatchInlineSnapshot(`
{
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/useEnsAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('default', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(deepUnref(result)).toMatchInlineSnapshot(`
{
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/useEnsAvatar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('default', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(deepUnref(result)).toMatchInlineSnapshot(`
{
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useEstimateGas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('default', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(deepUnref(result)).toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -74,7 +74,7 @@ test('behavior: address: undefined -> defined', async () => {

address.value = accounts[0]

await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(deepUnref(result)).toMatchInlineSnapshot(`
{
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useReadContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('default', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(result.isSuccess)

expect(result.data.value).toBe(4n)
expect(result.queryKey).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -44,7 +44,7 @@ test('parameters: chainId', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(result.isSuccess)

expect(result.data.value).toBe(4n)
expect(result.queryKey).toMatchInlineSnapshot(`
Expand Down
6 changes: 3 additions & 3 deletions packages/vue/src/composables/useReconnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('default', async () => {
const [reconnect] = renderComposable(() => useReconnect())

reconnect.reconnect()
await waitFor(reconnect.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(reconnect.isSuccess)

expect(reconnect.data.value).toStrictEqual([])
})
Expand All @@ -34,7 +34,7 @@ test('parameters: connectors (Connector)', async () => {
const [reconnect] = renderComposable(() => useReconnect())

reconnect.reconnect({ connectors: [connector] })
await waitFor(reconnect.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(reconnect.isSuccess)

expect(reconnect.data.value).toMatchObject(
expect.arrayContaining([
Expand All @@ -56,7 +56,7 @@ test('parameters: connectors (CreateConnectorFn)', async () => {
const [reconnect] = renderComposable(() => useReconnect())

reconnect.reconnect({ connectors: [connector] })
await waitFor(reconnect.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(reconnect.isSuccess)

expect(reconnect.data.value).toMatchObject(
expect.arrayContaining([
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/useSendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('default', async () => {
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
value: parseEther('0.01'),
})
await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(result.data.value).toMatch(transactionHashRegex)

Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useSignMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('default', async () => {
const [result] = renderComposable(() => useSignMessage())

result.signMessage({ message: 'foo bar baz' })
await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

await expect(
recoverMessageAddress({
Expand All @@ -32,7 +32,7 @@ test('behavior: local account', async () => {

const account = privateKeyToAccount(privateKey)
result.signMessage({ account, message: 'foo bar baz' })
await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

await expect(
recoverMessageAddress({
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useSignTypedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('default', async () => {
primaryType: 'Mail',
message: typedData.basic.message,
})
await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

await expect(
recoverTypedDataAddress({
Expand All @@ -43,7 +43,7 @@ test('behavior: local account', async () => {
primaryType: 'Mail',
message: typedData.basic.message,
})
await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

await expect(
recoverTypedDataAddress({
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/useSimulateContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('default', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(result.data.value).toMatchInlineSnapshot(`
{
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useSwitchAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ test('default', async () => {
expect(address1).toBeDefined()

switchAccount.switchAccount({ connector: connector2 })
await waitFor(switchAccount.isSuccess, (isSuccess) => isSuccess)
await waitFor(switchAccount.isSuccess)

const address2 = account.address.value
expect(address2).toBeDefined()
expect(address1).not.toBe(address2)

switchAccount.switchAccount({ connector: connector1 })
await waitFor(switchAccount.isSuccess, (isSuccess) => isSuccess)
await waitFor(switchAccount.isSuccess)

const address3 = account.address.value
expect(address3).toBeDefined()
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/composables/useSwitchChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ test('default', async () => {
expect(chainId1).toBeDefined()

switchChain.switchChain({ chainId: chain.mainnet2.id })
await waitFor(switchChain.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(switchChain.isSuccess)

const chainId2 = account.chainId.value
expect(chainId2).toBeDefined()
expect(chainId1).not.toBe(chainId2)

switchChain.switchChain({ chainId: chain.mainnet.id })
await waitFor(switchChain.isSuccess, (isSuccess) => Boolean(isSuccess))
await waitFor(switchChain.isSuccess)

const chainId3 = account.chainId.value
expect(chainId3).toBeDefined()
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/composables/useTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('default', async () => {
}),
)

await waitFor(result.isSuccess, (isSuccess) => isSuccess)
await waitFor(result.isSuccess)

expect(deepUnref(result)).toMatchInlineSnapshot(`
{
Expand Down
14 changes: 14 additions & 0 deletions packages/vue/src/composables/useTransactionReceipt.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expectTypeOf, test } from 'vitest'

import { useTransactionReceipt } from './useTransactionReceipt.js'

test('select data', () => {
const result = useTransactionReceipt({
query: {
select(data) {
return data?.blockNumber
},
},
})
expectTypeOf(result.data.value).toEqualTypeOf<bigint | undefined>()
})
Loading

0 comments on commit b1ee64c

Please sign in to comment.