Categories
JavaScript Answers

How to reset Jest mock functions calls count before every test with JavaScript?

Sometimes, we want to reset Jest mock functions calls count before every test with JavaScript.

in this article, we’ll look at how to reset Jest mock functions calls count before every test with JavaScript.

How to reset Jest mock functions calls count before every test with JavaScript?

To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks.

For instance, we write

afterEach(() => {
  local.getData.mockClear();
});

to call local.getData.mockClear to clear the mocked local.getData method after each test by calling it in the afterEach callback.

We can use

afterEach(() => {
  jest.clearAllMocks();
});

to call jest.clearAllMocks to clear all mocks after each test.

Conclusion

To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks.

Categories
JavaScript Answers

How to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript?

Sometimes, we want to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript.

In this article, we’ll look at how to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript.

How to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript?

To fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript, we can use Object.prototype.hasOwnProperty.call instead of the hasOwnProperty of an object.

For instance, we write

Object.prototype.hasOwnProperty.call(obj, prop);

to call hasOwnProperty on obj with obj as the value of this.

We pass in prop as the 2nd argument to check if prop exists in obj.

Conclusion

To fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript, we can use Object.prototype.hasOwnProperty.call instead of the hasOwnProperty of an object.

Categories
JavaScript Answers

How to use querySelectorAll only for elements that have a specific attribute set with JavaScript?

Sometimes, we want to use querySelectorAll only for elements that have a specific attribute set with JavaScript.

In this article, we’ll look at how to use querySelectorAll only for elements that have a specific attribute set with JavaScript.

How to use querySelectorAll only for elements that have a specific attribute set with JavaScript?

To use querySelectorAll only for elements that have a specific attribute set with JavaScript, we call querySelectorAll with a selector that matches the attribute name and value we’re looking for.

For instance, we write

const els = document.querySelectorAll(
  'input[value][type="checkbox"]:not([value=""])'
);

to call querySelectorAll with the value attribute of checkbox inputs that has value not equal to an empty string.

This will return a node list of all checkboxes that has value attribute not set to an empty string.

Conclusion

To use querySelectorAll only for elements that have a specific attribute set with JavaScript, we call querySelectorAll with a selector that matches the attribute name and value we’re looking for.

Categories
JavaScript Answers

How to use switch on ranges of integers in JavaScript?

Sometimes, we want to use switch on ranges of integers in JavaScript.

In this article, we’ll look at how to use switch on ranges of integers in JavaScript.

How to use switch on ranges of integers in JavaScript?

To use switch on ranges of integers in JavaScript, we use true as the value of switch and add the booleans for the range check in case.

For instance, we write

switch (true) {
  case x < 5:
    alert("less than five");
    break;
  case x < 9:
    alert("between 5 and 8");
    break;
  case x < 12:
    alert("between 9 and 11");
    break;
  default:
    alert("none");
    break;
}

to use switch with true.

And then we add the boolean expressions we want to check for case to do the range checks.

Conclusion

To use switch on ranges of integers in JavaScript, we use true as the value of switch and add the booleans for the range check in case.

Categories
JavaScript Answers

How to retrieve the text of the selected option in the select element with JavaScript?

Sometimes, we want to retrieve the text of the selected option in the select element with JavaScript.

In this article, we’ll look at how to retrieve the text of the selected option in the select element with JavaScript.

How to retrieve the text of the selected option in the select element with JavaScript?

To retrieve the text of the selected option in the select element with JavaScript, we use the selectedOptions property.

For instance, we write

const selected = document.getElementById("test").selectedOptions[0].text;

to get the select element with getElementById.

Then we get the selected options from it with selectedOptions.

We get the first selected option with [0].

And we get the text of the selected option with text.

Conclusion

To retrieve the text of the selected option in the select element with JavaScript, we use the selectedOptions property.