-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #978 from cozy/test-withLocales
test: Testing withLocales
- Loading branch information
Showing
3 changed files
with
71 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import React from 'react' | ||
import { I18n, translate } from './' | ||
import omit from 'lodash/omit' | ||
|
||
const withLocales = locales => Component => | ||
translate()(props => { | ||
return ( | ||
<I18n dictRequire={localeCode => locales[localeCode]} lang={props.lang}> | ||
<Component {...props} /> | ||
</I18n> | ||
) | ||
}) | ||
const withLocales = locales => Component => { | ||
// The inner components needs to receive t | ||
const Translated = translate()(Component) | ||
|
||
class Wrapped extends React.Component { | ||
render() { | ||
// Do not pass t downwards | ||
const { lang, ...rest } = omit(this.props, 't') | ||
return ( | ||
<I18n dictRequire={localeCode => locales[localeCode]} lang={lang}> | ||
<Translated {...rest} /> | ||
</I18n> | ||
) | ||
} | ||
} | ||
|
||
Wrapped.displayName = `withLocales(${Component.displayName || | ||
Component.name})` | ||
|
||
// The outer component needs to receive lang | ||
return translate()(Wrapped) | ||
} | ||
|
||
export default withLocales |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react' | ||
import { I18n } from '.' | ||
import withLocales from './withLocales' | ||
|
||
const globalLocales = { | ||
en: { | ||
'hello-world': 'Hello global world !' | ||
}, | ||
fr: { | ||
'hello-world': 'Bonjour le monde global !' | ||
} | ||
} | ||
|
||
const componentLocales = { | ||
en: { | ||
'hello-world': 'Hello local world !' | ||
}, | ||
fr: { | ||
'hello-world': 'Bonjour le monde local !' | ||
} | ||
} | ||
|
||
class Component extends React.Component { | ||
render() { | ||
const { t } = this.props | ||
return <div>{t('hello-world')}</div> | ||
} | ||
} | ||
const TComponent = withLocales(componentLocales)(Component) | ||
|
||
describe('with locales', () => { | ||
let root | ||
const setup = ({ lang }) => { | ||
root = mount( | ||
<I18n lang={lang} dictRequire={localeCode => globalLocales[localeCode]}> | ||
<TComponent /> | ||
</I18n> | ||
) | ||
} | ||
it('should provide t with correct locale strings', () => { | ||
setup({ lang: 'en' }) | ||
expect(root.text()).toBe('Hello local world !') | ||
setup({ lang: 'fr' }) | ||
expect(root.text()).toBe('Bonjour le monde local !') | ||
}) | ||
}) |