ZPPP
ZPPP

Reputation: 1578

How to test fetch API class with JEST?

static get(action, params = {}) {

        return new Promise((resolve, reject) => {
          fetch(UrlHelper.apiUrl(action, params), {
            credentials: 'same-origin'
          })
            .then(response => {
              return Api.checkStatus(resolve, reject, response)
            })
            .then(([ ok, response ]) => {
              return ok ? resolve(response) : reject(response);
            });
        });
     }

I don't fully understand what I need to test in this case.

What I need to test here? Promise ?

Upvotes: 0

Views: 229

Answers (1)

vinoth s
vinoth s

Reputation: 198

   var request = require("request");



describe("firt get api testing", () => {
  test("status code", () => {
    request("http://your api", function(
      error,
      response,
      body
    ) {
      var obj = JSON.parse(response.body);
      expect(response.statusCode).toBe(200);
      expect(obj[0].name).toBe("Karthika Sri");
      expect(obj[0].name).toMatch(/[a-z]/);
      //  expect(response.type).toBe("application/json");
      console.log("obj", obj[0].name);
    });
  });
});

Upvotes: 1

Related Questions