Implemented Rollback Functionality
This commit is contained in:
@@ -5,6 +5,8 @@ const {
|
||||
calculatePercentageChange,
|
||||
isValidPercentage,
|
||||
preparePriceUpdate,
|
||||
validateRollbackEligibility,
|
||||
prepareRollbackUpdate,
|
||||
} = require("../../src/utils/price");
|
||||
|
||||
describe("Price Utilities", () => {
|
||||
@@ -260,4 +262,315 @@ describe("Price Utilities", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateRollbackEligibility", () => {
|
||||
test("should return eligible for valid variant with different prices", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "75.00",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(true);
|
||||
expect(result.variant.id).toBe("gid://shopify/ProductVariant/123");
|
||||
expect(result.variant.currentPrice).toBe(50.0);
|
||||
expect(result.variant.compareAtPrice).toBe(75.0);
|
||||
expect(result.reason).toBeUndefined();
|
||||
});
|
||||
|
||||
test("should return not eligible when variant is null or undefined", () => {
|
||||
expect(validateRollbackEligibility(null).isEligible).toBe(false);
|
||||
expect(validateRollbackEligibility(null).reason).toBe(
|
||||
"Invalid variant object"
|
||||
);
|
||||
|
||||
expect(validateRollbackEligibility(undefined).isEligible).toBe(false);
|
||||
expect(validateRollbackEligibility(undefined).reason).toBe(
|
||||
"Invalid variant object"
|
||||
);
|
||||
});
|
||||
|
||||
test("should return not eligible when variant is not an object", () => {
|
||||
expect(validateRollbackEligibility("invalid").isEligible).toBe(false);
|
||||
expect(validateRollbackEligibility("invalid").reason).toBe(
|
||||
"Invalid variant object"
|
||||
);
|
||||
|
||||
expect(validateRollbackEligibility(123).isEligible).toBe(false);
|
||||
expect(validateRollbackEligibility(123).reason).toBe(
|
||||
"Invalid variant object"
|
||||
);
|
||||
});
|
||||
|
||||
test("should return not eligible when current price is invalid", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "invalid",
|
||||
compareAtPrice: "75.00",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("Invalid current price");
|
||||
expect(result.variant.currentPrice).toBeNaN();
|
||||
});
|
||||
|
||||
test("should return not eligible when current price is negative", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "-10.00",
|
||||
compareAtPrice: "75.00",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("Invalid current price");
|
||||
expect(result.variant.currentPrice).toBe(-10.0);
|
||||
});
|
||||
|
||||
test("should return not eligible when compare-at price is null", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: null,
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("No compare-at price available");
|
||||
expect(result.variant.compareAtPrice).toBe(null);
|
||||
});
|
||||
|
||||
test("should return not eligible when compare-at price is undefined", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
// compareAtPrice is undefined
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("No compare-at price available");
|
||||
expect(result.variant.compareAtPrice).toBe(null);
|
||||
});
|
||||
|
||||
test("should return not eligible when compare-at price is invalid", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "invalid",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("Invalid compare-at price");
|
||||
expect(result.variant.compareAtPrice).toBeNaN();
|
||||
});
|
||||
|
||||
test("should return not eligible when compare-at price is zero", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "0.00",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("Compare-at price must be greater than zero");
|
||||
expect(result.variant.compareAtPrice).toBe(0.0);
|
||||
});
|
||||
|
||||
test("should return not eligible when compare-at price is negative", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "-10.00",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe("Compare-at price must be greater than zero");
|
||||
expect(result.variant.compareAtPrice).toBe(-10.0);
|
||||
});
|
||||
|
||||
test("should return not eligible when prices are the same", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "50.00",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe(
|
||||
"Compare-at price is the same as current price"
|
||||
);
|
||||
expect(result.variant.currentPrice).toBe(50.0);
|
||||
expect(result.variant.compareAtPrice).toBe(50.0);
|
||||
});
|
||||
|
||||
test("should return not eligible when prices are nearly the same (within epsilon)", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "50.005", // Within 0.01 epsilon
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(false);
|
||||
expect(result.reason).toBe(
|
||||
"Compare-at price is the same as current price"
|
||||
);
|
||||
});
|
||||
|
||||
test("should handle numeric price values", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: 50.0,
|
||||
compareAtPrice: 75.0,
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(true);
|
||||
expect(result.variant.currentPrice).toBe(50.0);
|
||||
expect(result.variant.compareAtPrice).toBe(75.0);
|
||||
});
|
||||
|
||||
test("should handle decimal prices correctly", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "29.99",
|
||||
compareAtPrice: "39.99",
|
||||
};
|
||||
|
||||
const result = validateRollbackEligibility(variant);
|
||||
|
||||
expect(result.isEligible).toBe(true);
|
||||
expect(result.variant.currentPrice).toBe(29.99);
|
||||
expect(result.variant.compareAtPrice).toBe(39.99);
|
||||
});
|
||||
});
|
||||
|
||||
describe("prepareRollbackUpdate", () => {
|
||||
test("should prepare rollback update for eligible variant", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "75.00",
|
||||
};
|
||||
|
||||
const result = prepareRollbackUpdate(variant);
|
||||
|
||||
expect(result.newPrice).toBe(75.0);
|
||||
expect(result.compareAtPrice).toBe(null);
|
||||
});
|
||||
|
||||
test("should prepare rollback update with decimal prices", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "29.99",
|
||||
compareAtPrice: "39.99",
|
||||
};
|
||||
|
||||
const result = prepareRollbackUpdate(variant);
|
||||
|
||||
expect(result.newPrice).toBe(39.99);
|
||||
expect(result.compareAtPrice).toBe(null);
|
||||
});
|
||||
|
||||
test("should handle numeric price values", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: 25.5,
|
||||
compareAtPrice: 35.75,
|
||||
};
|
||||
|
||||
const result = prepareRollbackUpdate(variant);
|
||||
|
||||
expect(result.newPrice).toBe(35.75);
|
||||
expect(result.compareAtPrice).toBe(null);
|
||||
});
|
||||
|
||||
test("should throw error for variant without compare-at price", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: null,
|
||||
};
|
||||
|
||||
expect(() => prepareRollbackUpdate(variant)).toThrow(
|
||||
"Cannot prepare rollback update: No compare-at price available"
|
||||
);
|
||||
});
|
||||
|
||||
test("should throw error for variant with invalid compare-at price", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "invalid",
|
||||
};
|
||||
|
||||
expect(() => prepareRollbackUpdate(variant)).toThrow(
|
||||
"Cannot prepare rollback update: Invalid compare-at price"
|
||||
);
|
||||
});
|
||||
|
||||
test("should throw error for variant with zero compare-at price", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "0.00",
|
||||
};
|
||||
|
||||
expect(() => prepareRollbackUpdate(variant)).toThrow(
|
||||
"Cannot prepare rollback update: Compare-at price must be greater than zero"
|
||||
);
|
||||
});
|
||||
|
||||
test("should throw error for variant with same prices", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "50.00",
|
||||
compareAtPrice: "50.00",
|
||||
};
|
||||
|
||||
expect(() => prepareRollbackUpdate(variant)).toThrow(
|
||||
"Cannot prepare rollback update: Compare-at price is the same as current price"
|
||||
);
|
||||
});
|
||||
|
||||
test("should throw error for invalid variant object", () => {
|
||||
expect(() => prepareRollbackUpdate(null)).toThrow(
|
||||
"Cannot prepare rollback update: Invalid variant object"
|
||||
);
|
||||
|
||||
expect(() => prepareRollbackUpdate("invalid")).toThrow(
|
||||
"Cannot prepare rollback update: Invalid variant object"
|
||||
);
|
||||
});
|
||||
|
||||
test("should throw error for variant with invalid current price", () => {
|
||||
const variant = {
|
||||
id: "gid://shopify/ProductVariant/123",
|
||||
price: "invalid",
|
||||
compareAtPrice: "75.00",
|
||||
};
|
||||
|
||||
expect(() => prepareRollbackUpdate(variant)).toThrow(
|
||||
"Cannot prepare rollback update: Invalid current price"
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user