Skip to content

Commit 0ea09cc

Browse files
authored
test: demonstrate callstack in extended client action (#2680)
* test: expand action composition test * lint
1 parent f9e97fb commit 0ea09cc

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/clients/createClient.test.ts

+45-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { assertType, describe, expect, test, vi } from 'vitest'
22

33
import { anvilMainnet } from '../../test/src/anvil.js'
4-
import { getChainId } from '../actions/public/getChainId.js'
54
import { localhost, mainnet } from '../chains/index.js'
65
import type { EIP1193RequestFn, EIP1474Methods } from '../types/eip1193.js'
76
import { getAction } from '../utils/getAction.js'
8-
import { createClient } from './createClient.js'
7+
import { type Client, createClient } from './createClient.js'
98
import { publicActions } from './decorators/public.js'
109
import { createTransport } from './transports/createTransport.js'
1110
import { custom } from './transports/custom.js'
@@ -577,29 +576,62 @@ describe('extends', () => {
577576
})
578577

579578
test('action composition', async () => {
580-
const calls: string[] = []
581-
const extended = anvilMainnet
582-
.getClient()
579+
let calls: string[] = []
580+
581+
async function getChainId(_client: Client) {
582+
calls.push('getChainId:base')
583+
return 1337
584+
}
585+
586+
async function estimateGas(client: Client) {
587+
calls.push('estimateGas:base')
588+
await getAction(client, getChainId, 'getChainId')({})
589+
return 1000n
590+
}
591+
592+
const extended = createClient({
593+
chain: localhost,
594+
transport: http(),
595+
})
583596
.extend((client) => ({
584-
async getChainId() {
585-
calls.push('first')
586-
return getAction(client, getChainId, 'getChainId')({})
587-
},
597+
getChainId: () => getChainId(client),
598+
estimateGas: () => estimateGas(client),
588599
}))
589600
.extend((client) => ({
590601
async getChainId() {
591-
calls.push('second')
602+
calls.push('getChainId:first')
592603
return getAction(client, getChainId, 'getChainId')({})
593604
},
605+
async estimateGas() {
606+
calls.push('estimateGas:first')
607+
return getAction(client, estimateGas, 'estimateGas')({})
608+
},
594609
}))
595610
.extend((client) => ({
596611
async getChainId() {
597-
calls.push('third')
612+
calls.push('getChainId:second')
598613
return getAction(client, getChainId, 'getChainId')({})
599614
},
615+
async estimateGas() {
616+
calls.push('estimateGas:second')
617+
return getAction(client, estimateGas, 'estimateGas')({})
618+
},
600619
}))
601620

602-
expect(await extended.getChainId()).toBe(anvilMainnet.chain.id)
603-
expect(calls).toEqual(['third', 'second', 'first'])
621+
expect(await extended.getChainId()).toBe(1337)
622+
expect(calls).toEqual([
623+
'getChainId:second',
624+
'getChainId:first',
625+
'getChainId:base',
626+
])
627+
628+
calls = []
629+
expect(await extended.estimateGas()).toBe(1000n)
630+
expect(calls).toEqual([
631+
'estimateGas:second',
632+
'estimateGas:first',
633+
'estimateGas:base',
634+
'getChainId:base',
635+
])
604636
})
605637
})

0 commit comments

Comments
 (0)