Categories
JavaScript Answers

How to have different return values for multiple calls on a Jasmine spy with JavaScript?

Sometimes, we want to have different return values for multiple calls on a Jasmine spy with JavaScript.

In this article, we’ll look at how to have different return values for multiple calls on a Jasmine spy with JavaScript.

How to have different return values for multiple calls on a Jasmine spy with JavaScript?

To have different return values for multiple calls on a Jasmine spy with JavaScript, we call returnValues with all the values we want to return.

For instance, we write

describe("A spy, when configured to fake a series of return values", () => {
  beforeEach(function () {
    spyOn(util, "foo").and.returnValues(true, false);
  });

  it("when called multiple times returns the requested values in order", () => {
    expect(util.foo()).toBeTruthy();
    expect(util.foo()).toBeFalsy();
    expect(util.foo()).toBeUndefined();
  });
});

to mock the util.foo method with spyOn in the beforeEach hook to create the mock before each test.

And then we call returnValues with all the values we want to return.

Then in the test, we call expect to check for each value returned by util.foo.

Conclusion

To have different return values for multiple calls on a Jasmine spy with JavaScript, we call returnValues with all the values we want to return.

Categories
JavaScript Answers

How to get the text node of an element with JavaScript?

Sometimes, we want to get the text node of an element with JavaScript.

In this article, we’ll look at how to get the text node of an element with JavaScript.

How to get the text node of an element with JavaScript?

To get the text node of an element with JavaScript, we call array find to find the text node in a node.

For instance, we write

const extract = (node) => {
  const text = [...node.childNodes].find(
    (child) => child.nodeType === Node.TEXT_NODE
  );
  return text?.textContent?.trim();
};

to create the extract function that takes the node and get the child nodes with node.childNodes.

Then we spread the child nodes into an array.

And we call find with a callback that gets the nodeType with type Node.TEXT_NODE.

Finally, we get the textContent of the text node.

Conclusion

To get the text node of an element with JavaScript, we call array find to find the text node in a node.

Categories
JavaScript Answers

How to use Axios interceptors with JavaScript?

Sometimes, we want to use Axios interceptors with JavaScript.

In this article, we’ll look at how to use Axios interceptors with JavaScript.

How to use Axios interceptors with JavaScript?

To use Axios interceptors with JavaScript, we call axios.interceptors.request.use to add a request interceptor.

And we call axios.interceptors.response.use to add a response interceptor.

For instance, we write

axios.interceptors.request.use(
  (config) => {
    if (DEBUG) {
      console.info(config);
    }
    return config;
  },
  (error) => {
    if (DEBUG) {
      console.error(error);
    }
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  (response) => {
    if (response.config.parse) {
      // ...
    }
    return response;
  },
  (error) => {
    return Promise.reject(error.message);
  }
);

to call both methods to add a request and response interceptor.

We call axios.interceptors.request.use with a callback to do something with the request config and return it.

The 2nd argument is a function that handlers request errors.

Then we call axios.interceptors.response.use with a function that gets the response and do something with it.

The 2nd argument is a function that handlers error responses and return a rejected promise with the error content.

Conclusion

To use Axios interceptors with JavaScript, we call axios.interceptors.request.use to add a request interceptor.

And we call axios.interceptors.response.use to add a response interceptor.

Categories
JavaScript Answers

How to force cancel a promise with JavaScript?

Sometimes, we want to force cancel a promise with JavaScript.

In this article, we’ll look at how to force cancel a promise with JavaScript.

How to force cancel a promise with JavaScript?

To force cancel a promise with JavaScript, we use the AbortController constructor.

For instance, we write

const controller = new AbortController();

const task = new Promise((resolve, reject) => {
  //...
  controller.signal.addEventListener("abort", () => {
    reject();
  });
});

controller.abort();

to create an AbortController object.

Then we create the promise with the Promise constructor called with a callback.

In it, we listen to the abort controller’s abort event by calling controller.signal.addEventListener in the task promise.

We call it with a callback that calls reject to reject the promise, which stops the promise from running.

Then we call controller.abort to cancel the promise, which triggers the abort event on the controller.

Conclusion

To force cancel a promise with JavaScript, we use the AbortController constructor.

Categories
JavaScript Answers

How to get the number of elements in a JavaScript object?

Sometimes, we want to get the number of elements in a JavaScript object.

In this article, we’ll look at how to get the number of elements in a JavaScript object.

How to get the number of elements in a JavaScript object?

To get the number of elements in a JavaScript object, we can use the Object.keys method.

For instance, we write

const count = Object.keys(obj).length;

to call Object.keys with obj to get an array with the key strings in obj.

Then we use the length property to get the number of properties in obj.

Conclusion

To get the number of elements in a JavaScript object, we can use the Object.keys method.