Search code examples
reactjsreactjs-fluxfluxjestjs

How do I test a ReactJS Flux Action (in Jest) from the component test?


I have a component that on the click of a button calls a flux action:

onSend(event) {
  MessageActions.sendMessage(this.state.currentMessage);
};

Now, I want the test to basically be able to simulate the button click (I know how to do that).

And on the button click check to see if that "MessageActions.sendMessage" function is called with a given string.

How do I access MessageActions from my test?

The object I'm testing is called ChatMessage, but the MessageActions is defined outside the React.CreateClass code.

I've tried things in my test like:

var messageActions = require('myactions');
expect(messageActions.sendMessage).toBeCalled();

and

expect(ChatMessage.MessageActions.sendMessage).toBeCalled();

But nothing is working as it just says MessageActions is undefined.

The ReactJS docs have a (crappy) example of testing a store but nothing about how you test Actions used within your component.

Any help please


Solution

  • This should work:

    expect(MessageActions.sendMessage).toBeCalled();

    Just make sure to declare MessageActions

    var MessageActions = require('path/to/MessageActions');

    I suspect you're getting the undefined error because you've forgotten the above declaration within the "describe" function.