Categories
JavaScript Answers

How to modify Jasmine spies based on arguments with JavaScript?

Sometimes, we want to modify Jasmine spies based on arguments with JavaScript.

In this article, we’ll look at how to modify Jasmine spies based on arguments with JavaScript.

How to modify Jasmine spies based on arguments with JavaScript?

To modify Jasmine spies based on arguments with JavaScript, we can use the withArgs method.

For instance, we write

describe("user test", () => {
  it("gets user name and ID", () => {
    spyOn(externalApi, "get")
      .withArgs("abc")
      .and.returnValue("Jane")
      .withArgs("123")
      .and.returnValue(98765);
  });
});

to call withArgs with the arguments that we want to call externalApi.get with.

And then we call returnValue to mock the returned value given the argument we called withArgs with.

And we call withArgs and returnValue a 2nd time to call externalApi.get with the 2nd argument and mock its returned value given the argument.

Conclusion

To modify Jasmine spies based on arguments with JavaScript, we can use the withArgs method.

Categories
JavaScript Answers

How to display date/time in user’s locale format and time offset with JavaScript?

Sometimes, we want to display date/time in user’s locale format and time offset with JavaScript.

In this article, we’ll look at how to display date/time in user’s locale format and time offset with JavaScript.

How to display date/time in user’s locale format and time offset?

To display date/time in user’s locale format and time offset, we can use the DateTimeFormat object’s format method.

For instance, we write

const utcDate = new Date(Date.UTC(2022, 11, 20, 3, 23, 16, 738));
console.log(new Intl.DateTimeFormat().format(utcDate));

to create a UTC timestamp with Date.UTC.

And then we convert the timestamp to a DFate object with the Date constructor.

Next, we call format with utcDate to return a date string formatted to the user’s current locale.

Conclusion

To display date/time in user’s locale format and time offset, we can use the DateTimeFormat object’s format method.

Categories
JavaScript Answers

How to detect if browser tab has focus with JavaScript?

Sometimes, we want to detect if browser tab has focus with JavaScript.

In this article, we’ll look at how to detect if browser tab has focus with JavaScript.

How to detect if browser tab has focus with JavaScript?

To detect if browser tab has focus with JavaScript, we set window.onfocus and the window.onblur properties to event listener functions for the focus and blur events.

For instance, we write

let focused = true;

window.onfocus = () => {
  focused = true;
};
window.onblur = () => {
  focused = false;
};

to set window.onfocus and window.onblur to the focus and blur event listener functions.

When the window gets focus, then onfocus is run.

And when we switch to another window, onblur is run.

Conclusion

To detect if browser tab has focus with JavaScript, we set window.onfocus and the window.onblur properties to event listener functions for the focus and blur events.

Categories
React Answers

How to use Redirect in React react-router-dom v5?

Sometimes, we want to use Redirect in React react-router-dom v5.

In this article, we’ll look at how to use Redirect in React react-router-dom v5.

How to use Redirect in React react-router-dom v5?

To use Redirect in React react-router-dom v5, we call history.push.

For instance, we write

import { useHistory } from "react-router-dom";

function HomeButton() {
  const history = useHistory();

  const handleClick = () => {
    history.push("/home");
  };

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  );
}

to call the useHistory hook to return the history object.

Then we call history.push to go to the '/home' URL.

And then we set onClick prop to handleClick to do the redirect when we click the button.

Conclusion

To use Redirect in React react-router-dom v5, we call history.push.

Categories
JavaScript Answers

How to include text in middle of date format with Moment.js and JavaScript?

Sometimes, we want to include text in middle of date format with Moment.js and JavaScript.

In this article, we’ll look at how to include text in middle of date format with Moment.js and JavaScript.

How to include text in middle of date format with Moment.js and JavaScript?

To include text in middle of date format with Moment.js and JavaScript, we escape it with square brackets.

For instance, we write

const s = moment().format("MMM. D, YYYY [at] h:mm A z");

to call format with "MMM. D, YYYY [at] h:mm A z" to return a datetime string with ‘at’ between the date and time.

Conclusion

To include text in middle of date format with Moment.js and JavaScript, we escape it with square brackets.