Skip to content

Commit

Permalink
Fix fallback when is no provider (#32)
Browse files Browse the repository at this point in the history
* Fix fallback when is no provider

* Remove package-lock

* Update github action to check node 13.x
  • Loading branch information
aralroca authored Jan 8, 2020
1 parent b40046d commit 30d55fc
Show file tree
Hide file tree
Showing 8 changed files with 6,099 additions and 11,507 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
node-version: [10.x, 12.x, 13.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependences
run: |
npm install -g yarn
sudo apt-get install tree
- name: yarn install, build, and test
run: |
yarn install
yarn build
yarn test
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependences
run: |
npm install -g yarn
sudo apt-get install tree
- name: yarn install, build, and test
run: |
yarn install
yarn build
yarn test
149 changes: 91 additions & 58 deletions __tests__/useTranslation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { render, cleanup } from '@testing-library/react'
import I18nProvider from '../src/I18nProvider'
import useTranslation from '../src/useTranslation'

const Inner = ({ i18nKey, query }) => {
const Inner = ({ i18nKey, query }) => {
const { t } = useTranslation()
return t(i18nKey, query)
}

const TestEnglish = ({ i18nKey, query, namespaces }) => {
return (
<I18nProvider lang="en" namespaces={namespaces} >
<I18nProvider lang="en" namespaces={namespaces}>
<Inner i18nKey={i18nKey} query={query} />
</I18nProvider>
)
Expand All @@ -20,20 +20,19 @@ describe('useTranslation', () => {
afterEach(cleanup)

describe('plurals', () => {

test('should work with singular | count=1', () => {
const i18nKey = 'ns:withsingular'
const expected = 'The number is NOT ZERO'
const withSingular = {
'withsingular': 'The number is NOT ZERO',
'withsingular_0': 'The number is ZERO!',
'withsingular_plural': 'Oops!'
const withSingular = {
withsingular: 'The number is NOT ZERO',
withsingular_0: 'The number is ZERO!',
withsingular_plural: 'Oops!',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
query={{ count: 1 }}
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
query={{ count: 1 }}
/>
)
expect(container.textContent).toContain(expected)
Expand All @@ -42,16 +41,16 @@ describe('useTranslation', () => {
test('should work with singular | count=0', () => {
const i18nKey = 'ns:withsingular'
const expected = 'The number is NOT ONE'
const withSingular = {
'withsingular': 'The number is NOT ONE',
'withsingular_1': 'The number is ONE!',
'withsingular_plural': 'Oops!'
const withSingular = {
withsingular: 'The number is NOT ONE',
withsingular_1: 'The number is ONE!',
withsingular_plural: 'Oops!',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
query={{ count: 0 }}
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
query={{ count: 0 }}
/>
)
expect(container.textContent).toContain(expected)
Expand All @@ -60,52 +59,52 @@ describe('useTranslation', () => {
test('should work with _1 | count=1', () => {
const i18nKey = 'ns:withsingular'
const expected = 'The number is ONE!'
const with_1 = {
'withsingular': 'The number is NOT ONE',
'withsingular_1': 'The number is ONE!',
'withsingular_plural': 'Oops!'
const with_1 = {
withsingular: 'The number is NOT ONE',
withsingular_1: 'The number is ONE!',
withsingular_plural: 'Oops!',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: with_1 }}
<TestEnglish
namespaces={{ ns: with_1 }}
i18nKey={i18nKey}
query={{ count: 1 }}
/>
/>
)
expect(container.textContent).toContain(expected)
})

test('should work with _0 | count=0', () => {
const i18nKey = 'ns:withsingular'
const expected = 'The number is ZERO!'
const with_0 = {
'withsingular': 'The number is NOT ZERO',
'withsingular_0': 'The number is ZERO!',
'withsingular_plural': 'Oops!'
const with_0 = {
withsingular: 'The number is NOT ZERO',
withsingular_0: 'The number is ZERO!',
withsingular_plural: 'Oops!',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: with_0 }}
<TestEnglish
namespaces={{ ns: with_0 }}
i18nKey={i18nKey}
query={{ count: 0 }}
/>
/>
)
expect(container.textContent).toContain(expected)
})

test('should work with plural | count=2', () => {
const i18nKey = 'ns:withplural'
const expected = 'Number is bigger than one!'
const withPlural = {
'withplural': 'Singular',
'withplural_1': 'The number is ONE!',
'withplural_plural': 'Number is bigger than one!'
const withPlural = {
withplural: 'Singular',
withplural_1: 'The number is ONE!',
withplural_plural: 'Number is bigger than one!',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withPlural }}
i18nKey={i18nKey}
query={{ count: 2 }}
<TestEnglish
namespaces={{ ns: withPlural }}
i18nKey={i18nKey}
query={{ count: 2 }}
/>
)
expect(container.textContent).toContain(expected)
Expand All @@ -114,16 +113,16 @@ describe('useTranslation', () => {
test('should work with _2 | count=2', () => {
const i18nKey = 'ns:withplural'
const expected = 'The number is TWO!'
const withPlural = {
'withplural': 'Singular',
'withplural_2': 'The number is TWO!',
'withplural_plural': 'Number is bigger than one!'
const withPlural = {
withplural: 'Singular',
withplural_2: 'The number is TWO!',
withplural_plural: 'Number is bigger than one!',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withPlural }}
i18nKey={i18nKey}
query={{ count: 2 }}
<TestEnglish
namespaces={{ ns: withPlural }}
i18nKey={i18nKey}
query={{ count: 2 }}
/>
)
expect(container.textContent).toContain(expected)
Expand All @@ -136,19 +135,19 @@ describe('useTranslation', () => {
}

const expected = 'Example with template string'
const templateString = {
const templateString = {
'template-string': 'Example with template string',
}

const { container } = render(
<I18nProvider lang="en" namespaces={{ ns: templateString }} >
<I18nProvider lang="en" namespaces={{ ns: templateString }}>
<Inner />
</I18nProvider>
)
expect(container.textContent).toContain(expected)
})

test('should return the key as fallback', () => {
test('should return the key as fallback WITH PROVIDER', () => {
const Inner = () => {
const { t } = useTranslation()
const test = t('ns:template-string')
Expand All @@ -160,16 +159,33 @@ describe('useTranslation', () => {
}

const expected = 'ns:template-string | string'

const { container } = render(
<I18nProvider lang="en" namespaces={{}} >
<I18nProvider lang="en" namespaces={{}}>
<Inner />
</I18nProvider>
)
expect(container.textContent).toBe(expected)
})

test('should return the key as fallback using a template string', () => {
test('should return the key as fallback WITHOUT PROVIDER', () => {
const Inner = () => {
const { t } = useTranslation()
const test = t('ns:template-string')
return (
<>
{test} | {typeof test}
</>
)
}

const expected = 'ns:template-string | string'

const { container } = render(<Inner />)
expect(container.textContent).toBe(expected)
})

test('should return the key as fallback using a template string WITH PROVIDER', () => {
const Inner = () => {
const { t } = useTranslation()
const test = t`ns:template-string`
Expand All @@ -181,13 +197,30 @@ describe('useTranslation', () => {
}

const expected = 'ns:template-string | string'

const { container } = render(
<I18nProvider lang="en" namespaces={{}} >
<I18nProvider lang="en" namespaces={{}}>
<Inner />
</I18nProvider>
)
expect(container.textContent).toBe(expected)
})

test('should return the key as fallback using a template string WITHOUT PROVIDER', () => {
const Inner = () => {
const { t } = useTranslation()
const test = t`ns:template-string`
return (
<>
{test} | {typeof test}
</>
)
}

const expected = 'ns:template-string | string'

const { container } = render(<Inner />)
expect(container.textContent).toBe(expected)
})
})
})
Loading

0 comments on commit 30d55fc

Please sign in to comment.