Skip to content

Commit

Permalink
feat: Retro-compatibility with old context API
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jan 7, 2020
1 parent d19041b commit 15fd3be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions react/I18n/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class I18n extends Component {
}
}

getChildContext() {
return this.contextValue
}

// for preact
// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
Expand Down
53 changes: 53 additions & 0 deletions react/I18n/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react'
import PropTypes from 'prop-types'
import I18n, { useI18n } from '.'

const locales = { helloworld: 'Hello World !' }

const DumbI18nHelloWorld = ({ t, f, lang }) => (
<div>
{t('helloworld')}
<br />
{f('2020-01-06', 'DDD MMM')}
<br />
{lang}
</div>
)

const I18nHelloWorldHook = () => {
const { t, f, lang } = useI18n()
return <DumbI18nHelloWorld t={t} f={f} lang={lang} />
}

const I18nHelloWorldOldAPI = (props, context) => {
const { t, f, lang } = context
return <DumbI18nHelloWorld t={t} f={f} lang={lang} />
}

I18nHelloWorldOldAPI.contextTypes = {
t: PropTypes.func,
f: PropTypes.func,
lang: PropTypes.string
}

describe('new context api', () => {
it('should provide t and f and lang through useI18n hook', () => {
const root = mount(
<I18n lang="en" dictRequire={() => locales}>
<I18nHelloWorldHook />
</I18n>
)
expect(root.html()).toBe('<div>Hello World !<br>6 Jan<br>en</div>')
})
})

describe('old context api', () => {
it('should provide t and f and lang through old context API', () => {
const root = mount(
<I18n lang="en" dictRequire={() => locales}>
<I18nHelloWorldOldAPI />
</I18n>
)
expect(root.html()).toBe('<div>Hello World !<br>6 Jan<br>en</div>')
})
})

0 comments on commit 15fd3be

Please sign in to comment.