2019-12-06 16:40:07 +01:00
|
|
|
import React from 'react'
|
2020-08-20 06:13:59 +02:00
|
|
|
import { Provider } from 'react-redux'
|
|
|
|
import { render } from '@testing-library/react'
|
2020-01-30 20:34:45 +01:00
|
|
|
import { mount } from 'enzyme'
|
|
|
|
import { MemoryRouter } from 'react-router-dom'
|
|
|
|
import PropTypes from 'prop-types'
|
2020-08-20 06:13:59 +02:00
|
|
|
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: {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
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>
|
2019-12-06 16:40:07 +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>
|
|
|
|
)
|
|
|
|
|
|
|
|
return render(<Wrapper />)
|
|
|
|
}
|