1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/test/lib/render-helpers.js

114 lines
2.9 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 { MemoryRouter } from 'react-router-dom';
import PropTypes from 'prop-types';
import { I18nContext, LegacyI18nProvider } from '../../ui/contexts/i18n';
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,
};
2020-11-03 00:41:28 +01:00
export function renderWithProvider(component, store) {
const Wrapper = ({ children }) =>
store ? (
<Provider store={store}>
<MemoryRouter initialEntries={['/']} initialIndex={0}>
<I18nProvider currentLocale="en" current={en} en={en}>
<LegacyI18nProvider>{children}</LegacyI18nProvider>
</I18nProvider>
</MemoryRouter>
</Provider>
) : (
<LegacyI18nProvider>{children}</LegacyI18nProvider>
);
Wrapper.propTypes = {
children: PropTypes.node,
};
return render(component, { wrapper: Wrapper });
}
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 });
}