Hone logo
Hone
Problems

Testing Consumer Functions with Jest in TypeScript

Consumer functions are functions that take data and perform some action, often displaying it or using it to update the UI. Ensuring these functions behave correctly is crucial for application stability and a good user experience. This challenge will guide you in writing Jest tests specifically for consumer functions in a TypeScript environment, focusing on verifying their output based on different input states.

Problem Description

You are tasked with creating Jest tests for a consumer function called formatUserDisplay. This function takes a User object as input and returns a formatted string suitable for displaying user information. The function should handle cases where certain user properties might be missing (e.g., no email, no phone number).

What needs to be achieved:

Write a suite of Jest tests to verify that formatUserDisplay produces the expected output for various User object inputs, including cases with missing data.

Key Requirements:

  • The tests should cover different scenarios, including users with all properties, users with missing email, users with missing phone number, and users with both missing.
  • The tests should use Jest's expect assertions to compare the actual output of formatUserDisplay with the expected output.
  • The tests should be well-structured and easy to understand.

Expected Behavior:

The formatUserDisplay function should return a string formatted as follows:

"Name: [name], Email: [email], Phone: [phone]"

If a property is missing, it should be omitted from the string. For example, if the email is missing, the string should be:

"Name: [name], Phone: [phone]"

Edge Cases to Consider:

  • Empty string values for properties (e.g., email: ""). These should be treated as missing values and omitted from the output.
  • null or undefined values for properties. These should also be treated as missing values.
  • Invalid or unexpected data types for properties (though the function is expected to receive a User object, consider how to handle unexpected types gracefully in your tests).

Examples

Example 1:

Input: { name: "Alice", email: "alice@example.com", phone: "123-456-7890" }
Output: "Name: Alice, Email: alice@example.com, Phone: 123-456-7890"
Explanation: All properties are present, so the full formatted string is returned.

Example 2:

Input: { name: "Bob", email: null, phone: "987-654-3210" }
Output: "Name: Bob, Phone: 987-654-3210"
Explanation: The email is null, so it's omitted from the output.

Example 3:

Input: { name: "Charlie", email: "", phone: undefined }
Output: "Name: Charlie"
Explanation: Both email and phone are missing (empty string and undefined respectively), so only the name is included.

Constraints

  • The formatUserDisplay function is provided (see below). You are only required to write the Jest tests.
  • The User type is defined (see below).
  • All tests must pass without errors.
  • Tests should be written in TypeScript.

Notes

  • Consider using Jest's describe and it blocks to organize your tests logically.
  • Think about the different combinations of missing properties that need to be tested.
  • Focus on testing the output of the function, not the internal implementation.
  • Use meaningful test descriptions to clearly explain what each test is verifying.
type User = {
  name: string;
  email?: string | null | undefined;
  phone?: string | null | undefined;
};

const formatUserDisplay = (user: User): string => {
  let result = `Name: ${user.name}`;

  if (user.email && user.email.trim() !== "") {
    result += `, Email: ${user.email}`;
  }

  if (user.phone && user.phone.trim() !== "") {
    result += `, Phone: ${user.phone}`;
  }

  return result;
};
Loading editor...
typescript