import React, { useMemo } from 'react';
import { Provider } from 'react-redux';
import { render } from '@testing-library/react';
import { mount } from 'enzyme';
import { MemoryRouter } from 'react-router-dom';
import PropTypes from 'prop-types';
import { I18nContext, LegacyI18nProvider } from '../../ui/app/contexts/i18n';
import { getMessage } from '../../ui/app/helpers/utils/i18n-helper';
import * as en from '../../app/_locales/en/messages.json';
export function mountWithRouter(component, store = {}, pathname = '/') {
// Instantiate router context
const router = {
history: new MemoryRouter().history,
route: {
location: {
pathname,
},
match: {},
},
};
const createContext = () => ({
context: {
router,
t: (str) => str,
metricsEvent: () => undefined,
store,
},
childContextTypes: {
router: PropTypes.object,
t: PropTypes.func,
metricsEvent: PropTypes.func,
store: PropTypes.object,
},
});
const Wrapper = () => (
{component}
);
return mount(, createContext());
}
export const I18nProvider = (props) => {
const { currentLocale, current, en: eng } = props;
const t = useMemo(() => {
return (key, ...args) =>
getMessage(currentLocale, current, key, ...args) ||
getMessage(currentLocale, eng, key, ...args);
}, [currentLocale, current, eng]);
return (
{props.children}
);
};
I18nProvider.propTypes = {
currentLocale: PropTypes.string,
current: PropTypes.object,
en: PropTypes.object,
children: PropTypes.node,
};
I18nProvider.defaultProps = {
children: undefined,
};
export function renderWithProvider(component, store) {
const Wrapper = ({ children }) =>
store ? (
{children}
) : (
{children}
);
Wrapper.propTypes = {
children: PropTypes.node,
};
return render(component, { wrapper: Wrapper });
}