2

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?

Community
  • 1
  • 1
Simon Lomax
  • 8,714
  • 8
  • 42
  • 75
  • Should you be using jasmineReact.render rather than renderIntoDocument? – Colin Ramsay Jun 19 '15 at 12:14
  • If I use jasmineRect.render I get: ```Warning: Comp.type is deprecated. Use Comp directly to access the class.' Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.``` – Simon Lomax Jun 19 '15 at 12:24
  • Are you passing a dom node as the second argument? – Colin Ramsay Jun 19 '15 at 12:27
  • Just amended the question slightly so I return ```(
    )``` from render method. Didn't make any difference though. Is that what you were refering to?
    – Simon Lomax Jun 19 '15 at 12:31
  • Amended again so instead of ```TestUtils.renderIntoDocument();``` I call ```jasmineReact.render();``` – Simon Lomax Jun 19 '15 at 12:35

1 Answers1

0

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');
Colin Ramsay
  • 16,086
  • 9
  • 52
  • 57
  • Thanks Colin. Your first line works correctly - my bad, its mentioned in the docs. However, ``jasmineReact.render();`` is the line thats throwing the exception. If I change it to ```TestUtils.renderIntoDocument();``` then the whole test passes. Any idea why that would be? – Simon Lomax Jun 19 '15 at 13:27
  • jasmineReact.render assumes that you have the #jasmine_content div on your page somewhere, is it possible that's not present? – Colin Ramsay Jun 19 '15 at 13:38
  • Not sure I know what you mean when you say 'on the page'. Which page? When I look at the docs for ```jsamine-react``` it has this in the comments: // jasmineReact wraps React.render, so you don't have to worry // about the async nature of when the actual DOM get's rendered, or selecting // where your component needs to get rendered (default is #jasmine_content) Where would I put #jasmine_content ? – Simon Lomax Jun 19 '15 at 15:05