Skip to content

Commit

Permalink
nexo: add unit test when config is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo committed Nov 2, 2023
1 parent 5d2fc7c commit 8309751
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
60 changes: 55 additions & 5 deletions src/services/exchange/__tests__/nexo.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'reflect-metadata';

import { Container } from 'inversify';
import * as NexoProModule from 'nexo-pro';
import { NexoProClient } from 'nexo-pro/lib/types/client';

import { IConfig } from '@app/config/config.service';
import { container } from '@app/container';
import { ILogger } from '@app/logger/interface';
import NexoService from '@app/services/exchange/nexo.service';
import { TYPES } from '@app/types';
import { Config } from '@app/types/config';
Expand All @@ -31,16 +32,20 @@ export const mockNexoPro = () => {
};

describe('Service: Nexo', () => {
let container: Container;
let nexoProMock: ReturnType<typeof mockNexoPro>;
let loggerMock: ILogger;

beforeEach(() => {
jest.clearAllMocks();

loggerMock = {
debug: jest.fn(),
info: jest.fn(),
error: jest.fn(),
};
nexoProMock = mockNexoPro();

jest.spyOn(NexoProModule, 'default').mockImplementation(nexoProMock.client);

container = new Container();
container.bind<ILogger>(TYPES.LoggerService).toConstantValue(loggerMock);
container.bind<IConfig>(TYPES.ConfigService).toConstantValue({
config: {
nexo: { key: 'key', secret: 'secret' },
Expand All @@ -49,76 +54,118 @@ describe('Service: Nexo', () => {
});
});

afterEach(() => {
jest.clearAllMocks();
container.unbindAll();
});

it('should initialize client with correct params', async () => {
// Arrange

// Act
container.resolve(NexoService);

// Assert
expect(nexoProMock.client).toBeCalledWith({
api_key: 'key',
api_secret: 'secret',
});
});

it('should display an error if config is invalid', async () => {
// Arrange
container.unbind(TYPES.ConfigService);
container
.bind<IConfig>(TYPES.ConfigService)
.toConstantValue({ config: undefined, saveConfig: jest.fn() });

// Act
container.resolve(NexoService);

// Assert
expect(nexoProMock.client).not.toBeCalled();
expect(loggerMock.error).toBeCalledWith('unable to get config');
});

it('should get a price quote correctly', async () => {
// Arrange
nexoProMock.getQuote.mockResolvedValue({
price: 42,
});

// Act
const service = container.resolve(NexoService);

// Assert
expect(
await service.getQuote({ pair: 'BTC/USD', amount: 10 })
).toStrictEqual({ price: 42 });
});

it('should return undefined on quote error', async () => {
// Arrange
nexoProMock.getQuote.mockRejectedValue(new Error());

// Act
const service = container.resolve(NexoService);

// Assert
expect(
await service.getQuote({ pair: 'BTC/USD', amount: 10 })
).toStrictEqual(undefined);
});

it('should get all orders correctly when no order', async () => {
// Arrange
nexoProMock.getOrders.mockResolvedValue({ orders: [] });

// Act
const service = container.resolve(NexoService);

// Assert
expect(await service.getOrders({ pair: 'BTC/USD' })).toStrictEqual({
orders: [],
});
});

it('should get all orders correctly when multiple orders', async () => {
// Arrange
nexoProMock.getOrders.mockResolvedValue({
orders: [{ id: 'id1' }, { id: 'id2' }],
});

// Act
const service = container.resolve(NexoService);

// Assert
expect(await service.getOrders({ pair: 'BTC/USD' })).toStrictEqual({
orders: [{ id: 'id1' }, { id: 'id2' }],
});
});

it('should return undefined on get orders error', async () => {
// Arrange
nexoProMock.getOrders.mockRejectedValue(new Error());

// Act
const service = container.resolve(NexoService);

// Assert
expect(await service.getOrders({ pair: 'BTC/USD' })).toStrictEqual(
undefined
);
});

it('should get all orders correctly when multiple orders', async () => {
// Arrange
nexoProMock.placeOrder.mockResolvedValue({
orderId: 'id',
});

// Act
const service = container.resolve(NexoService);

// Assert
expect(
await service.placeOrder({ pair: 'BTC/USD', quantity: 10 })
).toStrictEqual({
Expand All @@ -127,10 +174,13 @@ describe('Service: Nexo', () => {
});

it('should return undefined on place order error', async () => {
// Arrange
nexoProMock.placeOrder.mockRejectedValue(new Error());

// Act
const service = container.resolve(NexoService);

// Assert
expect(
await service.placeOrder({ pair: 'BTC/USD', quantity: 10 })
).toStrictEqual(undefined);
Expand Down
13 changes: 11 additions & 2 deletions src/services/exchange/nexo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Client from 'nexo-pro';
import { NexoProClient } from 'nexo-pro/lib/types/client';

import { IConfig } from '@app/config/config.service';
import { ILogger } from '@app/logger/interface';
import {
GetOrdersParams,
GetOrdersResponse,
Expand All @@ -17,9 +18,17 @@ import { TYPES } from '@app/types';
@injectable()
class NexoService implements IExchange {
public client: NexoProClient;
private logger: ILogger;

constructor(@inject(TYPES.ConfigService) configService: IConfig) {
if (!configService.config) return;
constructor(
@inject(TYPES.ConfigService) configService: IConfig,
@inject(TYPES.LoggerService) logger: ILogger
) {
this.logger = logger;
if (!configService.config) {
this.logger.error('unable to get config');
return;
}

const { nexo } = configService.config;
this.client = Client({ api_key: nexo.key, api_secret: nexo.secret });
Expand Down

0 comments on commit 8309751

Please sign in to comment.