jasmine-react icon indicating copy to clipboard operation
jasmine-react copied to clipboard

I Can't do work. jasmineReact.spyOnClass();

Open ericzwong opened this issue 9 years ago • 4 comments

I get this error: "A component constructor could not be found for this class. Are you sure you passed in a the component definition for a React component? thrown"

and i use karma + babel

var React = require('react'); var TestUtils = require('react-addons-test-utils'); var ReactDOM = require('react-dom'); var rewire = require('rewire'); var jasmineReact = require('jasmine-react-helpers'); // var HelloRandom = require('../components/helloworld');

class HelloRandom extends React.Component { getRandomAuthor() { let names = [ 'aliceyang', 'wangzhe', 'bill', 'cook' ]; return names[Math.floor(Math.random() * names.length)]; } render() { let name = this.getRandomAuthor(); return (

{ name }
); } };

describe("Hello Random", function () { it("should spy on a function of react class", function () { jasmineReact.spyOnClass(HelloRandom, 'getRandomAuthor').andReturn('wangzhe');

let HelloRandomComponent = TestUtils.renderIntoDocument(<HelloRandom/>);
expect(ReactDOM.findDOMNode(HelloRandomComponent).innerHTML).toEqual('wangzhe');

}); });

ericzwong avatar Oct 30 '15 14:10 ericzwong

I'm getting the same error with ES6/Babel.

xhawk avatar Dec 29 '15 12:12 xhawk

ditto.

afraser avatar Jan 07 '16 14:01 afraser

It looks like this isn't compatible with React 0.14.X

I resolved by using TestUtils.renderIntoDocument and ReactDOM.findDOMNode to get the React component and DOM element.

jolyonruss avatar Feb 01 '16 18:02 jolyonruss

Same problem here, any work-arounds? I'm trying to use this to test methods like componentDidMount or other custom internal component functions.

icd2k3 avatar Mar 22 '16 17:03 icd2k3