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
Userinterface/type definition. - Create an
ObjectMotherclass with a staticcreateUsermethod. - The
createUsermethod should accept optional parameters to configure theUserobject's properties. If a parameter is not provided, it should use a sensible default value. - The
createUsermethod should return aUserobject. - Write at least three Jest tests to verify the
ObjectMotherclass and itscreateUsermethod. These tests should cover different configurations of theUserobject.
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
Userobject). - 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
idproperty should default to 1. - The
nameproperty should default to "Default User". - The
emailproperty should default to "default@example.com". - The
isActiveproperty should default totrue. - You must use Jest for testing.
- The solution must be written in TypeScript.
Notes
- Consider using optional parameters in the
createUsermethod to allow for flexible configuration. - Think about how to make the
ObjectMotherclass 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.