Files
pleno-vue/tests/unit/customer-product-fixed-price.spec.js
T

126 lines
3.1 KiB
JavaScript

// @vitest-environment jsdom
import { mount } from "@vue/test-utils";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/components/session/authenticatedRequest.vue", () => ({
authenticatedRequest: vi.fn(),
}));
vi.mock("@/components/session/token/SessionUser.vue", () => ({
SessionUser: {},
}));
import {
getProductBestApplicableDiscount,
getProductEffectivePrice,
getUserProductFixedPrice,
user,
} from "@/views/dashboards/superUserDashboard/user/SuperUserSelectedUserObject.vue";
import CustomerProductDiscountDisplay from "@/components/displays/department/pos/displays/CustomerProductDiscountDisplay.vue";
const product = {
id: 101,
category: 9,
category_id: 9,
name: "Premium wash",
price: 1000,
apply_category_discount: 1,
};
const globalDiscount = {
id: 999999,
product_or_category_id: "global",
is_category: 1,
percentage: 50,
fixed_price: null,
};
const categoryDiscount = {
id: 200,
product_or_category_id: 9,
is_category: 1,
percentage: 80,
fixed_price: null,
};
beforeEach(() => {
user.discounts.value = [];
});
describe("customer product fixed prices", () => {
it("uses a direct fixed product price as the effective price without changing discount selection", () => {
user.discounts.value = [
globalDiscount,
categoryDiscount,
{
id: 300,
product_or_category_id: 101,
is_category: 0,
percentage: 10,
fixed_price: 350,
},
];
expect(getProductBestApplicableDiscount(product)).toBe(80);
expect(getUserProductFixedPrice(product)).toBe(350);
expect(getProductEffectivePrice(product)).toBe(350);
});
it("falls back to the existing best-discount calculation when fixed price is not set", () => {
user.discounts.value = [
globalDiscount,
{
...categoryDiscount,
percentage: 30,
},
{
id: 300,
product_or_category_id: 101,
is_category: 0,
percentage: 10,
fixed_price: null,
},
];
expect(getUserProductFixedPrice(product)).toBeNull();
expect(getProductBestApplicableDiscount(product)).toBe(50);
expect(getProductEffectivePrice(product)).toBe(500);
});
it("treats zero as a set fixed price", () => {
user.discounts.value = [
{
id: 300,
product_or_category_id: 101,
is_category: 0,
percentage: 10,
fixed_price: 0,
},
];
expect(getUserProductFixedPrice(product)).toBe(0);
expect(getProductEffectivePrice(product)).toBe(0);
});
it("shows fixed price instead of discount percentage in the POS product badge", () => {
const wrapper = mount(CustomerProductDiscountDisplay, {
props: {
product,
customer_discounts: [
categoryDiscount,
{
id: 300,
product_or_category_id: 101,
is_category: 0,
percentage: 10,
fixed_price: 350,
},
],
},
});
expect(wrapper.text()).toContain("350 Kr.");
expect(wrapper.text()).not.toContain("-80%");
});
});