Categories
JavaScript Answers

How to test private functions in unit tests with Mocha and Node.js?

Sometimes, we want to test private functions in unit tests with Mocha and Node.js.

In this article, we’ll look at how to test private functions in unit tests with Mocha and Node.js.

How to test private functions in unit tests with Mocha and Node.js?

To test private functions in unit tests with Mocha and Node.js, we can use the rewire module.

To install it, we run

npm i rewire

Then we use it by writing

const rewire = require("rewire");
const foobar = rewire("./foobar");

describe("private_foobar1", () => {
  const privateFoobar1 = foobar.__get__("privateFoobar1");

  it("should do stuff", (done) => {
    const stuff = privateFoobar1(filter);
    should(stuff).be.ok;
  });
});

to import the ./foobar module that we want to test.

And then we use __get__ to get the privateFoobar1 function from ./foobar.

Then we call the returned privateFoobar1 in our test.

Conclusion

To test private functions in unit tests with Mocha and Node.js, we can use the rewire module.

Categories
JavaScript Answers

How to get the pure text without HTML element using JavaScript?

Sometimes, we want to get the pure text without HTML element using JavaScript.

In this article, we’ll look at how to get the pure text without HTML element using JavaScript.

How to get the pure text without HTML element using JavaScript?

To get the pure text without HTML element using JavaScript, we can use the innerText or textContent properties.

For instance, we write

const element = document.getElementById("txt");
const text = element.innerText || element.textContent;

to select the element with getElementById.

And then we use the innerText or textContent properties to get the text content of the element.

Conclusion

To get the pure text without HTML element using JavaScript, we can use the innerText or textContent properties.

Categories
JavaScript Answers

How to get selected value/text from select on change with JavaScript?

Sometimes, we want to get selected value/text from select on change with JavaScript.

In this article, we’ll look at how to get selected value/text from select on change with JavaScript.

How to get selected value/text from select on change with JavaScript?

To get selected value/text from select on change with JavaScript, we can use the select element’s value property.

For instance, we write

<select id="select_id">
  <option value="0">-Select-</option>
  <option value="1">Communication</option>
</select>

to add a select drop down.

Then we write

const d = document.getElementById("select_id");
d.onchange = () => {
  console.log(d.value);
};

to select the select drop down with getElementById.

And then we set d.onchange to a function that runs when the drop down selection changes.

In it, we get the value attribute value of the selected option element with d.value.

Conclusion

To get selected value/text from select on change with JavaScript, we can use the select element’s value property.

Categories
JavaScript Answers

How to add target=”_blank” to JavaScript window.location with JavaScript?

Sometimes, we want to add target="_blank" to JavaScript window.location with JavaScript.

In this article, we’ll look at how to add target="_blank" to JavaScript window.location with JavaScript.

How to add target="_blank" to JavaScript window.location with JavaScript?

To add target="_blank" to JavaScript window.location with JavaScript, we call window.open with '_blank'.

For instance, we write

window.open("http://www.example.com", "_blank");

to call window.open with the URL we want to open and '_blank' to open the URL in a new tab or window.

Conclusion

To add target="_blank" to JavaScript window.location with JavaScript, we call window.open with '_blank'.

Categories
JavaScript Answers

How to stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?

Sometimes, we want to stop a web page from scrolling to the top when a link is clicked that triggers JavaScript.

In this article, we’ll look at how to stop a web page from scrolling to the top when a link is clicked that triggers JavaScript.

How to stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?

To stop a web page from scrolling to the top when a link is clicked that triggers JavaScript, we call preventDefault in the link’s click handler.

For instance, we write

document.getElementById("#ma_link").addEventListener("click", (e) => {
  e.preventDefault();
  doSomething();
});

to select the link with getElementById.

Then we call addEventListener with 'click' and a click event handler callback.

In the callback, we call e.preventDefault to stop the default behavior of scrolling to the top of the page when the link is clicked.

Conclusion

To stop a web page from scrolling to the top when a link is clicked that triggers JavaScript, we call preventDefault in the link’s click handler.