I wrote jsFiddle using a React Base Fiddle (JSX) to find out what happened (I created my own "spy" that just went to the console when called).
I found out that the reason you cannot find your button is because it does not exist where you might expect it.
The modular Bootstrap component ( <Modal/> ) is actually contained in the React-Overlays modular component (called BaseModal in the code that is from here ). This, in turn, displays a component called Portal , the render method simply returns null . This is the null value that you are trying to find the displayed components.
Due to the fact that modal is not used in the traditional way of React, React cannot see the modal to use TestUtils on. A completely separate child <div/> node is placed in the body of the document , and this new <div/> used to create the modal.
So, so that you can simulate a click using React TestUtils (the button click handler is still attached to the button click event), you can use standard JS methods to find the DOM. Set up your test as follows:
describe('MyModal.jsx', function() { it('tests the Close Button', function() { var spy = sinon.spy(); var MyModalComponent = TestUtils.renderIntoDocument( <MyModal show onHide={spy}/> );
The getElementsByClassName function returns a collection of elements with this class, so you must transfer the first (and to your test case, your only one) from each collection.
Your test should now pass ^ _ ^
source share