Categories
JavaScript Answers

How to reject promise and catch the error if status is not OK with fetch and JavaScript?

Sometimes, we want to reject promise and catch the error if status is not OK with fetch and JavaScript.

In this article, we’ll look at how to reject promise and catch the error if status is not OK with fetch and JavaScript.

How to reject promise and catch the error if status is not OK with fetch and JavaScript?

To reject promise and catch the error if status is not OK with fetch and JavaScript, we can check the response.ok property.

For instance, we write

const response = await fetch(url);
if (response.ok) {
  return response.json();
} else {
  throw new Error("Something went wrong");
}

to make a get request to url by calling fetch with url.

Then we check if response.ok with true from the promise returned.

If it is, then we return response body returned by response.json

Otherwise, we throw an error.

This code should be in an async function.

Conclusion

To reject promise and catch the error if status is not OK with fetch and JavaScript, we can check the response.ok property.

Categories
JavaScript Answers

How to truncate an array with JavaScript?

Sometimes, we want to truncate an array with JavaScript.

In this article, we’ll look at how to truncate an array with JavaScript.

How to truncate an array with JavaScript?

To truncate an array with JavaScript, we can use the array slice method.

For instance, we write

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr = arr.slice(0, 4);
console.log(arr);

to call arr.slice with 0 and 4 to get an array with the first 4 items of arr.

Then we assign the returned array back to arr to update it.

Conclusion

To truncate an array with JavaScript, we can use the array slice method.

Categories
JavaScript Answers

How to find element’s position relative to the document with JavaScript?

Sometimes, we want to find element’s position relative to the document with JavaScript.

In this article, we’ll look at how to find element’s position relative to the document with JavaScript.

How to find element’s position relative to the document with JavaScript?

To find element’s position relative to the document with JavaScript, we can get the sum of the top value of the element and the document’s scrollTop value.

For instance, we write

element.getBoundingClientRect().top + document.documentElement.scrollTop;

to get position of the element relative to the top of the viewport with element.getBoundingClientRect().top.

And we add the viewport offset with scrollTop and add that to top.

Conclusion

To find element’s position relative to the document with JavaScript, we can get the sum of the top value of the element and the document’s scrollTop value.

Categories
JavaScript Answers

How to mock one specific method of a class with Jest and JavaScript?

Sometimes, we want to mock one specific method of a class with Jest and JavaScript.

In this article, we’ll look at how to mock one specific method of a class with Jest and JavaScript.

How to mock one specific method of a class with Jest and JavaScript?

To mock one specific method of a class with Jest and JavaScript, we can use the jest.spyOn method.

For instance, we write

import Person from "./Person";

test("Modify only instance", () => {
  let person = new Person("foo", "bar");
  let spy = jest.spyOn(person, "sayMyName").mockImplementation(() => "Hello");

  expect(person.sayMyName()).toBe("Hello");
  expect(person.bla()).toBe("bla");
  spy.mockRestore();
});

to mock the person.sayMyName method by calling spyOn and mockImplementation in the test.

The mockImplementation callback has the faked implementation of person.sayMyName.

Likewise, we can mock the same method for all tests by writing

import Person from "./Person";

beforeAll(() => {
  jest.spyOn(Person.prototype, "sayMyName").mockImplementation(() => "Hello");
});

afterAll(() => {
  jest.restoreAllMocks();
});

test("Modify class", () => {
  let person = new Person("foo", "bar");
  expect(person.sayMyName()).toBe("Hello");
});

We call jest.spyOn with Person.prototype and 'sayMyName and mockImplementation to do the mocking in the beforeAll callback.

The beforeAll callback runs before any tests are run.

Then we call restoreAllMocks to restore all mocks after all tests are run.

Conclusion

To mock one specific method of a class with Jest and JavaScript, we can use the jest.spyOn method.

Categories
JavaScript Answers

How to fix Phantom.js not waiting for full page load with JavaScript?

Sometimes, we want to fix Phantom.js not waiting for full page load with JavaScript.

In this article, we’ll look at how to fix Phantom.js not waiting for full page load with JavaScript.

How to fix Phantom.js not waiting for full page load with JavaScript?

To fix Phantom.js not waiting for full page load with JavaScript, we can call page.render in the setTimeout callback.

For instance, we write

page.open(address, (status) => {
  if (status !== "success") {
    console.log("Unable to load the address!");
    phantom.exit();
  } else {
    window.setTimeout(() => {
      page.render(output);
      phantom.exit();
    }, 1000);
  }
});

to call page.open with the address URL string to open the page.

And then we call setTimeout with a callback that calls page.render to render the page.

We specify that we wait 1 second before rendering.

Conclusion

To fix Phantom.js not waiting for full page load with JavaScript, we can call page.render in the setTimeout callback.