add test cases
This commit is contained in:
parent
93370f4173
commit
4e8a8e4276
@ -1,37 +1,192 @@
|
|||||||
import { Cart } from "../../../../model/cart";
|
import { Cart } from "../../../../model/cart";
|
||||||
import { Tariffs } from "../../../../model/tariff";
|
import { Tariffs } from "../../../../model/tariff";
|
||||||
import { User } from "../../../../model/user";
|
import { User } from "../../../../model/user";
|
||||||
import { exampleCartValues } from "../../../../stores/mocks/exampleCartValues";
|
import { exampleCartValues, TestCase } from "../../../../stores/mocks/exampleCartValues";
|
||||||
import { calcCartData } from "./cartCalcs";
|
import { calcCartData } from "./cartCalcs";
|
||||||
|
|
||||||
|
|
||||||
|
const MAX_PRICE_ERROR = 0.01;
|
||||||
|
const discounts = exampleCartValues.discounts;
|
||||||
|
|
||||||
|
describe("cart tests", () => {
|
||||||
|
it("без скидок", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[0]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("сумма в корзине достигла 5к, поэтому применилась скидка", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[1]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("добавил кастомный тариф такой, чтобы пофвилась скидка на продукт", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[2]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("т.е. применилась не id14, а id15, потому что применяется наибольшая подходящая. в то же время, на скидку за лояльность ещё не хватает", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[3]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("case 4", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[4]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("история про то, как скидки за привилегии помешали получить скидку за сервис", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[5]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("то же что и выше, но без лояльности", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[6]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
fit("история про то, как получилось получить скидку за сервис", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[7]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
it("две скидки за сервис", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[8]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
xit("юзер использовал промокод id33. он заменяет скидку на p6 собой. в один момент времени может быть активирован только 1 промокод, т.е. после активации следующего, предыдущий заменяется. но в промокоде может быть несколько скидок. промокоды имеют скидки только на привелеги", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[9]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
xit("юзер подтвердил свой статус НКО, поэтому, не смотря на то что он достиг по лояльности уровня скидки id2, она не применилась, а применилась id32", () => {
|
||||||
|
const testCase = prepareTestCase(exampleCartValues.testCases[10]);
|
||||||
|
|
||||||
|
const cartTotal = calcCartData(testCase.user, testCase.cartItems, discounts);
|
||||||
|
const allEnvolvedDiscounts: string[] = [...cartTotal.envolvedCartDiscounts];
|
||||||
|
cartTotal.items.forEach(cartItem => {
|
||||||
|
allEnvolvedDiscounts.push(...cartItem.envolvedDiscounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(allEnvolvedDiscounts.sort()).toEqual(testCase.expect.envolvedDiscounts.sort());
|
||||||
|
expect(cartTotal.totalPrice).toBeGreaterThan(testCase.expect.price - MAX_PRICE_ERROR);
|
||||||
|
expect(cartTotal.totalPrice).toBeLessThan(testCase.expect.price + MAX_PRICE_ERROR);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function prepareTestCase(testCase: TestCase): ({
|
||||||
|
expect: {
|
||||||
|
price: number;
|
||||||
|
envolvedDiscounts: string[];
|
||||||
|
};
|
||||||
|
user: User;
|
||||||
|
cartItems: Cart.CartItem[];
|
||||||
|
}) {
|
||||||
|
const user = testCase.input.UserInformation;
|
||||||
|
const tariffs = testCase.input.Products.map((testProduct): Tariffs.Tariff => ({
|
||||||
|
amount: testProduct.Amount,
|
||||||
|
customPricePerUnit: testProduct.Price && testProduct.Price / testProduct.Amount, // приводим price из сниппета к pricePerUnit
|
||||||
|
privilege: findPrivilegeById(testProduct.ID)
|
||||||
|
}));
|
||||||
|
const cartItems: Cart.CartItem[] = tariffs.map(tariff => {
|
||||||
|
const pricePerUnit = tariff.customPricePerUnit ?? tariff.privilege.pricePerUnit;
|
||||||
|
const price = pricePerUnit * tariff.amount;
|
||||||
|
|
||||||
|
const cartItem: Cart.CartItem = {
|
||||||
|
item: tariff,
|
||||||
|
price,
|
||||||
|
};
|
||||||
|
|
||||||
|
return cartItem;
|
||||||
|
});
|
||||||
|
|
||||||
|
return { expect: testCase.expect, user, cartItems };
|
||||||
|
}
|
||||||
|
|
||||||
function findPrivilegeById(id: string) {
|
function findPrivilegeById(id: string) {
|
||||||
const privilege = exampleCartValues.privileges.find(privilege => privilege.privilegeId === id);
|
const privilege = exampleCartValues.privileges.find(privilege => privilege.privilegeId === id);
|
||||||
if (!privilege) throw new Error(`Privilege not found with id ${id}`);
|
if (!privilege) throw new Error(`Privilege not found with id ${id}`);
|
||||||
|
|
||||||
return privilege;
|
return privilege;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("cart tests", () => {
|
|
||||||
it("test case 1", () => {
|
|
||||||
const user: User = exampleCartValues.testCases[0].input.UserInformation;
|
|
||||||
const tariffs: Tariffs.Tariff[] = exampleCartValues.testCases[0].input.Products.map((testProduct) => ({
|
|
||||||
amount: testProduct.Amount,
|
|
||||||
customPrice: testProduct.Price,
|
|
||||||
privilege: findPrivilegeById(testProduct.ID)
|
|
||||||
}));
|
|
||||||
const cartItems: Cart.CartItem[] = tariffs.map(tariff => {
|
|
||||||
const pricePerUnit = tariff.customPrice ?? tariff.privilege.price;
|
|
||||||
const price = pricePerUnit * tariff.amount;
|
|
||||||
|
|
||||||
const cartItem: Cart.CartItem = {
|
|
||||||
item: tariff,
|
|
||||||
price,
|
|
||||||
};
|
|
||||||
|
|
||||||
return cartItem;
|
|
||||||
});
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user