dimanche 2 août 2020

React Jest Testing Issue

I'm learning some testing with React and Jest, and I have some issues which I need some guidance on.

I have a logout method in my react component which looks like this:

  doLogout = async () => {
    const url = "/api/logout";

    let response;

    try {
      response = await fetch(url, { method: "post" });
    } catch (err) {
      alert("Failed to connect to server: " + err);
      return;
    }

    if (response.status !== 204) {
      alert("Error when connecting to server: status code " + response.status);
      return;
    }

my test report says I have some uncovered lines in this method, which I would like to cover:

headerbar.jsx |    78.95 |       75 |      100 |    78.95 |       21,22,26,27

Line 21: alert("Failed to connect to server: " + err);

Line 22: return;

Line 26: alert("Error when connecting to server: status code " + response.status);

Line 27: return;

All these lines are from the code above, I'm new to testing and I would like some guidance on these lines and how could I cover them in testing.

Aucun commentaire:

Enregistrer un commentaire