Hone logo
Hone
Problems

Validating User Input with Jest

Ensuring user input is valid is crucial for building robust and reliable applications. This challenge focuses on creating a set of Jest tests to validate a simple user input function, verifying that it correctly handles various input scenarios and throws appropriate errors when invalid input is provided. This exercise will solidify your understanding of Jest testing and input validation best practices in TypeScript.

Problem Description

You are tasked with creating Jest tests for a TypeScript function called validateUserInput. This function takes a string as input and performs the following validations:

  1. Not Empty: The input string must not be empty.
  2. Contains Only Letters and Numbers: The input string must contain only letters (a-z, A-Z) and numbers (0-9). No special characters or spaces are allowed.
  3. Minimum Length: The input string must be at least 5 characters long.

The validateUserInput function should:

  • Return the input string if it passes all validations.
  • Throw an Error with a descriptive message if any of the validations fail. The error messages should be clear and indicate which validation failed.

You need to write Jest tests to cover the following scenarios:

  • Valid Input: Test cases with strings that pass all validations.
  • Empty Input: Test cases with empty strings.
  • Invalid Characters: Test cases with strings containing special characters or spaces.
  • Too Short: Test cases with strings shorter than 5 characters.
  • Combination of Errors: Test cases that trigger multiple validation errors simultaneously.

Examples

Example 1:

Input: "Valid123"
Output: "Valid123"
Explanation: The input string is not empty, contains only letters and numbers, and is at least 5 characters long.

Example 2:

Input: ""
Output: Error: Input cannot be empty.
Explanation: The input string is empty, violating the "Not Empty" validation.

Example 3:

Input: "Short"
Output: Error: Input must be at least 5 characters long.
Explanation: The input string is shorter than 5 characters, violating the "Minimum Length" validation.

Example 4:

Input: "Invalid!@#"
Output: Error: Input must contain only letters and numbers.
Explanation: The input string contains special characters, violating the "Contains Only Letters and Numbers" validation.

Example 5:

Input: "  Space "
Output: Error: Input must contain only letters and numbers.
Explanation: The input string contains spaces, violating the "Contains Only Letters and Numbers" validation.

Constraints

  • The validateUserInput function is provided below. Do not modify this function.
  • You must use Jest for testing.
  • All tests must pass.
  • Error messages must match the expected messages exactly.
  • The input to validateUserInput will always be a string.

Notes

  • Consider using expect().toThrow() in your Jest tests to verify that the function throws errors with the correct messages.
  • Think about how to structure your tests to cover all the different validation scenarios efficiently.
  • Pay close attention to the error messages; they are a key part of the validation process.
function validateUserInput(input: string): string {
  if (!input) {
    throw new Error("Input cannot be empty.");
  }

  if (input.length < 5) {
    throw new Error("Input must be at least 5 characters long.");
  }

  if (!/^[a-zA-Z0-9]+$/.test(input)) {
    throw new Error("Input must contain only letters and numbers.");
  }

  return input;
}
Loading editor...
typescript