Skip to content

Commit

Permalink
ghostfolio: add unit test when config is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo committed Nov 2, 2023
1 parent 8309751 commit 227256e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
54 changes: 46 additions & 8 deletions src/services/ghostfolio/__tests__/ghostfolio.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import 'reflect-metadata';

import * as GhostfolioApiModule from 'ghostfolio-api';
import { GhostfolioApi, ImportRequestBody } from 'ghostfolio-api/lib/types';
import { Container } from 'inversify';

import { IConfig } from '@app/config/config.service';
import { container } from '@app/container';
import { ILogger } from '@app/logger/interface';
import GhostfolioService from '@app/services/ghostfolio/ghostfolio.service';
import { TYPES } from '@app/types';
import { Config } from '@app/types/config';
Expand All @@ -30,18 +31,22 @@ export const mockGhostfolioApi = (): GhostfolioApiMock => {
};

describe('Service: Ghostfolio', () => {
let container: Container;
let ghostfolioApiMock: GhostfolioApiMock;
let loggerMock: ILogger;

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

loggerMock = {
debug: jest.fn(),
info: jest.fn(),
error: jest.fn(),
};
ghostfolioApiMock = mockGhostfolioApi();

jest
.spyOn(GhostfolioApiModule, 'default')
.mockImplementation(ghostfolioApiMock.client);

container = new Container();
container.bind<ILogger>(TYPES.LoggerService).toConstantValue(loggerMock);
container.bind<IConfig>(TYPES.ConfigService).toConstantValue({
config: {
ghostfolio: { hostname: 'hostname', secret: 'secret', port: '3333' },
Expand All @@ -50,21 +55,49 @@ describe('Service: Ghostfolio', () => {
});
});

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

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

// Act
container.resolve(GhostfolioService);

// Assert
expect(ghostfolioApiMock.client).toBeCalledWith('secret', 'hostname', 3333);
});

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(GhostfolioService);

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

it('should get orders correctly', async () => {
const service = container.resolve(GhostfolioService);
// Arrange

// Act
const service = container.resolve(GhostfolioService);
service.order();

// Assert
expect(ghostfolioApiMock.order).toBeCalledTimes(1);
});

it('should import to default account', async () => {
// Arrange
const requestBody: ImportRequestBody = {
activities: [
{
Expand All @@ -78,15 +111,18 @@ describe('Service: Ghostfolio', () => {
},
],
};
const service = container.resolve(GhostfolioService);

// Act
const service = container.resolve(GhostfolioService);
service.importData(requestBody);

// Assert
expect(ghostfolioApiMock.importData).toBeCalledTimes(1);
expect(ghostfolioApiMock.importData).toBeCalledWith(requestBody);
});

it('should import to specific account', async () => {
// Arrange
container.unbind(TYPES.ConfigService);
container.bind<IConfig>(TYPES.ConfigService).toConstantValue({
config: {
Expand All @@ -113,10 +149,12 @@ describe('Service: Ghostfolio', () => {
},
],
};
const service = container.resolve(GhostfolioService);

// Act
const service = container.resolve(GhostfolioService);
service.importData(requestBody);

// Assert
expect(ghostfolioApiMock.importData).toBeCalledTimes(1);
expect(ghostfolioApiMock.importData).toBeCalledWith({
activities: [{ ...requestBody.activities[0], accountId: 'accountId' }],
Expand Down
15 changes: 12 additions & 3 deletions src/services/ghostfolio/ghostfolio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GhostfolioApi, ImportRequestBody } from 'ghostfolio-api/lib/types';
import { inject, injectable } from 'inversify';

import { IConfig } from '@app/config/config.service';
import { ILogger } from '@app/logger/interface';
import { IGhostfolio } from '@app/services/ghostfolio/interface';
import { TYPES } from '@app/types';
import { GhostfolioConfig } from '@app/types/config';
Expand All @@ -11,9 +12,17 @@ import { GhostfolioConfig } from '@app/types/config';
class GhostfolioService implements IGhostfolio {
public client: ReturnType<GhostfolioApi>;
private config: GhostfolioConfig;

constructor(@inject(TYPES.ConfigService) configService: IConfig) {
if (!configService.config) return;
private logger: ILogger;

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;
}

this.config = configService.config.ghostfolio;
this.client = ghostfolioApi(
Expand Down

0 comments on commit 227256e

Please sign in to comment.