Categories
JavaScript Answers

How to use Jest to spy on a method call with JavaScript?

Sometimes, we want to use Jest to spy on a method call with JavaScript.

In this article, we’ll look at how to use Jest to spy on a method call with JavaScript.

How to use Jest to spy on a method call with JavaScript?

To use Jest to spy on a method call with JavaScript, we call the spyOn method.

For instance, we write

const spy = jest.spyOn(Component.prototype, "methodName");
const wrapper = mount(<Component {...props} />);
wrapper.instance().methodName();
expect(spy).toHaveBeenCalled();

in our test to call spyOn to add a spy to the Component‘s methodName instance method.

The we call the methodName method on the component.

And we check if it’s called with with toHaveBeenCalled.

Conclusion

To use Jest to spy on a method call with JavaScript, we call the spyOn method.

Categories
JavaScript Answers

How to split and modify a string in Node.js and JavaScript?

Sometimes, we want to split and modify a string in Node.js and JavaScript.

In this article, we’ll look at how to split and modify a string in Node.js and JavaScript.

How to split and modify a string in Node.js and JavaScript?

To split and modify a string in Node.js and JavaScript, we use the split and map methods.

For instance, we write

const str = "123, 124, 234,252";
const arr = str.split(",").map((val) => Number(val) + 1);
console.log(arr);

to call str.split to split the string into a string array by the comma.

Then we call map with a callback to map each value to the value we want.

Conclusion

To split and modify a string in Node.js and JavaScript, we use the split and map methods.

Categories
JavaScript Answers

How to toggle display: none style with JavaScript?

Sometimes, we want to toggle display: none style with JavaScript.

In this article, we’ll look at how to toggle display: none style with JavaScript.

How to toggle display: none style with JavaScript?

To toggle display: none style with JavaScript, we set the style.display property.

For instance, we write

const yourUl = document.getElementById("yourUlId");
yourUl.style.display = yourUl.style.display === "none" ? "" : "none";

to select the element to modify with getElementById.

Then we set its style.display property to '' is it’s currently 'none'.

Otherwise, we set it to 'none'.

Conclusion

To toggle display: none style with JavaScript, we set the style.display property.

Categories
JavaScript Answers

How to flatten an array of arrays of objects with JavaScript?

To flatten an array of arrays of objects with JavaScript, we use the array flat method.

For instance, we write

const flattenedArr = [["object1"], ["object2"]].flat();

to return a flattened version of the nested array with flat.

Conclusion

To flatten an array of arrays of objects with JavaScript, we use the array flat method.

Categories
Angular Answers

How to load JSON from local file with http.get() in Angular and TypeScript?

Sometimes, we want to load JSON from local file with http.get() in Angular and TypeScript.

In this article, we’ll look at how to load JSON from local file with http.get() in Angular and TypeScript.

How to load JSON from local file with http.get() in Angular and TypeScript?

To load JSON from local file with http.get() in Angular and TypeScript, we call get with the relative path to the JSON.

For instance, we write

//...
export class AppComponent {
  private URL = "./assets/navItems.json";
  //...

  constructor(private httpClient: HttpClient) {}

  ngOnInit(): void {
    this.httpClient
      .get(this.URL)
      .subscribe((navItems) => (this.navItems = navItems));
  }
}

to call httpClient.get with the URL to the JSON.

Then we call subscribe with a callback to get the navItems response and set it as the value of this.navItems.

Conclusion

To load JSON from local file with http.get() in Angular and TypeScript, we call get with the relative path to the JSON.