Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import React, { Fragment } from 'react';
  2.  
  3. const Intl = jest.genMockFromModule('react-intl');
  4.  
  5. const intl = {
  6. formatMessage: ({ id, values }) => {
  7. if (values) {
  8. return JSON.stringify({ id, values });
  9. }
  10.  
  11. return id;
  12. },
  13. locale: 'en',
  14. };
  15.  
  16. Intl.injectIntl = Node => {
  17. const renderWrapped = props => <Node {...props} intl={intl} />;
  18. renderWrapped.displayName = Node.displayName || Node.name || 'Component';
  19. return renderWrapped;
  20. };
  21.  
  22. Intl.FormattedMessage = ({ id, tagName: TagName = 'span', values, children }) => {
  23. const valuesString = !!values
  24. ? Object.entries(values || {}).map(([key, value], index) => {
  25. if (React.isValidElement(value)) {
  26. return <Fragment key={index}>{value}</Fragment>;
  27. } else {
  28. return JSON.stringify({ [key]: value });
  29. }
  30. })
  31. : [null];
  32.  
  33. if (typeof children === 'function') {
  34. return children(id, ...valuesString);
  35. }
  36.  
  37. return (
  38. <TagName>
  39. {id}
  40. {valuesString}
  41. </TagName>
  42. );
  43. };
  44.  
  45. Intl.IntlProvider = props => <>{props.children}</>;
  46. module.exports = Intl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement