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

36 lines
1.8 KiB
JavaScript

import { describe, expect, it } from "vitest";
import {
isProductCategoryRestrictedForCustomer,
isProductRestrictedForCustomer,
isTankCleaningProduct,
} from "@/features/customer/customerProductRules.js";
const onlyTankCleaning = [{ attribute: "onlyTankCleaning" }];
const restrictTankCleaning = [{ attribute: "restrictTankCleaning" }];
describe("customer product rules", () => {
it("recognizes tankcleaning products by category and legacy names", () => {
expect(isTankCleaningProduct({ category: 5, name: "Saebe/kemi" })).toBe(true);
expect(isTankCleaningProduct({ category: 4, name: "Tank cleaning 4 spulehoveder" })).toBe(true);
expect(isTankCleaningProduct({ category: 4, name: "Saebe/kemi", category_name: "Tankrens" })).toBe(true);
expect(isTankCleaningProduct({ category: 4, name: "Forvogn" })).toBe(false);
});
it("limits only-tankcleaning customers to tankcleaning products", () => {
expect(isProductRestrictedForCustomer({ category: 4, name: "Forvogn" }, onlyTankCleaning)).toBe(true);
expect(isProductRestrictedForCustomer({ category: 5, name: "Tank cleaning" }, onlyTankCleaning)).toBe(false);
expect(isProductRestrictedForCustomer({ category: 4, name: "Forvogn" }, [])).toBe(false);
});
it("does not invert tankcleaning access for regular customers", () => {
expect(isProductRestrictedForCustomer({ category: 5, name: "Tank cleaning" }, [])).toBe(false);
expect(isProductRestrictedForCustomer({ category: 5, name: "Tank cleaning" }, restrictTankCleaning)).toBe(true);
});
it("applies category restrictions consistently", () => {
expect(isProductCategoryRestrictedForCustomer(4, onlyTankCleaning)).toBe(true);
expect(isProductCategoryRestrictedForCustomer(5, onlyTankCleaning)).toBe(false);
expect(isProductCategoryRestrictedForCustomer("tank_cleaning", restrictTankCleaning)).toBe(true);
});
});