1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/test/lib/render-helpers.js

125 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { useMemo } from 'react';
import { Provider } from 'react-redux';
import { render } from '@testing-library/react';
import { mount, shallow } from 'enzyme';
import { Router, MemoryRouter } from 'react-router-dom';
import PropTypes from 'prop-types';
import { createMemoryHistory } from 'history';
import { I18nContext, LegacyI18nProvider } from '../../ui/contexts/i18n';
import { LegacyMetaMetricsProvider } from '../../ui/contexts/metametrics';
import { getMessage } from '../../ui/helpers/utils/i18n-helper';
import * as en from '../../app/_locales/en/messages.json';
2018-09-24 18:28:04 +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 = {
history: new MemoryRouter().history,
2018-09-24 18:28:04 +02:00
route: {
location: {
pathname,
},
2018-09-24 18:28:04 +02:00
match: {},
},
};
2018-09-24 18:28:04 +02:00
const createContext = () => ({
context: {
router,
2020-02-15 21:34:12 +01:00
t: (str) => str,
metricsEvent: () => undefined,
2022-03-29 15:46:24 +02:00
trackEvent: () => undefined,
store,
},
childContextTypes: {
router: PropTypes.object,
t: PropTypes.func,
metricsEvent: PropTypes.func,
2022-03-29 15:46:24 +02:00
trackEvent: PropTypes.func,
store: PropTypes.object,
},
});
2018-09-24 18:28:04 +02:00
const Wrapper = () => (
<MemoryRouter initialEntries={[{ pathname }]} initialIndex={0}>
{component}
</MemoryRouter>
);
return mount(<Wrapper />, createContext());
2018-09-24 18:28:04 +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,
};
export function renderWithProvider(component, store, pathname = '/') {
const history = createMemoryHistory({ initialEntries: [pathname] });
const Wrapper = ({ children }) =>
store ? (
<Provider store={store}>
<Router history={history}>
<I18nProvider currentLocale="en" current={en} en={en}>
<LegacyI18nProvider>
<LegacyMetaMetricsProvider>{children}</LegacyMetaMetricsProvider>
</LegacyI18nProvider>
</I18nProvider>
</Router>
</Provider>
) : (
2022-08-16 17:25:30 +02:00
<Router history={history}>
<LegacyI18nProvider>
<LegacyMetaMetricsProvider>{children}</LegacyMetaMetricsProvider>
</LegacyI18nProvider>
</Router>
);
Wrapper.propTypes = {
children: PropTypes.node,
};
return {
...render(component, { wrapper: Wrapper }),
history,
};
}
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 });
}