Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulated event causes opinionated test #30

Closed
AspenJames opened this issue Apr 2, 2019 · 0 comments
Closed

Simulated event causes opinionated test #30

AspenJames opened this issue Apr 2, 2019 · 0 comments

Comments

@AspenJames
Copy link
Contributor

In src/components/TwitterMessage.js, a common pattern to make form-control change event handlers abstract is to write them as such:

handleChange = (event) => {
    this.setState({
      [event.target.id]: event.target.value
    });
  }

Then, by adding id attributes to your form elements that match the keys of your state, the handleChange function is now reusable.

This causes the following test to fail, since the simulated event does not contain any information about the event target's actual attributes:

describe("<TwitterMessage />", () => {
  describe("Saving input value in state", () => {
    it("should update the state when typing", () => {
      const wrapper = shallow(<TwitterMessage maxChars={140} />);
      const event = { target: { value: "f" } };
      wrapper.find("input").simulate("change", event);
      expect(wrapper.find("input").props().value).to.deep.equal(
        event.target.value,
        "The input value is not being updated when it changes"
      );
    });
  });

Adding an id attribute to the JSX in TwitterMessage.js as well as the simulated event will encourage setting the state more abstractly rather than explicitly with setState({ message: event.target.value})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant