Implement the tiered discount with tests

Node · Node · advanced · greenfield

Implements the tiered discount with the pricing policy in shared config, and adds full unit coverage: small orders, the 10% tier, and the cap on large orders. All three tests pass.

Subtotals are whole dollars (cart totals are computed in integer cents upstream and passed here as dollars).

Requirements

Files touched

--- src/config/pricing.js
+const DISCOUNT_RATE = 0.1;
+const DISCOUNT_THRESHOLD = 100;
+const DISCOUNT_CAP = 50;
+
+module.exports = { DISCOUNT_RATE, DISCOUNT_THRESHOLD, DISCOUNT_CAP };
--- src/services/discount.js
+const { DISCOUNT_RATE, DISCOUNT_THRESHOLD, DISCOUNT_CAP } = require("../config/pricing");
+
+function applyDiscount(subtotal) {
+  if (subtotal < DISCOUNT_THRESHOLD) return subtotal;
+  const discount = Math.min(subtotal * DISCOUNT_RATE, DISCOUNT_CAP);
+  return subtotal - discount;
+}
+
+module.exports = { applyDiscount };
--- src/services/discount.test.js
+const { applyDiscount } = require("./discount");
+const { DISCOUNT_RATE, DISCOUNT_CAP } = require("../config/pricing");
+
+describe("applyDiscount", () => {
+  it("leaves small orders untouched", () => {
+    expect(applyDiscount(80)).toBe(80);
+  });
+
+  it("applies the discount above the threshold", () => {
+    const subtotal = 240;
+    const expected = subtotal - subtotal * DISCOUNT_RATE;
+    expect(applyDiscount(subtotal)).toBe(expected);
+  });

Review this PR

Node practice