|
| 1 | +import { expectTypeOf, test } from 'vitest' |
| 2 | +import type { Chain } from '../../index.js' |
| 3 | +import { defineChain, extendSchema } from './defineChain.js' |
| 4 | + |
| 5 | +test('default', () => { |
| 6 | + const chain = defineChain({ |
| 7 | + id: 1, |
| 8 | + name: 'Test', |
| 9 | + nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, |
| 10 | + rpcUrls: { default: { http: ['https://localhost:8545'] } }, |
| 11 | + }) |
| 12 | + |
| 13 | + expectTypeOf(chain).toExtend<Chain>() |
| 14 | + expectTypeOf(chain.id).toEqualTypeOf<1>() |
| 15 | + expectTypeOf(chain.name).toEqualTypeOf<'Test'>() |
| 16 | +}) |
| 17 | + |
| 18 | +test('behavior: extend', () => { |
| 19 | + const chain = defineChain({ |
| 20 | + id: 1, |
| 21 | + name: 'Test', |
| 22 | + nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, |
| 23 | + rpcUrls: { default: { http: ['https://localhost:8545'] } }, |
| 24 | + }) |
| 25 | + |
| 26 | + const extended = chain.extend({ |
| 27 | + foo: 'bar' as const, |
| 28 | + }) |
| 29 | + |
| 30 | + expectTypeOf(extended).toExtend<Chain>() |
| 31 | + expectTypeOf(extended.foo).toEqualTypeOf<'bar'>() |
| 32 | + expectTypeOf(extended.id).toEqualTypeOf<1>() |
| 33 | +}) |
| 34 | + |
| 35 | +test('behavior: schema', () => { |
| 36 | + const chain = defineChain({ |
| 37 | + extendSchema: extendSchema<{ foo: string }>(), |
| 38 | + id: 1, |
| 39 | + name: 'Test', |
| 40 | + nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, |
| 41 | + rpcUrls: { default: { http: ['https://localhost:8545'] } }, |
| 42 | + }) |
| 43 | + |
| 44 | + const extended = chain.extend({ |
| 45 | + foo: 'bar', |
| 46 | + }) |
| 47 | + |
| 48 | + expectTypeOf(extended).toExtend<Chain>() |
| 49 | + expectTypeOf(extended.foo).toEqualTypeOf<'bar'>() |
| 50 | + expectTypeOf(extended.id).toEqualTypeOf<1>() |
| 51 | + |
| 52 | + // @ts-expect-error - missing required property |
| 53 | + chain.extend({}) |
| 54 | + |
| 55 | + // @ts-expect-error - wrong type |
| 56 | + chain.extend({ foo: 123 }) |
| 57 | +}) |
0 commit comments