Hone logo
Hone
Problems

Testing Functions with Arguments in Jest (TypeScript)

Testing functions that accept arguments is a crucial skill for ensuring code reliability. This challenge focuses on writing Jest tests for TypeScript functions that take arguments, verifying that they behave as expected with various inputs. Mastering this will allow you to confidently test any function, regardless of its complexity.

Problem Description

You are tasked with writing Jest tests for a TypeScript function called calculateDiscountedPrice. This function takes two arguments: price (a number representing the original price) and discountPercentage (a number representing the discount percentage, between 0 and 100). The function should calculate and return the discounted price.

What needs to be achieved:

  • Implement Jest tests to verify the calculateDiscountedPrice function's behavior.
  • Tests should cover various scenarios, including positive prices, zero discount, 100% discount, and edge cases.
  • Ensure the tests accurately assert the expected discounted price for each input.

Key Requirements:

  • The calculateDiscountedPrice function is provided. You should not modify it.
  • Write at least 5 Jest tests covering different scenarios.
  • Use expect assertions to verify the returned values.
  • Tests should be clear, concise, and well-documented.

Expected Behavior:

The calculateDiscountedPrice function should return the price after applying the discount. The discount should be calculated as price * (discountPercentage / 100). The returned value should be rounded to two decimal places.

Edge Cases to Consider:

  • discountPercentage is 0 (no discount).
  • discountPercentage is 100 (full discount).
  • price is 0.
  • discountPercentage is a negative number (should return the original price).
  • discountPercentage is greater than 100 (should return 0).

Examples

Example 1:

Input: price = 100, discountPercentage = 10
Output: 90.00
Explanation: Discount = 100 * (10 / 100) = 10. Discounted price = 100 - 10 = 90. Rounded to two decimal places: 90.00

Example 2:

Input: price = 50, discountPercentage = 50
Output: 25.00
Explanation: Discount = 50 * (50 / 100) = 25. Discounted price = 50 - 25 = 25. Rounded to two decimal places: 25.00

Example 3:

Input: price = 200, discountPercentage = -10
Output: 200.00
Explanation: Negative discount should return the original price.

Example 4:

Input: price = 100, discountPercentage = 110
Output: 0.00
Explanation: Discount percentage greater than 100 should return 0.

Constraints

  • price will always be a number.
  • discountPercentage will always be a number.
  • The calculateDiscountedPrice function will be provided.
  • Tests must use Jest and TypeScript.
  • The discounted price must be rounded to two decimal places.

Notes

  • Consider using toBeCloseTo for comparing floating-point numbers in your assertions to account for potential rounding errors.
  • Think about the different scenarios and edge cases that need to be tested to ensure the function's robustness.
  • Focus on writing clear and readable tests that are easy to understand and maintain.
const calculateDiscountedPrice = (price: number, discountPercentage: number): number => {
  const discount = price * (discountPercentage / 100);
  const discountedPrice = price - discount;
  return parseFloat(discountedPrice.toFixed(2));
};
Loading editor...
typescript