JSXAttributeMock.js 871 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * @flow
  3. */
  4. import toAST from 'to-ast'; // eslint-disable-line import/no-extraneous-dependencies
  5. import JSXExpressionContainerMock from './JSXExpressionContainerMock';
  6. export type JSXAttributeMockType = {
  7. type: 'JSXAttribute',
  8. name: {
  9. type: 'JSXIdentifier',
  10. name: string,
  11. },
  12. value: mixed,
  13. };
  14. export default function JSXAttributeMock(prop: string, value: mixed, isExpressionContainer?: boolean = false): JSXAttributeMockType {
  15. let astValue;
  16. if (value && value.type !== undefined) {
  17. astValue = value;
  18. } else {
  19. astValue = toAST(value);
  20. }
  21. let attributeValue = astValue;
  22. if (isExpressionContainer || astValue.type !== 'Literal') {
  23. attributeValue = JSXExpressionContainerMock(astValue);
  24. }
  25. return {
  26. type: 'JSXAttribute',
  27. name: {
  28. type: 'JSXIdentifier',
  29. name: prop,
  30. },
  31. value: attributeValue,
  32. };
  33. }