Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function InjectorComponent(props) {
- function injectDataAttribute(reactElement) {
- if (React.isValidElement(reactElement)) {
- try {
- reactElement.type.attrs = { "data-test-id": reactElement.type.displayName};
- } catch(e) {}
- }
- }
- React.Children.forEach(props.children, (child) => TraverseReactTree(child, injectDataAttribute));
- return props.children;
- }
- function TraverseReactTree(reactElem, cb) {
- if (React.isValidElement(reactElem)) {
- cb(reactElem);
- React.Children.map(reactElem.props.children, (child) => {
- cb(child);
- TraverseReactTree(child, cb);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement