From c3d010135fdd3ad555cd2284f808f3e964f40029 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Tue, 27 Jul 2021 13:36:38 +0200 Subject: [PATCH] Add test for single buy --- .../src/app/core/portfolio-calculator.spec.ts | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/apps/api/src/app/core/portfolio-calculator.spec.ts b/apps/api/src/app/core/portfolio-calculator.spec.ts index 61ef36624..5e2521d9f 100644 --- a/apps/api/src/app/core/portfolio-calculator.spec.ts +++ b/apps/api/src/app/core/portfolio-calculator.spec.ts @@ -46,6 +46,8 @@ function mockGetValue(symbol: string, date: Date) { if (symbol === 'VTI') { if (dateEqual(today, date)) { return { marketPrice: 213.32 }; + } else if (dateEqual(parse('2021-07-26', 'yyyy-MM-dd', new Date()), date)) { + return { marketPrice: 227.92 }; } else { const startDate = parse('2019-02-01', 'yyyy-MM-dd', new Date()); const daysInBetween = differenceInCalendarDays(date, startDate); @@ -610,7 +612,42 @@ describe('PortfolioCalculator', () => { }); describe('get current positions', () => { - it('with just VTI', async () => { + fit('with single VTI', async () => { + const portfolioCalculator = new PortfolioCalculator( + currentRateService, + Currency.USD + ); + portfolioCalculator.setTransactionPoints(orderVTITransactionPoint); + + const spy = jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date(Date.UTC(2021, 6, 26)).getTime()); // 2021-07-26 + const currentPositions = await portfolioCalculator.getCurrentPositions( + parse('2021-07-26', 'yyyy-MM-dd', new Date()) + ); + spy.mockRestore(); + + expect(currentPositions).toEqual({ + hasErrors: false, + positions: [ + { + averagePrice: new Big('195.39'), + currency: 'USD', + firstBuyDate: '2021-01-01', + grossPerformance: new Big('32.53'), // 227.92-195.39=32.53 + grossPerformancePercentage: new Big('0.166487537745023'), // (227.92-195.39)/195.39=0.166487537745023 + investment: new Big('195.39'), + marketPrice: 227.92, + name: 'Vanguard Total Stock Market Index Fund ETF Shares', + quantity: new Big('1'), + symbol: 'VTI', + transactionCount: 1 + } + ] + }); + }); + + it('with VTI only', async () => { const portfolioCalculator = new PortfolioCalculator( currentRateService, Currency.USD @@ -1432,6 +1469,23 @@ const ordersVTI: PortfolioOrder[] = [ } ]; +const orderVTITransactionPoint: TransactionPoint[] = [ + { + date: '2021-01-01', + items: [ + { + name: 'Vanguard Total Stock Market Index Fund ETF Shares', + quantity: new Big('1'), + symbol: 'VTI', + investment: new Big('195.39'), + currency: Currency.USD, + firstBuyDate: '2021-01-01', + transactionCount: 1 + } + ] + } +]; + const ordersVTITransactionPoints: TransactionPoint[] = [ { date: '2019-02-01',