2022-11-15 17:49:02 +01:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import { render } from '@testing-library/react';
|
2022-11-15 17:49:02 +01:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2023-02-14 19:14:00 +01:00
|
|
|
import { Router } from 'react-router-dom';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2022-08-03 22:30:43 +02:00
|
|
|
import { createMemoryHistory } from 'history';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { I18nContext, LegacyI18nProvider } from '../../ui/contexts/i18n';
|
2022-08-03 22:30:43 +02:00
|
|
|
import { LegacyMetaMetricsProvider } from '../../ui/contexts/metametrics';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { getMessage } from '../../ui/helpers/utils/i18n-helper';
|
2021-04-23 00:31:13 +02:00
|
|
|
import * as en from '../../app/_locales/en/messages.json';
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2021-04-23 00:31:13 +02:00
|
|
|
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 (
|
|
|
|
<I18nContext.Provider value={t}>{props.children}</I18nContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
I18nProvider.propTypes = {
|
|
|
|
currentLocale: PropTypes.string,
|
|
|
|
current: PropTypes.object,
|
|
|
|
en: PropTypes.object,
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
I18nProvider.defaultProps = {
|
|
|
|
children: undefined,
|
|
|
|
};
|
|
|
|
|
2022-08-03 22:30:43 +02:00
|
|
|
export function renderWithProvider(component, store, pathname = '/') {
|
|
|
|
const history = createMemoryHistory({ initialEntries: [pathname] });
|
2021-04-23 00:31:13 +02:00
|
|
|
const Wrapper = ({ children }) =>
|
2021-04-09 17:59:32 +02:00
|
|
|
store ? (
|
|
|
|
<Provider store={store}>
|
2022-08-03 22:30:43 +02:00
|
|
|
<Router history={history}>
|
2021-04-23 00:31:13 +02:00
|
|
|
<I18nProvider currentLocale="en" current={en} en={en}>
|
2022-08-03 22:30:43 +02:00
|
|
|
<LegacyI18nProvider>
|
|
|
|
<LegacyMetaMetricsProvider>{children}</LegacyMetaMetricsProvider>
|
|
|
|
</LegacyI18nProvider>
|
2021-04-23 00:31:13 +02:00
|
|
|
</I18nProvider>
|
2022-08-03 22:30:43 +02:00
|
|
|
</Router>
|
2021-04-09 17:59:32 +02:00
|
|
|
</Provider>
|
|
|
|
) : (
|
2022-08-16 17:25:30 +02:00
|
|
|
<Router history={history}>
|
|
|
|
<LegacyI18nProvider>
|
|
|
|
<LegacyMetaMetricsProvider>{children}</LegacyMetaMetricsProvider>
|
|
|
|
</LegacyI18nProvider>
|
|
|
|
</Router>
|
2021-04-09 17:59:32 +02:00
|
|
|
);
|
2020-08-20 06:13:59 +02:00
|
|
|
|
2021-04-23 00:31:13 +02:00
|
|
|
Wrapper.propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
2022-08-03 22:30:43 +02:00
|
|
|
return {
|
|
|
|
...render(component, { wrapper: Wrapper }),
|
|
|
|
history,
|
|
|
|
};
|
2020-08-20 06:13:59 +02:00
|
|
|
}
|
2022-02-23 21:30:26 +01:00
|
|
|
|
|
|
|
export function renderWithLocalization(component) {
|
|
|
|
const Wrapper = ({ children }) => (
|
|
|
|
<I18nProvider currentLocale="en" current={en} en={en}>
|
|
|
|
<LegacyI18nProvider>{children}</LegacyI18nProvider>
|
|
|
|
</I18nProvider>
|
|
|
|
);
|
|
|
|
|
|
|
|
Wrapper.propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
return render(component, { wrapper: Wrapper });
|
|
|
|
}
|
2022-11-15 17:49:02 +01:00
|
|
|
|
|
|
|
export function renderControlledInput(InputComponent, props) {
|
|
|
|
const ControlledWrapper = () => {
|
|
|
|
const [value, setValue] = useState('');
|
|
|
|
return (
|
|
|
|
<InputComponent
|
|
|
|
value={value}
|
|
|
|
onChange={(e) => setValue(e.target.value)}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
return { user: userEvent.setup(), ...render(<ControlledWrapper />) };
|
|
|
|
}
|
2022-11-22 22:04:27 +01:00
|
|
|
|
|
|
|
// userEvent setup function as per testing-library docs
|
|
|
|
// https://testing-library.com/docs/user-event/intr
|
|
|
|
export function renderWithUserEvent(jsx) {
|
|
|
|
return {
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(jsx),
|
|
|
|
};
|
|
|
|
}
|