React v0.14.2, createClass maintains an object map of defined methods in __reactAutoBindMap and reuses this map to rebind the methods to the component in the the private constructor for the component.

This behavior can be worked around in the tests by resetting this map to an empty object, forcing the constructor to skip this map and use the replacement spy.

function mockComponentMethod(component, method) {
   const boundMethod = method.bind(component)
   component.prototype.__reactAutoBindMap = {};
   component.prototype[] = boundMethod;

   return boundMethod;

The test to check that the spy was called can be written as follows:

const testMethodSpy = spyOn(TestClass.prototype, 'testMethod');
mockComponentMethod(TestClass, testMethodSpy);

Related Query

More Query from same tag