stubReactComponent.jsx 928 B

1234567891011121314151617181920212223242526272829
  1. // Inspired by TimothyRHuertas
  2. // https://gist.github.com/TimothyRHuertas/d7d06313c5411fe242bb
  3. import React from 'react';
  4. let divFactory = React.createFactory('div');
  5. let originalCreateElement = React.createElement;
  6. // eslint-disable-next-line import/no-anonymous-default-export
  7. export default function(stubber, stubbedComponents) {
  8. stubber.stub(React, 'createElement', function(component, props) {
  9. props = props || {};
  10. if (stubbedComponents.indexOf(component) === -1) {
  11. return originalCreateElement.apply(React, arguments);
  12. } else {
  13. let componentFactory = React.createFactory(component);
  14. let displayName = componentFactory(props).type.displayName;
  15. if (displayName) {
  16. if (props.className) {
  17. props.className = props.className + ' ' + displayName;
  18. } else {
  19. props.className = displayName;
  20. }
  21. }
  22. return divFactory(props);
  23. }
  24. });
  25. }