ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

How can I mock error message when submit form with post method? #290

Closed namlq93 closed 4 years ago

namlq93 commented 4 years ago

The project I just joined does not use an API from the backend but must create mock data by itself through the Axios-mock-adapter. I am making a form to change the password, if the inputs entered are correct, submit it will display successfully, if the inputs are not correct, an error will be displayed (eg input is not blank, the password is not matched...) How to do that?

.onPost("/user/changepassword"), {
      // How can I pass the values of the inputs
    })
    .reply(() => {
       // How can I check the value of the inputs: empty, password input does not match...
      if (conditions are ok) {
        return [200, MOCK_RESPONSE.SUCCESS];
      } else {
        return [200, MOCK_RESPONSE.ERROR];
      }
    });
ctimmerm commented 4 years ago

It's hard to say without knowing how the form is submitted.

namlq93 commented 4 years ago

It's hard to say without knowing how the form is submitted.

Here is my code:

<input name="new_password" value={props.new_password} onChange={handleInputChange} />
<input name="confirm_password" value={props.confirmation_password} onChange={handleInputChange} />
<Button onClick={handleUserPasswordChange}></Button>

I get the values of the inputs, when the user clicks submit, how can I check those values to print the appropriate MOCK_RESPONSE?

ctimmerm commented 4 years ago

From that code I still can't see how the request is being made.