Categories
JavaScript Answers

How to listen to input value change with JavaScript?

Sometimes, we want to listen to input value change with JavaScript.

In this article, we’ll look at how to listen to input value change with JavaScript.

How to listen to input value change with JavaScript?

To listen to input value change with JavaScript, we can call addEventListener to add the change event listener.

For instance, we write

<input type="text" name="thing" value="" />

to add an input.

Then we write

const doThing = (e) => {
  console.log(e.target.value);
};

document.getElementsByName("thing")[0].addEventListener("change", doThing);

to select the input by its name attribute value with

document.getElementsByName("thing")[0]

Then we call addEventListener to listen to the change event with the doThing function, which is emitted when we change the input value.

In doThing, we get the input value with e.target.value.

Conclusion

To listen to input value change with JavaScript, we can call addEventListener to add the change event listener.

Categories
JavaScript Answers

How to fix cannot set property of undefined with JavaScript?

Sometimes, we want to fix cannot set property of undefined with JavaScript.

In this article, we’ll look at how to fix cannot set property of undefined with JavaScript.

How to fix cannot set property of undefined with JavaScript?

To fix cannot set property of undefined with JavaScript, we should make sure we’re setting a property on an object.

For instance, we write

d = {
  [a]: {
    greetings: b,
    data: c,
  },
};

d[a]["greeting"] = "hello";

to assign d to an object.

Then we can set d[a]["greeting"] to 'hello' since d[a] is an object that has the greetings property.

Conclusion

To fix cannot set property of undefined with JavaScript, we should make sure we’re setting a property on an object.

Categories
JavaScript Answers

How to properly make mock throw an error in Jest?

Sometimes, we want to properly make mock throw an error in Jest.

In this article, we’ll look at how to properly make mock throw an error in Jest.

How to properly make mock throw an error in Jest?

To properly make mock throw an error in Jest, we call the mockImplementation method and throw an error in the callback we call the method with.

For instance, we write

it("should throw error if email not found", async () => {
  callMethod
    .mockImplementation(() => {
      throw new Error("User not found [403]");
    })
    .mockName("callMethod");

  const query = FORGOT_PASSWORD_MUTATION;
  const params = { email: "user@example.com" };
  const result = await simulateQuery({ query, params });

  console.log(result);
  expect(callMethod()).rejects.toMatch("User not found [403]");
});

to call callMethod.mockImplementation with a callback that throws and error.

Then we use

expect(callMethod()).rejects.toMatch("error");

to check the callMethod throws an error with the given content.

Conclusion

To properly make mock throw an error in Jest, we call the mockImplementation method and throw an error in the callback we call the method with.

Categories
JavaScript Answers

How to add optional function in a TypeScript interface?

Sometimes, we want to add optional function in a TypeScript interface.

In this article, we’ll look at how to add optional function in a TypeScript interface.

How to add optional function in a TypeScript interface?

To add optional function in a TypeScript interface, we add a ? after the function name.

For instance, we write

interface IElement {
  name: string;
  options: any;
  type: string;
  value?: string;
  validation?(any): boolean;
}

to make the validation function optional by putting a ? after the function name.

Conclusion

To add optional function in a TypeScript interface, we add a ? after the function name.

Categories
JavaScript Answers

How to send a JSON to server and retrieving a JSON in return with JavaScript?

Sometimes, we want to send a JSON to server and retrieving a JSON in return with JavaScript

In this article, we’ll look at how to send a JSON to server and retrieving a JSON in return with JavaScript.

How to send a JSON to server and retrieving a JSON in return with JavaScript?

To send a JSON to server and retrieving a JSON in return with JavaScript, we use fetch.

For instance, we write

const dataToSend = JSON.stringify({
  email: "hey@mail.com",
  password: "101010",
});

const resp = await fetch(url, {
  credentials: "same-origin",
  mode: "same-origin",
  method: "post",
  headers: { "Content-Type": "application/json" },
  body: dataToSend,
});
const dataReceived = await resp.json();

to call fetch to make a post request to the url in an async function.

We call fetch with the headers and body to send the request headers and body.

And then we get the JSON response body from the resp.json method.

Conclusion

To send a JSON to server and retrieving a JSON in return with JavaScript, we use fetch.