0

Consider the following code :

class Dashboard extends Component {
  state = {
    fileName: "",
    group: "",
    country: "",
    error: ""
  };
  onFormSubmit = async e => {
    e.preventDefault();
    const { group, country } = e.target;
    try {
      await axios.post("/api/dashboard", {
        name: group.value,
        country: country.value,
        image: this.state.fileName
      });
    } catch (error) {
      console.log(error.response.data.message);
      this.setState({ error: error.response.data.message });
    }
  };

  render() {
    return (
      <div>
        <form onSubmit={this.onFormSubmit}>
          <input
            type="text"
            onChange={this.onGroupChange}
            name="group"
            placeholder="Group Name"
            value={this.state.group}
          />
          {this.state.error}
          <input
            type="text"
            onChange={this.onCountryChange}
            name="country"
            placeholder="Country"
            value={this.state.country}
          />
          <ImageUpload
            fileName={this.state.fileName}
            onFileNameChange={this.onFileNameChange}
          />
          <button type="submit">Add Group</button>
        </form>
      </div>
    );
  }
}

I am trying to test the error state is set to appropriate value when post request fails. Here is the test so far:

  it("should set error state when dashboard api returns error", async () => {
    const errorMsg = "Group already exist";
    moxios.stubRequest("/api/dashboard", {
      status: 400,
      response: {
        message: errorMsg
      }
    });
    const event = {
      preventDefault: () => {},
      target: {
        group: "",
        country: ""
      }
    };
    const wrapper = shallow(<Dashboard />);
    await wrapper.find("form").simulate("submit", event);
    console.log(wrapper.state("error").value);
    expect(wrapper.state("error")).toBe(errorMsg);
  });
brass monkey
  • 5,841
  • 10
  • 36
  • 61
Unity Hour
  • 550
  • 2
  • 7
  • 22

1 Answers1

1

As the method onformSubmit is async, you can go with several approaches there:

1 Await for an async method directly:

it(
  'should set error state when dashboard api returns error',
  async () => {
    const wrapper = shallow(<Dashboard />);
    await wrapper.instance().onFormSubmit(event);
    expect(wrapper.state('error')).toBe(errorMsg);
  }
);

and separately test that the method onFormSubmit was called on submit event

2 Use timeout with returning Promise:

it(
  'should set error state when dashboard api returns error',
  () => {
    const wrapper = shallow(<Dashboard />);
    wrapper.find("form").simulate("submit", event);
    return new Promise(resolve => setTimeout(resolve, 1)).then(() => {
      expect(wrapper.state('error')).toBe(errorMsg);
    });
  },
  1000
);

3 await tick before checking:

function tick() {
  return new Promise(resolve => {
    setTimeout(resolve, 1);
  });
}

...

it('should set error state when dashboard api returns error', async () => {
  const wrapper = shallow(<Dashboard />);
  wrapper.find('form').simulate('submit', event);
  await tick();
  expect(wrapper.state('error')).toBe(errorMsg);
});

4 Use done callback:

it('should set error state when dashboard api returns error', done => {
  const wrapper = shallow(<Dashboard />);
  wrapper.find('form').simulate('submit', event);
  setImmediate(() => {
    expect(wrapper.state('error')).toBe(errorMsg);
    done();
  });
});
extempl
  • 2,987
  • 1
  • 26
  • 38
  • Test is passing no matter what happens. None of them are working. Can you provide the full code. – Unity Hour Oct 27 '18 at 04:44
  • Your first approach is totally similar to mine – Unity Hour Oct 27 '18 at 04:45
  • @UnityHour What do you mean by `passing`? Shouldn't it pass? Here is the test file code: https://pastebin.com/y7gRk8mk. Note, that I moved consts out of the `it` method, just for the ease to show the difference of approaches. – extempl Oct 27 '18 at 05:03
  • My first approach awaiting for an async method, not for a result of an event invocation. – extempl Oct 27 '18 at 05:03
  • @UnityHour added two other approaches and also updated pastebin. All of them works well. – extempl Oct 27 '18 at 05:27