Attempting to test a React component with jasmine-react

1.3k views Asked by At

This question is very similar to one of the solutions to this question.

I'm using this as my example as its similar to my real problem but nicely distilled.

The following test produces this result:

Warning: Comp.type is deprecated. Use Comp directly to access the class.

 Error: Expected a spy, but got undefined.

The test:

it("should call plop method on render", function(){

      var Comp = React.createClass({

        displayName: "Comp",

        plop: function() {
          console.log("plop");
        },

        render: function() {
          this.plop();
          return (<div></div>);
        }
      });


      //spy on method
      jasmineReact.spyOnClass(Comp, "plop");
      jasmineReact.render(<Comp />);
      expect(Comp.plop).toHaveBeenCalled();
    })

Any idea what I'm doing wrong?

1

There are 1 answers

3
Colin Ramsay On BEST ANSWER

JasmineReact's syntax for method expectations is different to the way you're using it. Try this:

expect(jasmineReact.classPrototype(Comp).plop).toHaveBeenCalled();

Additionally, if you're running an expectation against something like state, you need to use the instance that render returns, rather than the class:

var comp = jasmineReact.render(<Comp />);

expect(comp.state.something).toBe('else');