mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
Use message IDs as translations in tests
This commit is contained in:
parent
b711251978
commit
81a2a2c50b
2 changed files with 22 additions and 4 deletions
17
src/app.js
17
src/app.js
|
|
@ -4,6 +4,7 @@ import ReactDOMServer from 'react-dom/server';
|
|||
import Helmet from 'react-helmet';
|
||||
import { BrowserRouter, StaticRouter } from 'react-router-dom';
|
||||
import { Provider } from 'react-redux';
|
||||
import { mapValues } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import { IntlProvider, addLocaleData } from 'react-intl';
|
||||
import configureStore from './store';
|
||||
|
|
@ -18,7 +19,19 @@ import config from './config';
|
|||
import localeData from 'react-intl/locale-data/en';
|
||||
import messages from './translations/en.json';
|
||||
|
||||
const isTestEnv = process.env.NODE_ENV === 'test';
|
||||
|
||||
// Locale should not affect the tests. We ensure this by providing
|
||||
// messages with the key as the value of each message.
|
||||
const testMessages = mapValues(messages, (val, key) => key);
|
||||
const localeMessages = isTestEnv ? testMessages : messages;
|
||||
|
||||
const setupLocale = () => {
|
||||
if (isTestEnv) {
|
||||
// Don't change the locale in tests
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the translation messages
|
||||
addLocaleData([...localeData]);
|
||||
|
||||
|
|
@ -31,7 +44,7 @@ export const ClientApp = props => {
|
|||
const { store } = props;
|
||||
setupLocale();
|
||||
return (
|
||||
<IntlProvider locale={config.locale} messages={messages}>
|
||||
<IntlProvider locale={config.locale} messages={localeMessages}>
|
||||
<Provider store={store}>
|
||||
<BrowserRouter>
|
||||
<Routes routes={routeConfiguration()} />
|
||||
|
|
@ -49,7 +62,7 @@ export const ServerApp = props => {
|
|||
const { url, context, store } = props;
|
||||
setupLocale();
|
||||
return (
|
||||
<IntlProvider locale={config.locale} messages={messages}>
|
||||
<IntlProvider locale={config.locale} messages={localeMessages}>
|
||||
<Provider store={store}>
|
||||
<StaticRouter location={url} context={context}>
|
||||
<Routes routes={routeConfiguration()} />
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import React from 'react';
|
||||
import renderer from 'react-test-renderer';
|
||||
import { mapValues } from 'lodash';
|
||||
import { shallow } from 'enzyme';
|
||||
import toJson from 'enzyme-to-json';
|
||||
import { IntlProvider, addLocaleData } from 'react-intl';
|
||||
|
|
@ -7,7 +8,11 @@ import en from 'react-intl/locale-data/en';
|
|||
import { BrowserRouter } from 'react-router-dom';
|
||||
import { Provider } from 'react-redux';
|
||||
import configureStore from '../store';
|
||||
import localeData from '../translations/en.json';
|
||||
import messages from '../translations/en.json';
|
||||
|
||||
// Locale should not affect the tests. We ensure this by providing
|
||||
// messages with the key as the value of each message.
|
||||
const testMessages = mapValues(messages, (val, key) => key);
|
||||
|
||||
// Provide all the context for components that connect to the Redux
|
||||
// store, i18n, router, etc.
|
||||
|
|
@ -15,7 +20,7 @@ export const TestProvider = props => {
|
|||
const store = configureStore();
|
||||
addLocaleData([...en]);
|
||||
return (
|
||||
<IntlProvider locale="en" messages={localeData}>
|
||||
<IntlProvider locale="en" messages={testMessages}>
|
||||
<BrowserRouter>
|
||||
<Provider store={store}>{props.children}</Provider>
|
||||
</BrowserRouter>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue