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
CSS

How to insert ellipsis (…) into HTML tag if content too wide with CSS?

Sometimes, we want to insert ellipsis (…) into HTML tag if content too wide with CSS.

In this article, we’ll look at how to insert ellipsis (…) into HTML tag if content too wide with CSS.

How to insert ellipsis (…) into HTML tag if content too wide with CSS?

To insert ellipsis (…) into HTML tag if content too wide with CSS, we can set a few CSS properties.

For instance, we write

.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
}

to set the elements with class ellipsis to have various styles.

We set white-space to nowrap to stop text from wrapping.

overflow is set to hidden to hide overflowing text.

text-overflow is set to ellipsis to truncate overflowing text and add a ellipsis after the end of the truncated text.

The styles will be applied when the selected element has a set width.

Conclusion

To insert ellipsis (…) into HTML tag if content too wide with CSS, we can set a few CSS properties.