
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
sinon-spy-react
Advanced tools
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.
// Before
const spy = spyOnComponentMethod(Component, 'componentDidMount');
// After
const spy = sinon.spy(Component.prototype, 'componentDidMount');
// Before
const stub = stubComponentMethod(Component, 'getInitialState').returns({
foo: 'bar'
});
// After
const stub = sinon.stub(Component.prototype, 'getInitialState').returns({
foo: 'bar'
});
npm install sinon-spy-react --save-dev
For the old 1.x documentation, look here.
spyOnComponentMethod(reactClass, methodName)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. componentDidMount). getDefaultProps and 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.
stubComponentMethod(reactClass, methodName)This method stubs the method on the given React component class and returns the stub.
Note: The 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.
import assert from 'assert'; // obviously you can use a assertion library of your choice
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import { spyOnComponentMethod, stubComponentMethod } from 'sinon-spy-react';
import Component from './Component'; // some React component to test
it('calls componentDidMount after mounting', () => {
const spy = spyOnComponentMethod(Component, 'componentDidMount');
const component = TestUtils.renderIntoDocument(<Component />);
assert(spy.calledOnce);
});
it('does something with specific initial state', () => {
const stub = stubComponentMethod(Component, 'getInitialState').returns({
foo: 'bar' // the stubbed/mocked initial state
});
const spy = spyOnComponentMethod(Component, 'someSpecialMethod'); // gets called if state.foo === 'bar'
const component = TestUtils.renderIntoDocument(<Component />);
assert(spy.calledOnce);
stub.restore();
});
The examples are written in ES6 but you can use this library without problems with ES5.
FAQs
Spy on React.js classes with Sinon
The npm package sinon-spy-react receives a total of 90 weekly downloads. As such, sinon-spy-react popularity was classified as not popular.
We found that sinon-spy-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.