-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimization for optional lng prop for useTranslation, should now pre…
…vent missings when lazy loading translations #1637
- Loading branch information
Showing
9 changed files
with
216 additions
and
40 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,40 @@ | ||
class Backend { | ||
constructor(services, options = {}) { | ||
this.init(services, options); | ||
this.type = 'backend'; | ||
this.queue = []; | ||
} | ||
|
||
init(services, options) { | ||
this.services = services; | ||
this.options = options; | ||
} | ||
|
||
read(language, namespace, callback) { | ||
this.queue.push({ language, namespace, callback }); | ||
} | ||
|
||
flush(what) { | ||
let q = [...this.queue]; | ||
if (what) { | ||
const filterFor = []; | ||
if (what.language) filterFor.push('language'); | ||
if (what.namespace) filterFor.push('namespace'); | ||
if (filterFor.length > 0) { | ||
q = q.filter((item) => { | ||
const allOk = filterFor.map((ff) => item[ff] === what[ff]).every((r) => r); | ||
if (allOk) return true; | ||
return false; | ||
}); | ||
} | ||
} | ||
q.forEach((item) => { | ||
this.queue.splice(this.queue.indexOf(item), 1); | ||
item.callback(null, { | ||
key1: `${item.language}/${item.namespace} for key1`, | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default Backend; |
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,85 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import i18n from './i18n'; | ||
import BackendMock from './lngAwareBackendMock'; | ||
import { useTranslation } from '../src/useTranslation'; | ||
|
||
jest.unmock('../src/useTranslation'); | ||
|
||
describe('useTranslation loading ns with lng via props', () => { | ||
let newI18n; | ||
let backend; | ||
|
||
beforeEach(() => { | ||
newI18n = i18n.createInstance(); | ||
backend = new BackendMock(); | ||
newI18n.use(backend).init({ | ||
lng: 'en', | ||
fallbackLng: 'en', | ||
}); | ||
}); | ||
|
||
it('should wait for correct translation with suspense', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useTranslation('common', { i18n: newI18n, useSuspense: true, lng: 'de' }), | ||
); | ||
expect(result.all).toHaveLength(0); | ||
backend.flush(); | ||
await waitForNextUpdate(); | ||
const { t } = result.current; | ||
expect(t('key1')).toBe('de/common for key1'); | ||
}); | ||
|
||
it('should wait for correct translation without suspense', async () => { | ||
const { result } = renderHook(() => | ||
useTranslation('common', { i18n: newI18n, useSuspense: false, lng: 'it' }), | ||
); | ||
const { t } = result.current; | ||
expect(t('key1')).toBe('key1'); | ||
|
||
backend.flush(); | ||
expect(t('key1')).toBe('it/common for key1'); | ||
}); | ||
|
||
it('should return defaultValue if resources not yet loaded', async () => { | ||
const { result } = renderHook(() => | ||
useTranslation('common', { i18n: newI18n, useSuspense: false, lng: 'fr' }), | ||
); | ||
const { t } = result.current; | ||
expect(t('key1', 'my default value')).toBe('my default value'); | ||
expect(t('key1', { defaultValue: 'my default value' })).toBe('my default value'); | ||
|
||
backend.flush({ language: 'en' }); | ||
expect(t('key1', 'my default value')).toBe('en/common for key1'); | ||
expect(t('key1', { defaultValue: 'my default value' })).toBe('en/common for key1'); | ||
|
||
backend.flush({ language: 'fr' }); | ||
expect(t('key1', 'my default value')).toBe('fr/common for key1'); | ||
expect(t('key1', { defaultValue: 'my default value' })).toBe('fr/common for key1'); | ||
}); | ||
|
||
it('should correctly return and render correct tranlations in multiple useTranslation usages', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useTranslation('newns', { i18n: newI18n, useSuspense: true, lng: 'pt' }), | ||
); | ||
backend.flush(); | ||
await waitForNextUpdate(); | ||
const { t } = result.current; | ||
expect(t('key1')).toBe('pt/newns for key1'); | ||
|
||
const retDe = renderHook(() => | ||
useTranslation('newns', { i18n: newI18n, useSuspense: true, lng: 'de' }), | ||
); | ||
backend.flush({ language: 'de' }); | ||
await retDe.waitForNextUpdate(); | ||
const { t: tDE } = retDe.result.current; | ||
expect(tDE('key1')).toBe('de/newns for key1'); | ||
|
||
const retPT = renderHook(() => | ||
useTranslation('newns', { i18n: newI18n, useSuspense: true, lng: 'pt' }), | ||
); | ||
backend.flush({ language: 'pt' }); | ||
// await retPT.waitForNextUpdate(); // already loaded | ||
const { t: tPT } = retPT.result.current; | ||
expect(tPT('key1')).toBe('pt/newns for key1'); | ||
}); | ||
}); |