- Mastering React Test:Driven Development
- Daniel Irvine
- 92字
- 2021-06-24 14:45:08
Extracting an expectation group function
The three expectations in this test will be needed every time we define a new text field. A simple way to avoid repeating each expectation is to extract an arrow function that runs all three as a group:
const expectToBeInputFieldOfTypeText = formElement => {
expect(formElement).not.toBeNull();
expect(formElement.tagName).toEqual('INPUT');
expect(formElement.type).toEqual('text');
};
Define this function above your test and replace the expectations in your test with a call to this function.
In the next chapter, we'll build a Jest matcher that performs a similar function, but for a different use case.