Categories
JavaScript Answers

How to test file inputs with Cypress and JavaScript?

Sometimes, we want to test file inputs with Cypress and JavaScript.

In this article, we’ll look at how to test file inputs with Cypress and JavaScript.

How to test file inputs with Cypress and JavaScript?

To test file inputs with Cypress and JavaScript, we use the cypress-file-upload package.

To install it, we run

npm install --save-dev cypress-file-upload

Then we use it by writing

import "cypress-file-upload";

const fixtureFile = "photo.png";
cy.get('[data-cy="file-input"]').attachFile(fixtureFile);

to call cy.get to select the file input with the data-cy attribute set to file-input.

And then we call attachFile to set the file input to the fixtureFile path.

Conclusion

To test file inputs with Cypress and JavaScript, we use the cypress-file-upload package.

Categories
JavaScript Answers

How to get a random number between 0.0200 and 0.120 with JavaScript?

Sometimes, we want to get a random number between 0.0200 and 0.120 with JavaScript.

In this article, we’ll look at how to get a random number between 0.0200 and 0.120 with JavaScript.

How to get a random number between 0.0200 and 0.120 with JavaScript?

To get a random number between 0.0200 and 0.120 with JavaScript, we use the Math.random method.

For instance, we write

const n = (Math.random() * (0.12 - 0.02) + 0.02).toFixed(4);

to use Math.random() * (0.12 - 0.02) + 0.02 to return a number between 0.02 and 0.12.

Then we call toFixed with 4 to round the returned number to 4 decimal places.

Conclusion

To get a random number between 0.0200 and 0.120 with JavaScript, we use the Math.random method.

Categories
JavaScript Answers

How to filter on array of RXJS Observable with JavaScript?

Sometimes, we want to filter on array of RXJS Observable with JavaScript.

In this article, we’ll look at how to filter on array of RXJS Observable with JavaScript.

How to filter on array of RXJS Observable with JavaScript?

To filter on array of RXJS Observable with JavaScript, we use the filter function.

For instance, we write

import { filter } from "rxjs/operators";

const filteredEpicsObs = getEpics()
  .pipe(filter((epic) => epic.id === id))
  .subscribe((x) => {
    //...
  });

to call pipe on the observable returned by getEpics.

We call pipe with the filter returned by filter, which is called with a callback to match the values with the given id.

Then we get the filtered results from the subscribe callback.

Conclusion

To filter on array of RXJS Observable with JavaScript, we use the filter function.

Categories
JavaScript Answers

How to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript?

Sometimes, we want to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript.

In this article, we’ll look at how to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript.

How to remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript?

To remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript, we call replace with a regex.

For instance, we write

const newString = str.replace(/[^A-Z0-9]/gi, "_");

to call str.replace with a regex that matches anything that aren’t alphanumeric characters.

The g flag makes replace replace all matches.

And i makes replace find matches in a case-insensitive manner.

We replace the matches with underscores.

Conclusion

To remove special symbols and extra spaces and replace with underscore using the replace method with JavaScript, we call replace with a regex.

Categories
JavaScript Answers

How to round a number in JavaScript?

Sometimes, we want to round a number in JavaScript.

In this article, we’ll look at how to round a number in JavaScript.

How to round a number in JavaScript?

To round a number in JavaScript, we use the Math.round method.

For instance, we write

const rounded = Math.round(332.24);

to call Math,round with the number we want to round.

It returns the rounded number as an integer.

Conclusion

To round a number in JavaScript, we use the Math.round method.