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
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.

Categories
JavaScript Answers

How to use window.location to open URL in a new tab with JavaScript?

Sometimes, we want to use window.location to open URL in a new tab with JavaScript.

In this article, we’ll look at how to use window.location to open URL in a new tab with JavaScript.

How to use window.location to open URL in a new tab with JavaScript?

To use window.location to open URL in a new tab with JavaScript, we call window.open with '_blank' as the 2nd argument.

For instance, we write

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

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

Conclusion

To use window.location to open URL in a new tab with JavaScript, we call window.open with '_blank' as the 2nd argument.