This package has been deprecated
Please use classes instead of React.createClass(). More information: https://github.com/levrik/sinon-spy-react#deprecated
Spy on React.js classes with Sinon
Please use classes instead of the
React.createClass API since it's deprecated. Spying and stubbing of class methods is possible without this library.
How do I migrate my tests after conversion to classes?
// Beforeconst spy = ;// Afterconst spy = sinon;
// Beforeconst stub =;// Afterconst stub = sinon;
npm install sinon-spy-react --save-dev
For the old 1.x documentation, look here.
This method creates and returns a spy on the given React component class and method. The original function is wrapped into the spy. That means the original code will still be executed. To prevent this or execute custom code use a stub.
Note: It isn't possible to create a spy on a method which does not exist except for lifecycle methods (e.g.
getChildContext are not supported. For
getInitialState use a stub. That can be used to return a custom initial state for the test scenario, for example.
This method stubs the method on the given React component class and returns the stub.
restore method of the stub only restores the function on the React class definition, not on the instance. Create a new instance of the component after calling
restore to get an instance with the restored method.
; // obviously you can use a assertion library of your choice;;;; // some React component to test;;
The examples are written in ES6 but you can use this library without problems with ES5.