|
1 | 1 | import { assertType, describe, expect, test, vi } from 'vitest'
|
2 | 2 |
|
3 | 3 | import { anvilMainnet } from '../../test/src/anvil.js'
|
4 |
| -import { getChainId } from '../actions/public/getChainId.js' |
5 | 4 | import { localhost, mainnet } from '../chains/index.js'
|
6 | 5 | import type { EIP1193RequestFn, EIP1474Methods } from '../types/eip1193.js'
|
7 | 6 | import { getAction } from '../utils/getAction.js'
|
8 |
| -import { createClient } from './createClient.js' |
| 7 | +import { type Client, createClient } from './createClient.js' |
9 | 8 | import { publicActions } from './decorators/public.js'
|
10 | 9 | import { createTransport } from './transports/createTransport.js'
|
11 | 10 | import { custom } from './transports/custom.js'
|
@@ -577,29 +576,62 @@ describe('extends', () => {
|
577 | 576 | })
|
578 | 577 |
|
579 | 578 | 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 | + }) |
583 | 596 | .extend((client) => ({
|
584 |
| - async getChainId() { |
585 |
| - calls.push('first') |
586 |
| - return getAction(client, getChainId, 'getChainId')({}) |
587 |
| - }, |
| 597 | + getChainId: () => getChainId(client), |
| 598 | + estimateGas: () => estimateGas(client), |
588 | 599 | }))
|
589 | 600 | .extend((client) => ({
|
590 | 601 | async getChainId() {
|
591 |
| - calls.push('second') |
| 602 | + calls.push('getChainId:first') |
592 | 603 | return getAction(client, getChainId, 'getChainId')({})
|
593 | 604 | },
|
| 605 | + async estimateGas() { |
| 606 | + calls.push('estimateGas:first') |
| 607 | + return getAction(client, estimateGas, 'estimateGas')({}) |
| 608 | + }, |
594 | 609 | }))
|
595 | 610 | .extend((client) => ({
|
596 | 611 | async getChainId() {
|
597 |
| - calls.push('third') |
| 612 | + calls.push('getChainId:second') |
598 | 613 | return getAction(client, getChainId, 'getChainId')({})
|
599 | 614 | },
|
| 615 | + async estimateGas() { |
| 616 | + calls.push('estimateGas:second') |
| 617 | + return getAction(client, estimateGas, 'estimateGas')({}) |
| 618 | + }, |
600 | 619 | }))
|
601 | 620 |
|
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 | + ]) |
604 | 636 | })
|
605 | 637 | })
|
0 commit comments