From 81a2a2c50b90ecea292c141c81265400a3d1231d Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Thu, 30 Nov 2017 11:04:10 +0200 Subject: [PATCH] Use message IDs as translations in tests --- src/app.js | 17 +++++++++++++++-- src/util/test-helpers.js | 9 +++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index 277ac632..24d81e86 100644 --- a/src/app.js +++ b/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 ( - + @@ -49,7 +62,7 @@ export const ServerApp = props => { const { url, context, store } = props; setupLocale(); return ( - + diff --git a/src/util/test-helpers.js b/src/util/test-helpers.js index e63c4e6c..f544cbda 100644 --- a/src/util/test-helpers.js +++ b/src/util/test-helpers.js @@ -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 ( - + {props.children}