Skip to content

Conversation

@yutin1987
Copy link

#823 simulate events returning promises

@yutin1987
Copy link
Author

2017-04-02 12 40 52

@nfcampos
Copy link
Collaborator

nfcampos commented Apr 1, 2017

I don't think I agree with this. Always returning a promise from simulate seems to suggest the event system is somehow asynchronous (which it itsn't) and users of the library should always wait for the promise to resolve before proceeding with their tests.

For the use case you exemplify in the test you added, why not do this?

    it('should simulate promise events', (done) => {
      class Foo extends React.Component {
        ...
      }

      const wrapper = shallow(<Foo />);

      expect(wrapper.find('.clicks-0').length).to.equal(1);
      wrapper.prop('onClick')().then(() => {
        expect(wrapper.find('.clicks-1').length).to.equal(1);
        done();
      });
    });

ie. select the value of the onClick prop and call it directly

@ljharb
Copy link
Member

ljharb commented Apr 1, 2017

Events are not appropriate to model as Promises. Events happen multiple times; a promise resolves once.

Also, yes, events are synchronous in the browser.

You can wrap yourself with new Promise to avoid the done pattern.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants