Hone logo
Hone
Problems

Testing a Function with Multiple Inputs Using a Test Matrix in Jest (TypeScript)

Writing comprehensive tests is crucial for ensuring code quality. Sometimes, a function's behavior depends on various input combinations. This challenge focuses on creating a test matrix in Jest to efficiently test a function with multiple inputs, ensuring it behaves as expected across a range of scenarios.

Problem Description

You are given a TypeScript function calculateDiscountedPrice that calculates the discounted price of an item based on its original price and a discount percentage. The function should return the discounted price. Your task is to create a Jest test suite that utilizes a test matrix to test this function with a variety of input combinations, covering both valid and edge case scenarios.

The calculateDiscountedPrice function is defined as follows:

/**
 * Calculates the discounted price of an item.
 * @param originalPrice The original price of the item.
 * @param discountPercentage The discount percentage (e.g., 10 for 10%).
 * @returns The discounted price.  Returns -1 if inputs are invalid.
 */
function calculateDiscountedPrice(originalPrice: number, discountPercentage: number): number {
  if (originalPrice < 0 || discountPercentage < 0 || discountPercentage > 100) {
    return -1;
  }

  const discountAmount = originalPrice * (discountPercentage / 100);
  return originalPrice - discountAmount;
}

Key Requirements:

  • Create a Jest test suite for the calculateDiscountedPrice function.
  • Use a test matrix (array of objects) to define multiple test cases, each with its own originalPrice and discountPercentage inputs and the expected discounted price.
  • Ensure the test suite covers valid input combinations, as well as edge cases (e.g., 0% discount, 100% discount, invalid inputs).
  • The tests should clearly assert that the function returns the correct discounted price for each test case.
  • Tests should handle invalid inputs (negative prices or discount percentages, discount percentages greater than 100) and return -1 as specified.

Expected Behavior:

The test suite should pass if all test cases in the matrix produce the expected discounted price or -1 for invalid inputs. The tests should be readable and well-organized, clearly demonstrating the use of a test matrix.

Examples

Example 1:

Input: originalPrice = 100, discountPercentage = 10
Output: 90
Explanation: 10% of 100 is 10, so the discounted price is 100 - 10 = 90.

Example 2:

Input: originalPrice = 50, discountPercentage = 0
Output: 50
Explanation: 0% discount means no discount, so the price remains 50.

Example 3:

Input: originalPrice = 200, discountPercentage = 100
Output: 0
Explanation: 100% discount means the item is free, so the price is 0.

Example 4: (Edge Case)

Input: originalPrice = -10, discountPercentage = 20
Output: -1
Explanation: Negative original price is invalid, so the function should return -1.

Example 5: (Edge Case)

Input: originalPrice = 100, discountPercentage = 110
Output: -1
Explanation: Discount percentage greater than 100 is invalid, so the function should return -1.

Constraints

  • The originalPrice and discountPercentage will be numbers.
  • The discountPercentage should be between 0 and 100 (inclusive) for valid inputs.
  • The originalPrice should be non-negative for valid inputs.
  • The test suite should be efficient and avoid redundant test cases.
  • The test suite should be well-documented and easy to understand.

Notes

Consider using test.each in Jest to iterate through the test matrix. This provides a clean and concise way to define and execute multiple tests with different input values. Think about how to structure your test matrix to cover a wide range of scenarios effectively. Remember to handle invalid inputs gracefully and assert that the function returns -1 in those cases.

Loading editor...
typescript