feat: replace account page accordion with tabs and show open position result (#112)
This commit was merged in pull request #113.
This commit is contained in:
@@ -2,9 +2,19 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { Portfolio } from "@/entities/portfolio";
|
||||
|
||||
import { toAccountPagePositionSections } from "./positions";
|
||||
import {
|
||||
formatPositionReturnPercent,
|
||||
toAccountPagePositionSections,
|
||||
toUnrealizedReturns,
|
||||
} from "./positions";
|
||||
import { portfolio, position, second } from "./positions.fixtures";
|
||||
|
||||
function makePosition(
|
||||
overrides: Partial<Portfolio["positions"][number]>,
|
||||
): Portfolio["positions"][number] {
|
||||
return { ...position, ...overrides };
|
||||
}
|
||||
|
||||
function sections(positions: Portfolio["positions"]) {
|
||||
return toAccountPagePositionSections({
|
||||
...portfolio,
|
||||
@@ -82,6 +92,8 @@ describe("account position sections", () => {
|
||||
{
|
||||
displayName: "Акция",
|
||||
logoSrc: "https://invest-brands.cdn-tinkoff.ru/akciyax160.png",
|
||||
returnPercent: null,
|
||||
returnPercentText: null,
|
||||
returnText: "+10 ₽",
|
||||
returnTone: "positive",
|
||||
shareText: "66,7 %",
|
||||
@@ -95,6 +107,8 @@ describe("account position sections", () => {
|
||||
{
|
||||
displayName: "Облигация",
|
||||
logoSrc: null,
|
||||
returnPercent: "-11.111111",
|
||||
returnPercentText: "-11,11 %",
|
||||
returnText: "-5 ₽",
|
||||
returnTone: "negative",
|
||||
shareText: "33,3 %",
|
||||
@@ -107,6 +121,8 @@ describe("account position sections", () => {
|
||||
{
|
||||
displayName: "Фонд",
|
||||
logoSrc: null,
|
||||
returnPercent: null,
|
||||
returnPercentText: null,
|
||||
returnText: null,
|
||||
returnTone: undefined,
|
||||
shareText: null,
|
||||
@@ -187,3 +203,109 @@ describe("account position sections", () => {
|
||||
expect(source).toEqual(before);
|
||||
});
|
||||
});
|
||||
|
||||
describe("account position returns", () => {
|
||||
it.each([
|
||||
{
|
||||
absoluteReturn: { currency: "RUB", value: "25" },
|
||||
averagePurchasePrice: { currency: "RUB", value: "280" },
|
||||
expected: "7.142857",
|
||||
name: "gain",
|
||||
quantity: "1.25",
|
||||
type: "STOCK",
|
||||
},
|
||||
{
|
||||
absoluteReturn: { currency: "RUB", value: "-25" },
|
||||
averagePurchasePrice: { currency: "RUB", value: "280" },
|
||||
expected: "-7.142857",
|
||||
name: "loss",
|
||||
quantity: "1.25",
|
||||
type: "STOCK",
|
||||
},
|
||||
{
|
||||
absoluteReturn: { currency: "RUB", value: "-5" },
|
||||
averagePurchasePrice: { currency: "RUB", value: "45" },
|
||||
expected: "-11.111111",
|
||||
name: "bond without accrued interest",
|
||||
quantity: "1",
|
||||
type: "BOND",
|
||||
},
|
||||
] as const)("calculates $name from the clean purchase cost", (testCase) => {
|
||||
const result = sections([makePosition({
|
||||
absoluteReturn: testCase.absoluteReturn,
|
||||
averagePurchasePrice: testCase.averagePurchasePrice,
|
||||
quantity: testCase.quantity,
|
||||
type: testCase.type,
|
||||
})]);
|
||||
|
||||
expect(result[0]?.groups[0]?.positions[0].returnPercent)
|
||||
.toBe(testCase.expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ name: "zero quantity", overrides: { quantity: "0" } },
|
||||
{ name: "negative quantity", overrides: { quantity: "-1" } },
|
||||
{ name: "missing average price", overrides: { averagePurchasePrice: null } },
|
||||
{
|
||||
name: "currency mismatch",
|
||||
overrides: { averagePurchasePrice: { currency: "USD", value: "280" } },
|
||||
},
|
||||
{ name: "zero cost", overrides: { averagePurchasePrice: { currency: "RUB", value: "0" } } },
|
||||
])("returns null for $name", ({ overrides }) => {
|
||||
const result = sections([makePosition({
|
||||
absoluteReturn: { currency: "RUB", value: "25" },
|
||||
averagePurchasePrice: { currency: "RUB", value: "280" },
|
||||
quantity: "1.25",
|
||||
...overrides,
|
||||
})]);
|
||||
|
||||
expect(result[0]?.groups[0]?.positions[0].returnPercent).toBeNull();
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ absoluteReturn: "1", expected: "0.000001" },
|
||||
{ absoluteReturn: "-1", expected: "-0.000001" },
|
||||
])("rounds an exact half away from zero for $absoluteReturn", (testCase) => {
|
||||
const result = sections([makePosition({
|
||||
absoluteReturn: { currency: "RUB", value: testCase.absoluteReturn },
|
||||
averagePurchasePrice: { currency: "RUB", value: "200000000" },
|
||||
quantity: "1",
|
||||
})]);
|
||||
|
||||
expect(result[0]?.groups[0]?.positions[0].returnPercent)
|
||||
.toBe(testCase.expected);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ expected: "+1.01", value: "1.005" },
|
||||
{ expected: "-1.01", value: "-1.005" },
|
||||
])("formats $value for display with a sign and two decimals", (testCase) => {
|
||||
expect(formatPositionReturnPercent(testCase.value)).toBe(testCase.expected);
|
||||
});
|
||||
|
||||
it("sums complete unrealized returns by currency", () => {
|
||||
const result = toUnrealizedReturns([
|
||||
makePosition({ absoluteReturn: { currency: "RUB", value: "0.1" } }),
|
||||
makePosition({ absoluteReturn: { currency: "RUB", value: "0.2" } }),
|
||||
makePosition({ absoluteReturn: { currency: "USD", value: "-2.5" } }),
|
||||
]);
|
||||
|
||||
expect(result).toEqual([
|
||||
{ currency: "RUB", value: "0.3" },
|
||||
{ currency: "USD", value: "-2.5" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("omits only currencies with incomplete returns", () => {
|
||||
const result = toUnrealizedReturns([
|
||||
makePosition({ absoluteReturn: { currency: "RUB", value: "10" } }),
|
||||
makePosition({
|
||||
absoluteReturn: null,
|
||||
currentValue: { currency: "RUB", value: "100" },
|
||||
}),
|
||||
makePosition({ absoluteReturn: { currency: "USD", value: "2" } }),
|
||||
]);
|
||||
|
||||
expect(result).toEqual([{ currency: "USD", value: "2" }]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user