front-hub/src/utils/calcCart/calcCart.test.ts

888 lines
28 KiB
TypeScript
Raw Normal View History

2023-07-22 10:38:10 +00:00
import { CartData, Discount, Tariff } from "@frontend/kitui";
import { calcCart } from "./calcCart";
describe("Cart calculations", () => {
describe("without discounts", () => {
it("calculates cart with 1 item", () => {
const cart = calcCart([templategenTariff1], [], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [],
};
expect(cart).toStrictEqual(expectedCart);
});
it("calculates cart with 3 items", () => {
const cart = calcCart([templategenTariff1, squizTariff, reducerTariff], [], 0);
const expectedCart: CartData = {
itemCount: 3,
priceAfterDiscounts: 100 * 100 + 200 * 200 + 300 * 300,
priceBeforeDiscounts: 100 * 100 + 200 * 200 + 300 * 300,
services: [
{
serviceKey: "templategen",
price: 100 * 100,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
{
serviceKey: "squiz",
price: 200 * 200,
privileges: [
{
description: "d2",
price: 200 * 200,
privilegeId: "p2",
serviceKey: "squiz",
tariffId: "t2",
},
]
},
{
serviceKey: "reducer",
price: 300 * 300,
privileges: [
{
description: "d3",
price: 300 * 300,
privilegeId: "p3",
serviceKey: "reducer",
tariffId: "t3",
},
]
},
],
envolvedDiscounts: [],
};
expect(cart).toStrictEqual(expectedCart);
});
it("calculates cart with items for the same service", () => {
const cart = calcCart([templategenTariff1, templategenTariff2, reducerTariff], [], 0);
const expectedCart: CartData = {
itemCount: 3,
priceAfterDiscounts: 100 * 100 + 600 * 600 + 300 * 300,
priceBeforeDiscounts: 100 * 100 + 600 * 600 + 300 * 300,
services: [
{
serviceKey: "templategen",
price: 100 * 100 + 600 * 600,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
{
description: "d5",
price: 600 * 600,
privilegeId: "p5",
serviceKey: "templategen",
tariffId: "t5",
},
]
},
{
serviceKey: "reducer",
price: 300 * 300,
privileges: [
{
description: "d3",
price: 300 * 300,
privilegeId: "p3",
serviceKey: "reducer",
tariffId: "t3",
},
]
},
],
envolvedDiscounts: [],
};
expect(cart).toStrictEqual(expectedCart);
});
it("returns blank cart when no tariffs", () => {
const cart = calcCart([], [], 0);
const expectedCart: CartData = {
itemCount: 0,
priceAfterDiscounts: 0,
priceBeforeDiscounts: 0,
services: [],
envolvedDiscounts: [],
};
expect(cart).toStrictEqual(expectedCart);
});
});
describe("with single discount", () => {
it("applies privilege discount to tariff 1", () => {
const cart = calcCart([templategenTariff1], [templategenPrivilegeDiscount], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100 * 0.9,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100 * 0.9,
privileges: [
{
description: "d1",
price: 100 * 100 * 0.9,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [templategenPrivilegeDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies privilege discount to tariff 2", () => {
const cart = calcCart([reducerTariff], [reducerPrivilegeDiscount], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 300 * 300 * 0.95,
priceBeforeDiscounts: 300 * 300,
services: [
{
serviceKey: "reducer",
price: 300 * 300 * 0.95,
privileges: [
{
description: "d3",
price: 300 * 300 * 0.95,
privilegeId: "p3",
serviceKey: "reducer",
tariffId: "t3",
},
]
},
],
envolvedDiscounts: [reducerPrivilegeDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies service discount to tariffs", () => {
const cart = calcCart([templategenTariff1, templategenTariff2], [templategenServiceDiscount], 0);
const expectedCart: CartData = {
itemCount: 2,
priceAfterDiscounts: (100 * 100 + 600 * 600) * 0.8,
priceBeforeDiscounts: 100 * 100 + 600 * 600,
services: [
{
serviceKey: "templategen",
price: (100 * 100 + 600 * 600) * 0.8,
privileges: [
{
description: "d1",
price: 100 * 100 * 0.8,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
{
description: "d5",
price: 600 * 600 * 0.8,
privilegeId: "p5",
serviceKey: "templategen",
tariffId: "t5",
},
]
},
],
envolvedDiscounts: [templategenServiceDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies privilege discount to 2 tariffs", () => {
const cart = calcCart([templategenTariff1, templategenTariff2], [templategenPrivilegeDiscount], 0);
const expectedCart: CartData = {
itemCount: 2,
priceAfterDiscounts: 100 * 100 * 0.9 + 600 * 600,
priceBeforeDiscounts: 100 * 100 + 600 * 600,
services: [
{
serviceKey: "templategen",
price: 100 * 100 * 0.9 + 600 * 600,
privileges: [
{
description: "d1",
price: 100 * 100 * 0.9,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
{
description: "d5",
price: 600 * 600,
privilegeId: "p5",
serviceKey: "templategen",
tariffId: "t5",
},
]
},
],
envolvedDiscounts: [templategenPrivilegeDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies cart purchases discount to tariff", () => {
const cart = calcCart([templategenTariff1], [cartPurchasesDiscount], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100 * 0.7,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [cartPurchasesDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("doesn't apply cart purchases discount when cartPurchasesAmount is not enough", () => {
const cart = calcCart([templategenTariff1], [highAmountCartPurchasesDiscount], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies loyalty discount to tariff", () => {
const cart = calcCart([templategenTariff1], [loyaltyDiscount], 1001);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100 * 0.6,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [loyaltyDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("doesn't apply loyalty discount when purchasesAmount is not enough", () => {
const cart = calcCart([templategenTariff1], [loyaltyDiscount], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100,
privileges: [
{
description: "d1",
price: 100 * 100,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [],
};
expect(cart).toStrictEqual(expectedCart);
});
});
describe("with multiple discounts", () => {
it("applies privilege and service discounts to tariff", () => {
const cart = calcCart([templategenTariff1], [templategenPrivilegeDiscount, templategenServiceDiscount], 0);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100 * 0.9 * 0.8,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100 * 0.9 * 0.8,
privileges: [
{
description: "d1",
price: 100 * 100 * 0.9 * 0.8,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [templategenPrivilegeDiscount, templategenServiceDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies all types of discounts to tariff", () => {
const cart = calcCart(
[templategenTariff1],
[templategenPrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount],
1001
);
const expectedCart: CartData = {
itemCount: 1,
priceAfterDiscounts: 100 * 100 * 0.9 * 0.8 * 0.7 * 0.6,
priceBeforeDiscounts: 100 * 100,
services: [
{
serviceKey: "templategen",
price: 100 * 100 * 0.9 * 0.8,
privileges: [
{
description: "d1",
price: 100 * 100 * 0.9 * 0.8,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
],
envolvedDiscounts: [templategenPrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies different discounts to different tariffs", () => {
const cart = calcCart(
[templategenTariff1, reducerTariff],
[templategenPrivilegeDiscount, reducerPrivilegeDiscount],
1001
);
const expectedCart: CartData = {
itemCount: 2,
priceAfterDiscounts: 100 * 100 * 0.9 + 300 * 300 * 0.95,
priceBeforeDiscounts: 100 * 100 + 300 * 300,
services: [
{
serviceKey: "templategen",
price: 100 * 100 * 0.9,
privileges: [
{
description: "d1",
price: 100 * 100 * 0.9,
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
]
},
{
serviceKey: "reducer",
price: 300 * 300 * 0.95,
privileges: [
{
description: "d3",
price: 300 * 300 * 0.95,
privilegeId: "p3",
serviceKey: "reducer",
tariffId: "t3",
},
]
},
],
envolvedDiscounts: [templategenPrivilegeDiscount, reducerPrivilegeDiscount],
};
expect(cart).toStrictEqual(expectedCart);
});
it("applies all types of discounts to multiple tariffs", () => {
const cart = calcCart(
[templategenTariff1, templategenTariff2, squizTariff, reducerTariff],
2023-07-22 14:05:53 +00:00
[templategenPrivilegeDiscount, reducerPrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount],
2023-07-22 10:38:10 +00:00
1001
);
const expectedCart: CartData = {
itemCount: 4,
2023-07-22 14:05:53 +00:00
priceAfterDiscounts: ((100 * 100 * 0.9 + 600 * 600) * 0.8 + 200 * 200 + 300 * 300 * 0.95) * 0.7 * 0.6,
2023-07-22 10:38:10 +00:00
priceBeforeDiscounts: 100 * 100 + 600 * 600 + 200 * 200 + 300 * 300,
services: [
{
serviceKey: "templategen",
2023-07-22 14:05:53 +00:00
price: (100 * 100 * 0.9 + 600 * 600) * 0.8,
2023-07-22 10:38:10 +00:00
privileges: [
{
description: "d1",
2023-07-22 14:05:53 +00:00
price: 100 * 100 * 0.9 * 0.8,
2023-07-22 10:38:10 +00:00
privilegeId: "p1",
serviceKey: "templategen",
tariffId: "t1",
},
{
description: "d5",
2023-07-22 14:05:53 +00:00
price: 600 * 600 * 0.8,
2023-07-22 10:38:10 +00:00
privilegeId: "p5",
serviceKey: "templategen",
tariffId: "t5",
},
]
},
{
serviceKey: "squiz",
price: 200 * 200,
privileges: [
{
description: "d2",
price: 200 * 200,
privilegeId: "p2",
serviceKey: "squiz",
tariffId: "t2",
},
]
},
{
serviceKey: "reducer",
price: 300 * 300 * 0.95,
privileges: [
{
description: "d3",
price: 300 * 300 * 0.95,
privilegeId: "p3",
serviceKey: "reducer",
tariffId: "t3",
},
]
},
],
2023-07-22 14:05:53 +00:00
envolvedDiscounts: [templategenPrivilegeDiscount, reducerPrivilegeDiscount, templategenServiceDiscount, cartPurchasesDiscount, loyaltyDiscount],
2023-07-22 10:38:10 +00:00
};
expect(cart).toStrictEqual(expectedCart);
});
});
});
const templategenTariff1: Tariff = {
_id: "t1",
name: "tariff1",
price: 0,
isCustom: false,
privilegies: [
{
name: "n1",
privilegeId: "p1",
serviceKey: "templategen",
description: "d1",
type: "count",
value: "МБ",
price: 100,
amount: 100,
},
],
isDeleted: false,
createdAt: "",
updatedAt: ""
};
const templategenTariff2: Tariff = {
_id: "t5",
name: "tariff5",
price: 0,
isCustom: false,
privilegies: [
{
name: "n5",
privilegeId: "p5",
serviceKey: "templategen",
description: "d5",
type: "count",
value: "МБ",
price: 600,
amount: 600,
},
],
isDeleted: false,
createdAt: "",
updatedAt: ""
};
const squizTariff: Tariff = {
_id: "t2",
name: "tariff2",
price: 0,
isCustom: false,
privilegies: [
{
name: "n2",
privilegeId: "p2",
serviceKey: "squiz",
description: "d2",
type: "count",
value: "МБ",
price: 200,
amount: 200,
},
],
isDeleted: false,
createdAt: "",
updatedAt: ""
};
const reducerTariff: Tariff = {
_id: "t3",
name: "tariff3",
price: 0,
isCustom: false,
privilegies: [
{
name: "n3",
privilegeId: "p3",
serviceKey: "reducer",
description: "d3",
type: "count",
value: "МБ",
price: 300,
amount: 300,
},
],
isDeleted: false,
createdAt: "",
updatedAt: ""
};
const templategenPrivilegeDiscount: Discount = {
ID: "id1",
Name: "n1",
Layer: 1,
Description: "d1",
Condition: {
Period: {
From: "",
To: ""
},
User: "",
UserType: "",
Coupon: "",
PurchasesAmount: 0,
CartPurchasesAmount: 0,
Product: "p1",
Term: "1000",
Usage: "0",
PriceFrom: 0,
Group: "templategen"
},
Target: {
Products: [
{
ID: "p1",
Factor: 0.9,
Overhelm: false
}
],
Factor: 0.9,
TargetScope: "Sum",
TargetGroup: "templategen",
Overhelm: false
},
Audit: {
UpdatedAt: "",
CreatedAt: "",
Deleted: false
},
Deprecated: false
};
const templategenServiceDiscount: Discount = {
ID: "id2",
Name: "n2",
Layer: 2,
Description: "d2",
Condition: {
Period: {
From: "",
To: ""
},
User: "",
UserType: "",
Coupon: "",
PurchasesAmount: 0,
CartPurchasesAmount: 0,
Product: "",
Term: "1000",
Usage: "0",
PriceFrom: 0,
Group: "templategen"
},
Target: {
Products: [
{
2023-07-22 14:05:53 +00:00
ID: "",
2023-07-22 10:38:10 +00:00
Factor: 0.8,
Overhelm: false
}
],
Factor: 0.8,
TargetScope: "Sum",
TargetGroup: "templategen",
Overhelm: false
},
Audit: {
UpdatedAt: "",
CreatedAt: "",
Deleted: false
},
Deprecated: false
};
const reducerPrivilegeDiscount: Discount = {
ID: "id11",
Name: "n11",
Layer: 1,
Description: "d11",
Condition: {
Period: {
From: "",
To: ""
},
User: "",
UserType: "",
Coupon: "",
PurchasesAmount: 0,
CartPurchasesAmount: 0,
Product: "p3",
Term: "1000",
Usage: "0",
PriceFrom: 0,
Group: "reducer"
},
Target: {
Products: [
{
ID: "p1",
Factor: 0.95,
Overhelm: false
}
],
Factor: 0.95,
TargetScope: "Sum",
TargetGroup: "reducer",
Overhelm: false
},
Audit: {
UpdatedAt: "",
CreatedAt: "",
Deleted: false
},
Deprecated: false
};
const cartPurchasesDiscount: Discount = {
ID: "id3",
Name: "n3",
Layer: 3,
Description: "d3",
Condition: {
Period: {
From: "",
To: ""
},
User: "",
UserType: "",
Coupon: "",
PurchasesAmount: 0,
CartPurchasesAmount: 1000,
Product: "",
Term: "0",
Usage: "0",
PriceFrom: 0,
Group: "templategen"
},
Target: {
Products: [
{
ID: "p1",
Factor: 0.7,
Overhelm: false
}
],
Factor: 0.7,
TargetScope: "Sum",
TargetGroup: "templategen",
Overhelm: false
},
Audit: {
UpdatedAt: "",
CreatedAt: "",
Deleted: false
},
Deprecated: false
};
const highAmountCartPurchasesDiscount: Discount = {
ID: "id3",
Name: "n3",
Layer: 3,
Description: "d3",
Condition: {
Period: {
From: "",
To: ""
},
User: "",
UserType: "",
Coupon: "",
PurchasesAmount: 0,
CartPurchasesAmount: 1000000,
Product: "",
Term: "0",
Usage: "0",
PriceFrom: 0,
Group: "templategen"
},
Target: {
Products: [
{
ID: "p1",
Factor: 0.7,
Overhelm: false
}
],
Factor: 0.7,
TargetScope: "Sum",
TargetGroup: "templategen",
Overhelm: false
},
Audit: {
UpdatedAt: "",
CreatedAt: "",
Deleted: false
},
Deprecated: false
};
const loyaltyDiscount: Discount = {
ID: "id3",
Name: "n3",
Layer: 4,
Description: "d3",
Condition: {
Period: {
From: "",
To: ""
},
User: "",
UserType: "",
Coupon: "",
PurchasesAmount: 1000,
CartPurchasesAmount: 0,
Product: "",
Term: "0",
Usage: "0",
PriceFrom: 0,
Group: "templategen"
},
Target: {
Products: [
{
ID: "p1",
Factor: 0.6,
Overhelm: false
}
],
Factor: 0.6,
TargetScope: "Sum",
TargetGroup: "templategen",
Overhelm: false
},
Audit: {
UpdatedAt: "",
CreatedAt: "",
Deleted: false
},
Deprecated: false
};