Skip to content

Commit

Permalink
test: client action composition
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 30, 2024
1 parent 129e6fd commit c966474
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/clients/createClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { assertType, describe, expect, test, vi } from 'vitest'

import { anvilMainnet } from '../../test/src/anvil.js'
import { getChainId } from '../actions/public/getChainId.js'
import { localhost, mainnet } from '../chains/index.js'
import type { EIP1193RequestFn, EIP1474Methods } from '../types/eip1193.js'
import { getAction } from '../utils/getAction.js'
import { createClient } from './createClient.js'
import { publicActions } from './decorators/public.js'
import { createTransport } from './transports/createTransport.js'
Expand Down Expand Up @@ -573,4 +575,33 @@ describe('extends', () => {
expect(extended.chain.id).toEqual(client.chain.id)
})
})

test('action composition', async () => {
const calls: string[] = []
const extended = createClient({
chain: localhost,
transport: http(),
})
.extend((client) => ({
async getChainId() {
calls.push('first')
return getAction(client, getChainId, 'getChainId')({})
},
}))
.extend((client) => ({
async getChainId() {
calls.push('second')
return getAction(client, getChainId, 'getChainId')({})
},
}))
.extend((client) => ({
async getChainId() {
calls.push('third')
return getAction(client, getChainId, 'getChainId')({})
},
}))

expect(await extended.getChainId()).toBe(localhost.id)
expect(calls).toEqual(['third', 'second', 'first'])
})
})

0 comments on commit c966474

Please sign in to comment.