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';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { mount, shallow } from 'enzyme';
|
2022-08-03 22:30:43 +02:00
|
|
|
import { Router, MemoryRouter } 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-28 21:53:59 +02:00
|
|
|
export function shallowWithContext(jsxComponent) {
|
|
|
|
return shallow(jsxComponent, {
|
|
|
|
context: { t: (str1, str2) => (str2 ? str1 + str2 : str1) },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function mountWithRouter(component, store = {}, pathname = '/') {
|
2018-09-24 18:28:04 +02:00
|
|
|
// Instantiate router context
|
|
|
|
const router = {
|
2020-01-30 20:34:45 +01:00
|
|
|
history: new MemoryRouter().history,
|
2018-09-24 18:28:04 +02:00
|
|
|
route: {
|
2020-01-30 20:34:45 +01:00
|
|
|
location: {
|
2020-08-19 18:27:05 +02:00
|
|
|
pathname,
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
2018-09-24 18:28:04 +02:00
|
|
|
match: {},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-09-24 18:28:04 +02:00
|
|
|
|
|
|
|
const createContext = () => ({
|
2020-01-30 20:34:45 +01:00
|
|
|
context: {
|
|
|
|
router,
|
2020-02-15 21:34:12 +01:00
|
|
|
t: (str) => str,
|
2020-08-14 13:47:02 +02:00
|
|
|
metricsEvent: () => undefined,
|
2022-03-29 15:46:24 +02:00
|
|
|
trackEvent: () => undefined,
|
2020-01-30 20:34:45 +01:00
|
|
|
store,
|
|
|
|
},
|
|
|
|
childContextTypes: {
|
|
|
|
router: PropTypes.object,
|
|
|
|
t: PropTypes.func,
|
|
|
|
metricsEvent: PropTypes.func,
|
2022-03-29 15:46:24 +02:00
|
|
|
trackEvent: PropTypes.func,
|
2020-01-30 20:34:45 +01:00
|
|
|
store: PropTypes.object,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-24 18:28:04 +02:00
|
|
|
|
2019-12-06 16:40:07 +01:00
|
|
|
const Wrapper = () => (
|
2020-01-30 20:34:45 +01:00
|
|
|
<MemoryRouter initialEntries={[{ pathname }]} initialIndex={0}>
|
|
|
|
{component}
|
|
|
|
</MemoryRouter>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-12-06 16:40:07 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return mount(<Wrapper />, createContext());
|
2018-09-24 18:28:04 +02:00
|
|
|
}
|
2020-08-20 06:13:59 +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),
|
|
|
|
};
|
|
|
|
}
|