2021-02-04 19:15:23 +01:00
|
|
|
import React 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 { LegacyI18nProvider } from '../../ui/app/contexts/i18n';
|
2018-09-24 18:28:04 +02:00
|
|
|
|
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,
|
2020-01-30 20:34:45 +01:00
|
|
|
store,
|
|
|
|
},
|
|
|
|
childContextTypes: {
|
|
|
|
router: PropTypes.object,
|
|
|
|
t: PropTypes.func,
|
|
|
|
metricsEvent: PropTypes.func,
|
|
|
|
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
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function renderWithProvider(component, store) {
|
2020-08-20 06:13:59 +02:00
|
|
|
const Wrapper = () => (
|
|
|
|
<Provider store={store}>
|
2020-11-03 00:41:28 +01:00
|
|
|
<LegacyI18nProvider>{component}</LegacyI18nProvider>
|
2020-08-20 06:13:59 +02:00
|
|
|
</Provider>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-08-20 06:13:59 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return render(<Wrapper />);
|
2020-08-20 06:13:59 +02:00
|
|
|
}
|