-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.tsx
135 lines (113 loc) · 4.1 KB
/
index.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* global describe, expect, it */
import { mount } from 'enzyme'
import * as React from 'react'
import { createI18nContext } from './index'
interface IOutterProps {
name: string
}
describe('I18nProvider and translated', () => {
describe('integration', () => {
describe('when locale and keys are strings', () => {
type Locale = string
const makeT = (locale: Locale) => (
locale === 'de' ?
((key: string) => key === 'welcome' ? 'Herzlich willkommen' : 'NOOOOOOOOOOOOOOOO') :
// tslint:disable-next-line:variable-name
((_key: string) => 'NOOOOOOOOOOOOOOOO')
)
const TranslatedComponent = ({name}: IOutterProps) => (
<I18nConsumer>
{(i18n) =>
<span>{ i18n.t('welcome') } {name}! (locale={i18n.locale})</span>
}
</I18nConsumer>
)
const { I18nProvider, I18nConsumer } = createI18nContext({locale: 'de', t: () => 'ja!'})
const App = () => <I18nProvider locale={'de'} makeT={makeT}>
<TranslatedComponent name={'lieber Nutzer'}/>
</I18nProvider>
it('works', () => {
const component = mount(<App/>)
expect(component.html()).toBe('<span>Herzlich willkommen lieber Nutzer! (locale=de)</span>')
})
})
describe('when locale and keys are enums', () => {
enum Locale { DE, EN }
enum Key { WELCOME }
// tslint:disable-next-line:variable-name
const germanT = (_key: Key) => 'jawohl!'
// tslint:disable-next-line:variable-name
const englishT = (_key: Key) => 'what he says?'
const makeT = (locale: Locale) => {
switch (locale) {
case Locale.DE: return germanT
case Locale.EN: return englishT
default:
// tslint:disable-next-line:variable-name
const _exhaustiveCheck: never = locale
return _exhaustiveCheck
}
}
const TranslatedComponent = ({name}: IOutterProps) => (
<I18nConsumer>
{(i18n) =>
<span>{ i18n.t(Key.WELCOME) } {name}! (locale={i18n.locale})</span>
}
</I18nConsumer>
)
const { I18nProvider, I18nConsumer } = createI18nContext(
// tslint:disable-next-line:variable-name
{locale: Locale.DE, t: (_key: Key) => 'ja!'}
)
const App = () => <I18nProvider locale={Locale.DE} makeT={makeT}>
<TranslatedComponent name={'lieber Nutzer'}/>
</I18nProvider>
it('works', () => {
const component = mount(<App/>)
expect(component.html()).toBe('<span>jawohl! lieber Nutzer! (locale=0)</span>')
})
})
describe('when we need context like the name of a person', () => {
enum Locale { DE, EN }
enum Key { WELCOME }
interface ITArgs {
key: Key
context: {
name: string
}
}
// tslint:disable-next-line:variable-name
const germanT = (args: ITArgs) => `jawohl, ${args.context.name}!`
// tslint:disable-next-line:variable-name
const englishT = (args: ITArgs) => `what he says, ${args.context.name}?`
const makeT = (locale: Locale) => {
switch (locale) {
case Locale.DE: return germanT
case Locale.EN: return englishT
default:
// tslint:disable-next-line:variable-name
const _exhaustiveCheck: never = locale
return _exhaustiveCheck
}
}
const TranslatedComponent = ({name}: IOutterProps) => (
<I18nConsumer>
{(i18n) =>
<span>{ i18n.t({key: Key.WELCOME, context: {name}}) } (locale={i18n.locale})</span>
}
</I18nConsumer>
)
const { I18nProvider, I18nConsumer } = createI18nContext(
// tslint:disable-next-line:variable-name
{locale: Locale.DE, t: (_args) => 'ja!'}
)
const App = () => <I18nProvider locale={Locale.DE} makeT={makeT}>
<TranslatedComponent name={'lieber Nutzer'}/>
</I18nProvider>
it('works', () => {
const component = mount(<App/>)
expect(component.html()).toBe('<span>jawohl, lieber Nutzer! (locale=0)</span>')
})
})
})
})