npm install --save-dev react-shallowdom-helpers import ShallowDOMHelpers from "react-shallowdom-helpers";
const renderedComponent = ShallowDOMHelpers.render(<MyComponent>); let renderedComponent = ShallowDOMHelpers.render(<MyComponent>);
ShallowDOMHelpers.click(renderedComponent, '.toggle-modal');
renderedComponent = ShallowDOMHelpers.reRenderMountedInstance();
// latest state is now set in the componentReturn the current mounted instance. You have access to properties such as state in this object.
ShallowDOMHelpers.getMountedInstance();It returns an array containing all the matching elements. If no element is found an empty array is returned.
ShallowDOMHelpers.findAllByClass(renderedComponent, 'class-name');It returns the first element matching the class name.
ShallowDOMHelpers.findByClass(renderedComponent, 'class-name');It returns an array containing all the matching elements. If no element is found an empty array is returned.
ShallowDOMHelpers.findByTag(renderedComponent, 'p');It returns the first element matching the tag.
ShallowDOMHelpers.findByTag(renderedComponent, 'p');It returns the first element matching the id.
ShallowDOMHelpers.findById(renderedComponent, 'id-here'); ShallowDOMHelpers.render(<Component>Inner Children</Component>);
const innerChildren = ShallowDOMHelpers.getInnerChildren(renderedComponent);
// Inner Children const text = ShallowDOMHelpers.getText(renderedComponent);
expect(text).to.eq('Text here'); const text = ShallowDOMHelpers.getTextByClass(renderedComponent, 'class-name-here');
expect(text).to.eq('Text here'); const text = ShallowDOMHelpers.getTextById(renderedComponent, 'id-here');
expect(text).to.eq('Text here'); const text = ShallowDOMHelpers.getTextByTag(renderedComponent, 'p');
expect(text).to.eq('Text here'); // by class name
ShallowDOMHelpers.click(renderedComponent, '.className', { target: { className: 'className' } });
// by id
ShallowDOMHelpers.click(renderedComponent, '#id');
// by tag
ShallowDOMHelpers.click(renderedComponent, 'p', { target: { tagName: 'p' } }); // by class name
ShallowDOMHelpers.change(renderedComponent, '.className', { target: { text: '1234'} });
// by id
ShallowDOMHelpers.change(renderedComponent, '#id', '123', { target: { text: '1234' } });
// by tag
ShallowDOMHelpers.change(renderedComponent, 'p', '123', { target: { text: '1234' } });Return React's TestUtils.
ShallowDOMHelpers.TestUtils;Create a fork of the project. Add the functionality you want as well as tests and submit a PR.
nvm use 5
npm install
npm test- Add more matchers
- Refactor, refactor, refactor