Questions

  1. Let's say we are implementing a Jest test and we have a variable called result, which we want to check isn't null. How can we do this with Jest matcher functions?
  1. Let's say we have a variable called person that is of type IPerson:
interface IPerson {
id: number;
name: string;
}

We want to check that the person variable is { id: 1, name: "bob" }. How can we do this with Jest matcher functions?

  1. Is it possible to carry out our check in the last question with a Jest snapshot test? If so, how?
  2. We have implemented a component called CheckList which renders text from an array in a list. Each list item has a checkbox so that the user can select list items. The component has a function prop called onItemSelect that is called when a user selects an item by checking the checkbox. We are implementing a test to verify that the onItemSelect prop works. The following line of code renders the component in the test:
const { container } = render(<SimpleList data={["Apple", "Banana", "Strawberry"]} onItemSelect={handleListItemSelect} />);

How can we use a Jest mock function for handleListItemSelect and check that it is called?

  1. In the implementation of SimpleList in the last question, the onItemSelect function takes in a parameter called item, which is the string value that the user has selected. In our test, let's pretend we have already simulated the user selecting Banana. How can we check the onItemSelect function was called, with the item parameter being Banana?
  1. In the implementation of SimpleList in the last two questions, the text is displayed using a label that is tied to the checkbox using the for attribute. How can we use functions in react-testing-library to firstly locate the Banana checkbox and then check it?
  2. In this chapter, we found that the coverage was low in our code that rendered posts from the JSONPlaceholder REST API. One of the areas that wasn't covered was handling HTTP error codes in the componentDidMount function when we get the posts from the REST API. Create a test to cover this area of code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.238.20