| | |
| | |
| | |
| |
|
| | import ReactDOM from 'react-dom'; |
| | import { act } from 'react-dom/test-utils'; |
| | import i18n, { useTranslate } from '..'; |
| |
|
| | function Label() { |
| | const translate = useTranslate(); |
| | return translate( 'hook (%(lang)s)', { args: { lang: translate.localeSlug } } ); |
| | } |
| |
|
| | describe( 'useTranslate()', () => { |
| | let container; |
| |
|
| | beforeEach( () => { |
| | |
| | act( () => { |
| | i18n.setLocale(); |
| | } ); |
| |
|
| | |
| | container = document.createElement( 'div' ); |
| | document.body.appendChild( container ); |
| | } ); |
| |
|
| | afterEach( () => { |
| | |
| | ReactDOM.unmountComponentAtNode( container ); |
| | document.body.removeChild( container ); |
| | container = null; |
| | } ); |
| |
|
| | test( 'renders a translated string', () => { |
| | |
| | i18n.setLocale( { |
| | '': { localeSlug: 'cs' }, |
| | 'hook (%(lang)s)': [ 'háček (%(lang)s)' ], |
| | } ); |
| |
|
| | |
| | act( () => { |
| | ReactDOM.render( <Label />, container ); |
| | } ); |
| |
|
| | |
| | expect( container.textContent ).toBe( 'háček (cs)' ); |
| | } ); |
| |
|
| | test( 'rerenders after locale change', () => { |
| | |
| | act( () => { |
| | ReactDOM.render( <Label />, container ); |
| | } ); |
| |
|
| | expect( container.textContent ).toBe( 'hook (en)' ); |
| |
|
| | |
| | act( () => { |
| | i18n.setLocale( { |
| | '': { localeSlug: 'cs' }, |
| | 'hook (%(lang)s)': [ 'háček (%(lang)s)' ], |
| | } ); |
| | } ); |
| |
|
| | expect( container.textContent ).toBe( 'háček (cs)' ); |
| | } ); |
| |
|
| | test( 'rerenders after update of current locale translations', () => { |
| | |
| | act( () => { |
| | i18n.setLocale( { |
| | '': { localeSlug: 'cs' }, |
| | 'hook (%(lang)s)': [ 'háček (%(lang)s)' ], |
| | } ); |
| | } ); |
| |
|
| | |
| | act( () => { |
| | ReactDOM.render( <Label />, container ); |
| | } ); |
| |
|
| | |
| | expect( container.textContent ).toBe( 'háček (cs)' ); |
| |
|
| | |
| | act( () => { |
| | i18n.setLocale( { |
| | '': { localeSlug: 'cs' }, |
| | 'hook (%(lang)s)': [ 'hák (%(lang)s)' ], |
| | } ); |
| | } ); |
| |
|
| | |
| | expect( container.textContent ).toBe( 'hák (cs)' ); |
| | } ); |
| | } ); |
| |
|