Hone logo
Hone
Problems

Testing Conditional Logic with Jest

This challenge focuses on writing Jest tests to verify the behavior of functions containing conditional logic. Understanding how to effectively test conditional statements is crucial for ensuring your code behaves as expected under various circumstances, leading to more robust and reliable applications. You'll be provided with a function containing conditional logic and your task is to write Jest tests to cover different branches of that logic.

Problem Description

You are given a TypeScript function calculateDiscount that calculates a discount based on the orderTotal and customerType. The function has the following logic:

  • If orderTotal is less than 100, no discount is applied.
  • If orderTotal is between 100 and 500 (inclusive), a 10% discount is applied if the customerType is "premium", otherwise no discount.
  • If orderTotal is greater than 500, a 20% discount is applied regardless of customerType.

Your task is to write a suite of Jest tests to ensure that calculateDiscount function behaves correctly for all possible input combinations. You need to test scenarios where no discount is applied, a 10% discount is applied, and a 20% discount is applied. Consider both "premium" and non-"premium" customer types when testing the 10% discount scenario.

Key Requirements:

  • Write clear and descriptive test names.
  • Use expect assertions to verify the calculated discount amount.
  • Cover all branches of the conditional logic within the calculateDiscount function.
  • Ensure your tests are readable and maintainable.

Expected Behavior:

The calculateDiscount function should return the discounted price. The tests should verify that the returned value is the correct discounted price for various inputs.

Edge Cases to Consider:

  • orderTotal being exactly 100 or 500.
  • customerType being an empty string or null/undefined (treat these as non-premium).
  • Negative orderTotal (should return the original order total, no discount).

Examples

Example 1:

Input: orderTotal = 50, customerType = "premium"
Output: 50
Explanation: orderTotal is less than 100, so no discount is applied.

Example 2:

Input: orderTotal = 200, customerType = "premium"
Output: 180
Explanation: orderTotal is between 100 and 500, and customerType is "premium", so a 10% discount is applied (200 * 0.1 = 20).

Example 3:

Input: orderTotal = 600, customerType = "standard"
Output: 480
Explanation: orderTotal is greater than 500, so a 20% discount is applied (600 * 0.2 = 120).

Example 4:

Input: orderTotal = 300, customerType = "guest"
Output: 300
Explanation: orderTotal is between 100 and 500, but customerType is not "premium", so no discount is applied.

Constraints

  • orderTotal will be a number (can be positive, negative, or zero).
  • customerType will be a string (can be empty, null, undefined, or any other string).
  • The discount calculation should be accurate to two decimal places.
  • The function should not throw any errors for valid inputs.

Notes

  • Consider using toBeCloseTo for comparing floating-point numbers in your assertions to account for potential rounding errors.
  • Think about how to structure your tests to maximize coverage and readability. Grouping tests by scenario (e.g., no discount, 10% discount, 20% discount) can be helpful.
  • The calculateDiscount function is provided below. You only need to write the Jest tests.
function calculateDiscount(orderTotal: number, customerType: string | null | undefined): number {
  if (orderTotal < 0) {
    return orderTotal;
  }

  if (orderTotal < 100) {
    return orderTotal;
  }

  if (orderTotal >= 100 && orderTotal <= 500) {
    if (customerType === "premium") {
      return orderTotal * 0.9;
    } else {
      return orderTotal;
    }
  }

  return orderTotal * 0.8;
}
Loading editor...
typescript