Skip to content

Commit

Permalink
chore: update unit test for metho net_version
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-JohnnyNguyen committed May 2, 2024
1 parent cf364c0 commit 4e55ad3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 50 deletions.
116 changes: 67 additions & 49 deletions src/communicates/__tests__/communicate.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
import { CacheModule, INestApplication, Logger } from '@nestjs/common';
import { Socket, io } from 'socket.io-client';
import { CommunicateGateway } from '../communicate.gateway';
import { CommunicateService } from '../communicate.service';
import { ConfigModule } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import configuration from '../../config/configuration';
import { DatastoreService } from 'src/repositories';
import { VerseService, AllowCheckService, TransactionService, ProxyService, TypeCheckService, RateLimitService } from 'src/services';
import { HttpModule } from '@nestjs/axios';

async function createNestApp(...gateways: any): Promise<INestApplication> {
const testingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({ load: [configuration] }),
HttpModule,
CacheModule.register(),
],
providers: gateways,
}).compile();
testingModule.useLogger(new Logger());
return testingModule.createNestApplication();
}

describe('Communicate gateway', () => {
let gateway: CommunicateGateway;
let app: INestApplication;
let ioClient: Socket;

beforeAll(async () => {
app = await createNestApp(
CommunicateGateway,
CommunicateService,

Check warning on line 33 in src/communicates/__tests__/communicate.gateway.spec.ts

View workflow job for this annotation

GitHub Actions / check code

'gateway' is assigned a value but never used
VerseService,
AllowCheckService,
TransactionService,
ProxyService,
TypeCheckService,
DatastoreService,
RateLimitService,
);
gateway = app.get<CommunicateGateway>(CommunicateGateway);
app.listen(3001);
});

beforeEach(async () => {
ioClient = io('http://localhost:3001', {
autoConnect: false,
transports: ['websocket', 'pooling'],
});
});

afterAll(async () => {
await app.close();
});

it(`Should emit "pong" on "ping"`, async () => {
ioClient.connect();
ioClient.emit('ping', 'Hello World!');
Expand Down Expand Up @@ -53,55 +97,29 @@ describe('Communicate gateway', () => {
ioClient.disconnect();
});

// it('executed method net_version', async () => {
// const body = {
// jsonrpc: '2.0',
// method: 'net_version',
// params: [],
// id: 1,
// };

// ioClient.connect();
// ioClient.emit('execute', body);
// await new Promise<void>((resolve) => {
// ioClient.on('connect', () => {
// console.log('Connected');
// });
// ioClient.on('executed', (data) => {
// expect(data.method).toBe('bnb_chainId');
// expect(data.response.error.message).toBe(
// 'Method bnb_chainId is not allowed',
// );
// resolve();
// });
// });

// ioClient.disconnect();
// // const res = {
// // send: () => {
// // return;
// // },
// // status: (code: number) => res,
// // } as Response;
it('executed method net_version', async () => {
const body = {
jsonrpc: '2.0',
method: 'net_version',
params: [],
id: 1,
};

// // jest
// // .spyOn(typeCheckService, 'isJsonrpcArrayRequestBody')
// // .mockReturnValue(false);
// // jest.spyOn(typeCheckService, 'isJsonrpcRequestBody').mockReturnValue(true);
// // const handleBatchRequestMock = jest.spyOn(
// // proxyService,
// // 'handleBatchRequest',
// // );
// // const handleSingleRequestMock = jest.spyOn(
// // proxyService,
// // 'handleSingleRequest',
// // );
ioClient.connect();
ioClient.emit('execute', body);
await new Promise<void>((resolve) => {
ioClient.on('connect', () => {
console.log('Connected');
});
ioClient.on('executed', (data) => {
expect(data.method).toBe('net_version');
expect(data.response.result).toBe(
'12345',
);
resolve();
});
});

// // const controller = moduleRef.get<ProxyController>(ProxyController);
// // expect(async () =>
// // controller.handler(isUseReadNode, requestContext, body, res),
// // ).not.toThrow();
// // expect(handleBatchRequestMock).not.toHaveBeenCalled();
// // expect(handleSingleRequestMock).toHaveBeenCalled();
// });
ioClient.disconnect();
});
});
1 change: 0 additions & 1 deletion src/communicates/communicate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export class CommunicateService {
message: err.message,
},
};
console.error(err.message);
return {
status,
data,
Expand Down

0 comments on commit 4e55ad3

Please sign in to comment.