Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-component-tester
Advanced tools
A simple helper to make testing react components with shallow rendering easier
because testing components with shallow rendering is fantastic...but can be a bit of a pain and other testing libraries focus on finding components rather than providing an opinionated way of testing them, including their state.
a component system that builds a basic responsive postcard can be found in the tests directory with example usage (which also self tests the library)
often you want to test how components are rendered with different prop values. this tester allows the addition of "flavours" to make the test code more terse
to create a tester, run create and pass in the component under test to "be used"
const tester = ReactTester.create().use(Address);
then add a flavour which takes a name and a props object
const NO_ADDRESS = tester.addFlavour('NO_ADDRESS', {
addressee: 'Mr Robert Smith',
});
then the flavour can be tested on, here the NONE flavour's type is checked
const actual = NONE.type;
const expected = 'div';
assert.deepEqual(actual, expected);
takes a react component and returns the number of times it appears in the rendered tree for the component under test
here the THREE_LINES flavour is tested for the number of AddressLine components it would render if given three lines of address
it('render four address lines if there are three lines in the given address', () => {
const actual = THREE_LINES.countComponents(AddressLine);
const expected = 4;
assert.deepEqual(actual, expected);
});
takes a zero based decimal delimited string path and returns the resulting rendered component. this will make your tests more brittle to DOM changes but can come in handy
here the first child is found. if we wanted the second child of the third child of the fourth child we would pass "3.2.1"
const value = NOTHING.findChild('0');
const actual = typeof value;
const expected = 'undefined';
assert.deepEqual(actual, expected);
takes a react component and returns an array of all child rendered components of that type
here we find all AddressLine components for the NO_ADDRESS flavour, take the first one and check that it is the addressee
const addressee = NO_ADDRESS.findComponents(AddressLine)[0];
const actual = addressee.props.text;
const expected = 'Mr Robert Smith';
assert.deepEqual(actual, expected);
returns the current state of the given component flavour. resetState should be run after each test to ensure tests do not collide
here we fire the click function given to the dumb component and confirm that the smart component's state is updated correctly
NONE.component.props.onClick();
const actual = NONE.getState().type;
const expected = stampTypes.FIRST_CLASS;
assert.deepEqual(actual, expected);
propFunc
takes a string which is the prop of a dumb component to which you are passing a function
mapsTo
takes a string which maps to the method on the smart component class
if your mapsTo
method expects some specific arguments, these can be passed as an array using withArgs
the returned value will be a boolean indicating if the given prop function maps to the expected class method
here we test that the stamp dumb component was correctly given the smart component's handleOnClick
method
const isMapped =
NONE
.component
.propFunc('onClick')
.withArgs({ test: 123 })
.mapsTo('handleOnClick');
assert(isMapped);
returns the state of the flavour of the component under test to the initial state
If for any reason you need to restore spies placed on a component as part of set up you can use the teardown method. Returns the tester.
const tester = ReactTester.create().use(Address);
// Do stuff...
tester.teardown();
the rendered component that is under test
the props of the rendered component will be returned for each component found
for ease of use, the style prop is placed on each component too
each returned component will have a type
if a component doesn't have any children, it's content will be added as the value property
all methods are spied on by default, if for some reason you would like to remove them you can pass a config on create
ReactTester.create({
spyOnDefault: false,
spyOn: {
someMethod: false,
},
}).use(SomeComponent);
You can either pass:
spyOnDefault
as false to disable all spiesspyOn
config to disable per methodFAQs
A simple helper to make testing react components with shallow rendering easier
The npm package react-component-tester receives a total of 8 weekly downloads. As such, react-component-tester popularity was classified as not popular.
We found that react-component-tester 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.