From 6bd80bbc16ebd257e0c49ad600a3003517d5e34b Mon Sep 17 00:00:00 2001 From: Cyrille Derche Date: Wed, 13 Nov 2024 14:57:50 +0100 Subject: [PATCH] fix tests --- src/app.service.spec.ts | 3 +- src/rpc.service.spec.ts | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/rpc.service.spec.ts diff --git a/src/app.service.spec.ts b/src/app.service.spec.ts index 5c6ed72..b5c946a 100644 --- a/src/app.service.spec.ts +++ b/src/app.service.spec.ts @@ -2,13 +2,14 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AppService } from './app.service'; import { RabbitMQModule } from './rabbitmq.module'; +import { RpcModule } from './rpc.module'; describe('AppService', () => { let service: AppService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [RabbitMQModule], + imports: [RabbitMQModule, RpcModule], providers: [AppService], }).compile(); diff --git a/src/rpc.service.spec.ts b/src/rpc.service.spec.ts new file mode 100644 index 0000000..796052d --- /dev/null +++ b/src/rpc.service.spec.ts @@ -0,0 +1,71 @@ +// src/rpc.service.spec.ts +import { Test, TestingModule } from '@nestjs/testing'; +import { ConfigService } from '@nestjs/config'; +import { RpcService } from './rpc.service'; +import { mainnet, sepolia, arbitrumSepolia } from 'viem/chains'; + +describe('RpcService', () => { + let rpcService: RpcService; + // let configService: ConfigService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + RpcService, + { + provide: ConfigService, + useValue: { + get: jest.fn((key: string) => { + if (key === 'rpc.alchemyApiKey') { + return 'test-alchemy-api-key'; + } + if (key === 'rpc.infuraApiKey') { + return 'test-infura-api-key'; + } + return null; + }), + }, + }, + ], + }).compile(); + + rpcService = module.get(RpcService); + // configService = module.get(ConfigService); + }); + + describe('onModuleInit', () => { + it('should initialize transport configuration correctly', async () => { + await rpcService.onModuleInit(); + + // Check that transport is configured correctly for mainnet + const mainnetTransport = rpcService['transport'][mainnet.id]; + expect(mainnetTransport).toBeDefined(); + + // Check that transport is configured correctly for sepolia + const sepoliaTransport = rpcService['transport'][sepolia.id]; + expect(sepoliaTransport).toBeDefined(); + + // Check that transport is configured correctly for arbitrumSepolia + const arbitrumTransport = rpcService['transport'][arbitrumSepolia.id]; + expect(arbitrumTransport).toBeDefined(); + }); + }); + + describe('getTransport', () => { + beforeEach(async () => { + // Ensure transport is initialized before testing getTransport + await rpcService.onModuleInit(); + }); + + it('should return the transport for a valid chain ID', () => { + const transport = rpcService.getTransport(mainnet.id); + expect(transport).toBeDefined(); + }); + + it('should throw an error if the transport for a chain ID is not configured', () => { + expect(() => rpcService.getTransport(999)).toThrow( + 'Transport not configured for chain 999.', + ); + }); + }); +});