score:1

you can also use the chai plugin to spy on custom functions in you jsx file.

// to use this pluggin add this to the top of your testing file

const chai = require("chai"), spies = require("chai-spies");
chai.use(spies);
import foo from "./<path to component>/foo.jsx";

describe("foo", () => {
  it("a call to customfunction will not error", () => {
    let spy = chai.spy(foo.prototype, "customfunciton"); // spy
    const wrapper = mount(<foo/>);
    wrapper.setprops({bar: "baz"}); // manipulate you component in some way
    expect(spy).to.have.been.called.once();
  });
});

@leland-richardson is right, it depends on what your test is doing. understanding that will help you compose new ways to manipulate your component and thus make assertions.

another example testing a function that updates your components state.

it("function will assert new state", () => {
  const wrapper = shallow(<foo {...props}/>);
  wrapper.instance.customfunction(); // call custom function
  wrapper.update();
  expect(wrapper.state("bar")).to.equal("new-state");
});

chai-spies also has a handful of chainable getters that make testing custom functions much easier. please see the docs for a more in-depth explanation.

score:27

the best answer to this question really depends on what it is that customfunction is actually doing...

you can call the function like this:

wrapper.instance().customfunction('foo', 'bar');

if it's a function that sets state on the instance itself, and thus affects what the rendered output looks like, you may want to call .update() as well

wrapper.instance().customfunction('foo', 'bar'); // uses setstate internally
wrapper.update(); // updates render tree
// do assertions on the rendered output

Related Query

More Query from same tag