-
Notifications
You must be signed in to change notification settings - Fork 0
/
I18NProvider.test.tsx
201 lines (180 loc) · 5.34 KB
/
I18NProvider.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as React from 'react';
import {render} from '@testing-library/react';
import {I18NComponentProps, I18NProvider, LanguageBundleSet, withI18N} from "../src";
type Props = React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
const TextFC = (props: Props) => {
return <span {...props}/>
}
class TextCC extends React.Component<Props, any> {
render() {
return <span {...this.props}/>
}
}
const I18NTextFC = withI18N<Props & I18NComponentProps>(TextFC);
const I18NTextCC = withI18N<Props & I18NComponentProps>(TextCC);
const RootLanguages:LanguageBundleSet = {
en: {
hello: "Hello.1",
role: "Role.1",
args: (arg1:any, arg2:any) => `Has ${arg1} and ${arg2}`,
bold: "Some **bold** text",
italic: "Some *italic* text",
strikethrough: "Some ~~strikethrough~~ text",
code: "Some `code` text",
link: "A [link](url) here",
mixed: "Some ***bold italic*** `code` [link](url) ~~strikethrough~~ here",
},
other: {
hello: "Hello.2",
role: "Role.2"
}
}
const NestedLanguages:LanguageBundleSet = {
en: {
hello: "Hello.nested.1",
},
other: {
hello: "Hello.nested.2",
}
}
describe('it', () => {
it('works with class components', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextCC i18n={{children:"hello"}}/>
</I18NProvider>
);
dom.getByText("Hello.1");
});
it('renders to children by default', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"hello"} />
</I18NProvider>
);
dom.getByText("Hello.1");
});
it('renders to children explicitly', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={{children:"hello"}}/>
</I18NProvider>
);
dom.getByText("Hello.1");
});
it('renders multiple properties', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={[
{children:"hello"},
{role:"role"}
]}/>
</I18NProvider>
);
dom.getByText("Hello.1");
dom.getByRole("Role.1");
});
it('supports args', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={{children:"args", args:[1, "abc"]}}/>
</I18NProvider>
);
dom.getByText("Has 1 and abc");
});
it('supports nested bundles and inherits lang', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NProvider bundles={NestedLanguages}>
<I18NTextFC i18n={[
{children:"hello"},
{role:"role"}
]}/>
</I18NProvider>
</I18NProvider>
);
dom.getByText("Hello.nested.1");
dom.getByRole("Role.1");
});
it('supports nested bundles and overrides lang', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NProvider bundles={NestedLanguages} lang={"other"}>
<I18NTextFC i18n={[
{children:"hello"},
{role:"role"}
]}/>
</I18NProvider>
</I18NProvider>
);
dom.getByText("Hello.nested.2");
dom.getByRole("Role.2");
});
it('supports bold markdown', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"bold"}/>
</I18NProvider>
);
// @ts-ignore
dom.getByText(( content, element) => {
return element?.innerHTML === "Some <strong>bold</strong> text"
});
});
it('supports italic markdown', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"italic"}/>
</I18NProvider>
);
// @ts-ignore
dom.getByText(( content, element) => {
return element?.innerHTML === "Some <em>italic</em> text"
});
});
it('supports strikethrough markdown', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"strikethrough"}/>
</I18NProvider>
);
// @ts-ignore
dom.getByText(( content, element) => {
return element?.innerHTML === "Some <del>strikethrough</del> text"
});
});
it('supports code markdown', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"code"}/>
</I18NProvider>
);
// @ts-ignore
dom.getByText(( content, element) => {
return element?.innerHTML === "Some <code>code</code> text"
});
});
it('supports link markdown', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"link"}/>
</I18NProvider>
);
// @ts-ignore
dom.getByText(( content, element) => {
return element?.innerHTML === 'A <a href="url">link</a> here'
});
});
it('supports mixed markdown', () => {
const dom = render(
<I18NProvider bundles={RootLanguages} lang={"en"}>
<I18NTextFC i18n={"mixed"}/>
</I18NProvider>
);
const html = `Some <em><strong>bold italic</strong></em> <code>code</code> <a href="url">link</a> <del>strikethrough</del> here`;
// @ts-ignore
dom.getByText(( content, element) => {
return element?.innerHTML.replace(/(\r?\n)/g, " ") === html
});
});
});