Hone logo
Hone
Problems

Testing Objects with Object Mothers in Jest (TypeScript)

Object mothers are a powerful technique for creating predictable and reusable test data. This challenge focuses on implementing object mothers in a TypeScript project and using them effectively within Jest tests. You'll create a class that generates instances of a specific object type, ensuring consistent and controlled data for your tests.

Problem Description

You are tasked with creating an ObjectMother class for a User object. The User object has the following properties: id (number), name (string), email (string), and isActive (boolean). The ObjectMother class should provide a static method createUser that generates a User object with configurable properties. The goal is to create a reliable and repeatable way to generate User objects for your Jest tests, avoiding hardcoded data within the tests themselves.

Key Requirements:

  • Create a User interface/type definition.
  • Create an ObjectMother class with a static createUser method.
  • The createUser method should accept optional parameters to configure the User object's properties. If a parameter is not provided, it should use a sensible default value.
  • The createUser method should return a User object.
  • Write at least three Jest tests to verify the ObjectMother class and its createUser method. These tests should cover different configurations of the User object.

Expected Behavior:

The createUser method should consistently generate User objects based on the provided parameters. The tests should pass, demonstrating that the object mother is functioning correctly and producing the expected data.

Edge Cases to Consider:

  • What should the default values be for each property if no parameter is provided? (Consider reasonable defaults for a User object).
  • How should you handle invalid input (e.g., a non-string value for name)? (For simplicity, you can assume valid input for this challenge, but consider it for future improvements).

Examples

Example 1:

Input: No parameters passed to createUser()
Output: { id: 1, name: "Default User", email: "default@example.com", isActive: true }
Explanation: The createUser method generates a User object with default values for all properties.

Example 2:

Input: createUser({ name: "John Doe" })
Output: { id: 1, name: "John Doe", email: "default@example.com", isActive: true }
Explanation: The createUser method generates a User object with the provided name and default values for the other properties.

Example 3:

Input: createUser({ id: 123, email: "john.doe@example.com", isActive: false })
Output: { id: 123, name: "Default User", email: "john.doe@example.com", isActive: false }
Explanation: The createUser method generates a User object with the provided id, email, and isActive, and the default name.

Constraints

  • The id property should default to 1.
  • The name property should default to "Default User".
  • The email property should default to "default@example.com".
  • The isActive property should default to true.
  • You must use Jest for testing.
  • The solution must be written in TypeScript.

Notes

  • Consider using optional parameters in the createUser method to allow for flexible configuration.
  • Think about how to make the ObjectMother class extensible to support other object types in the future.
  • Focus on creating a clean and maintainable solution. Good naming conventions and clear code structure are important.
  • The goal is to demonstrate the concept of object mothers and their benefits in testing. Don't overcomplicate the implementation.
Loading editor...
typescript