0

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

Cihan Keser
  • 3,190
  • 4
  • 30
  • 43
TheStoneFox
  • 3,007
  • 3
  • 31
  • 47

1 Answers1

1

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.

vkrams
  • 7,267
  • 17
  • 79
  • 129
todsul
  • 26
  • 1
  • Hi, I've already tried what you're suggesting. If you look at the code on the github repo, maybe you can see if I'm doing something wrong... Class = https://raw.githubusercontent.com/thestonefox/react-chat/development/scripts/components/chat_message.js and the test for it is = https://raw.githubusercontent.com/thestonefox/react-chat/development/__tests__/scripts/components/chat_message-test.js – TheStoneFox Feb 19 '15 at 07:25
  • Nevermind, I see what you mean now, even the React = require() needs to be in the beforeEach otherwise it doesn't work! Learn something new! :) – TheStoneFox Feb 19 '15 at 17:15