All files / __mocks__ react-i18next.js

42.85% Statements 15/35
14.81% Branches 4/27
25% Functions 3/12
50% Lines 15/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 5918x 18x   18x   18x   18x                                               18x 18x 1953x 1953x 48x 48x       1953x     18x   18x       564x                  
const React = require('react');
const reactI18next = require('react-i18next');
 
const hasChildren = (node) => node && (node.children || (node.props && node.props.children));
 
const getChildren = (node) => (node && node.children ? node.children : node.props && node.props.children);
 
const renderNodes = (reactNodes) => {
  if (typeof reactNodes === 'string') {
    return reactNodes;
  }
 
  return Object.keys(reactNodes).map((key, i) => {
    const child = reactNodes[key];
    const isElement = React.isValidElement(child);
 
    if (typeof child === 'string') {
      return child;
    }
    if (hasChildren(child)) {
      const view = renderNodes(getChildren(child));
      return React.cloneElement(child, { ...child.props, key: i }, view);
    }
    if (typeof child === 'object' && !isElement) {
      return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, '');
    }
 
    return child;
  });
};
 
const useMock = [(key) => key, {}];
useMock.t = (key, defaultValue, options = {}) => {
  let translatedString = defaultValue || key;
  Object.entries(options).forEach(([k, v]) => {
    Eif (key !== 'interpolation') {
      translatedString = translatedString.replace(new RegExp(`{{${k}}}`, 'g'), v);
    }
  });
 
  return translatedString;
};
 
useMock.i18n = { language: 'en_US' };
 
module.exports = {
  // this mock makes sure any components using the translate HoC receive the t function as a prop
  Trans: ({ children }) => (Array.isArray(children) ? renderNodes(children) : renderNodes([children])),
  Translation: ({ children }) => children((k) => k, { i18n: {} }),
  useTranslation: () => useMock,
  // mock if needed
  I18nextProvider: reactI18next.I18nextProvider,
  initReactI18next: reactI18next.initReactI18next,
  setDefaults: reactI18next.setDefaults,
  getDefaults: reactI18next.getDefaults,
  setI18n: reactI18next.setI18n,
  getI18n: reactI18next.getI18n,
};