From c966474e8923b946bf9c2eeab5e94c8d0761e3b0 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Fri, 30 Aug 2024 19:12:55 +0100 Subject: [PATCH] test: client action composition --- src/clients/createClient.test.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/clients/createClient.test.ts b/src/clients/createClient.test.ts index 5dfcbe4c0c..274dfae2d8 100644 --- a/src/clients/createClient.test.ts +++ b/src/clients/createClient.test.ts @@ -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' @@ -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']) + }) })