2022-02-14 18:50:47 +01:00
|
|
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service';
|
|
|
|
import { parseDate } from '@ghostfolio/common/helper';
|
|
|
|
import Big from 'big.js';
|
|
|
|
|
|
|
|
import { CurrentRateServiceMock } from './current-rate.service.mock';
|
2022-04-09 10:17:31 +02:00
|
|
|
import { PortfolioCalculator } from './portfolio-calculator';
|
2022-02-14 18:50:47 +01:00
|
|
|
|
|
|
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => {
|
|
|
|
return {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
CurrentRateService: jest.fn().mockImplementation(() => {
|
|
|
|
return CurrentRateServiceMock;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-04-09 10:17:31 +02:00
|
|
|
describe('PortfolioCalculator', () => {
|
2022-02-14 18:50:47 +01:00
|
|
|
let currentRateService: CurrentRateService;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
currentRateService = new CurrentRateService(null, null, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get current positions', () => {
|
|
|
|
it.only('with BALN.SW buy', async () => {
|
2022-04-09 10:17:31 +02:00
|
|
|
const portfolioCalculator = new PortfolioCalculator({
|
2022-02-14 18:50:47 +01:00
|
|
|
currentRateService,
|
|
|
|
currency: 'CHF',
|
|
|
|
orders: [
|
|
|
|
{
|
|
|
|
currency: 'CHF',
|
|
|
|
date: '2021-11-30',
|
|
|
|
dataSource: 'YAHOO',
|
|
|
|
fee: new Big(1.55),
|
|
|
|
name: 'Bâloise Holding AG',
|
|
|
|
quantity: new Big(2),
|
|
|
|
symbol: 'BALN.SW',
|
|
|
|
type: 'BUY',
|
|
|
|
unitPrice: new Big(136.6)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2022-04-09 10:17:31 +02:00
|
|
|
portfolioCalculator.computeTransactionPoints();
|
2022-02-14 18:50:47 +01:00
|
|
|
|
|
|
|
const spy = jest
|
|
|
|
.spyOn(Date, 'now')
|
|
|
|
.mockImplementation(() => parseDate('2021-12-18').getTime());
|
|
|
|
|
2022-04-09 10:17:31 +02:00
|
|
|
const currentPositions = await portfolioCalculator.getCurrentPositions(
|
2022-02-14 18:50:47 +01:00
|
|
|
parseDate('2021-11-30')
|
|
|
|
);
|
|
|
|
|
2022-07-22 19:00:36 +02:00
|
|
|
const investments = portfolioCalculator.getInvestments();
|
|
|
|
|
|
|
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByMonth();
|
|
|
|
|
2022-02-14 18:50:47 +01:00
|
|
|
spy.mockRestore();
|
|
|
|
|
|
|
|
expect(currentPositions).toEqual({
|
|
|
|
currentValue: new Big('297.8'),
|
2022-03-05 11:00:02 +01:00
|
|
|
errors: [],
|
2022-02-14 18:50:47 +01:00
|
|
|
grossPerformance: new Big('24.6'),
|
|
|
|
grossPerformancePercentage: new Big('0.09004392386530014641'),
|
|
|
|
hasErrors: false,
|
|
|
|
netPerformance: new Big('23.05'),
|
|
|
|
netPerformancePercentage: new Big('0.08437042459736456808'),
|
|
|
|
positions: [
|
|
|
|
{
|
|
|
|
averagePrice: new Big('136.6'),
|
|
|
|
currency: 'CHF',
|
|
|
|
dataSource: 'YAHOO',
|
|
|
|
firstBuyDate: '2021-11-30',
|
|
|
|
grossPerformance: new Big('24.6'),
|
|
|
|
grossPerformancePercentage: new Big('0.09004392386530014641'),
|
|
|
|
investment: new Big('273.2'),
|
|
|
|
netPerformance: new Big('23.05'),
|
|
|
|
netPerformancePercentage: new Big('0.08437042459736456808'),
|
|
|
|
marketPrice: 148.9,
|
|
|
|
quantity: new Big('2'),
|
|
|
|
symbol: 'BALN.SW',
|
|
|
|
transactionCount: 1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
totalInvestment: new Big('273.2')
|
|
|
|
});
|
2022-07-22 19:00:36 +02:00
|
|
|
|
|
|
|
expect(investments).toEqual([
|
|
|
|
{ date: '2021-11-30', investment: new Big('273.2') }
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(investmentsByMonth).toEqual([
|
|
|
|
{ date: '2021-11-01', investment: new Big('273.2') }
|
|
|
|
]);
|
2022-02-14 18:50:47 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|