-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT.spec.tsx
187 lines (169 loc) · 5.22 KB
/
T.spec.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
import { render } from "@testing-library/react";
import * as React from "react";
import translationsJson from "@test-utils/fixtures/translations.json";
import { T } from "./T";
import { TargemProvider } from "./TargemProvider";
import { TargemStatefulProvider } from "./TargemStatefulProvider";
describe("<T />", () => {
const Provider: React.SFC<{ locale?: string }> = ({
children,
locale = "ru",
}) => (
<TargemProvider translations={translationsJson} locale={locale}>
{children}
</TargemProvider>
);
const StatefulProvider: React.SFC<{ locale?: string }> = ({
children,
locale = "ru",
}) => (
<TargemStatefulProvider
detectLocale={false}
defaultLocale={locale}
translations={translationsJson}>
{children}
</TargemStatefulProvider>
);
test("translates message inside TargemProvider", () => {
const res1 = render(
<Provider>
<T message="Current locale" />
</Provider>,
);
expect(res1.getByText("Текущая локаль")).toBeInTheDocument();
const res2 = render(
<Provider>
<T>Hello, World!</T>
</Provider>,
);
expect(res2.getByText("Привет, Мир!")).toBeInTheDocument();
});
test("allows wrapping translation with a component", () => {
const res = render(
<Provider>
<T
message="Current locale"
component="a"
componentProps={{ href: "www.google.com" }}
/>
</Provider>,
);
const el = res.getByText("Текущая локаль");
expect(el).toBeInTheDocument();
expect(el.nodeName).toBe("A");
expect(el.getAttribute("href")).toBe("www.google.com");
});
test("translates pluralized message", () => {
const res = render(
<Provider>
<T
message="Dear, {{ name }}. You have one unread message"
messagePlural="Dear, {{ name }}. You have {{ count }} unread messages"
context="someContext"
count={2}
scope={{ name: "Alex" }}
/>
</Provider>,
);
expect(
res.getByText("Дорогой Alex. У вас 2 непрочитанных сообщения."),
).toBeInTheDocument();
});
test("translates pluralized message with count=0", () => {
const res = render(
<Provider locale="en-GB">
<T
message="Dear, {{ name }}. You have one unread message"
messagePlural="Dear, {{ name }}. You have {{ count }} unread messages"
context="someContext"
count={0}
scope={{ name: "Alex" }}
/>
</Provider>,
);
expect(
res.getByText("Dear, Alex. You have 0 unread messages"),
).toBeInTheDocument();
});
test("translates message inside TargemStatefulProvider", () => {
const res = render(
<StatefulProvider>
<T
message="I love you, {{ name }}"
context="someContext"
scope={{ name: "Alex" }}
/>
</StatefulProvider>,
);
expect(res.getByText("Я люблю тебя, Alex")).toBeInTheDocument();
});
test("translates on the fly when locale changes", () => {
const res = render(
<Provider locale="en-GB">
<T message="Hello, World!" />
</Provider>,
);
expect(res.getByText("Hello, World!")).toBeInTheDocument();
res.rerender(
<Provider locale="ru">
<T message="Hello, World!" />
</Provider>,
);
expect(res.getByText("Привет, Мир!")).toBeInTheDocument();
res.rerender(
<Provider locale="he">
<T message="Hello, World!" />
</Provider>,
);
expect(res.getByText("שלום עולם!")).toBeInTheDocument();
});
test("formats count and scope numbers when props.formatNumbers is passed", () => {
const res = render(
<Provider locale="en-GB">
<T
message="You have one answer, your score is: {{ score }}"
count={5000}
scope={{ score: 1000.5678 }}
formatNumbers={{ maximumFractionDigits: 2 }}
/>
</Provider>,
);
expect(
res.getByText("You have 5,000 answers, your score is: 1,000.57"),
).toBeInTheDocument();
res.rerender(
<Provider locale="ru">
<T
message="You have one answer, your score is: {{ score }}"
count={5000}
scope={{ score: 1000.5689 }}
formatNumbers
/>
</Provider>,
);
// `maximumFractionDigits: 3` by default in `Intl.NumberFormatOptions`
expect(
res.getByText("У Вас 5 000 ответов, Ваш счёт: 1 000,569"),
).toBeInTheDocument();
});
test("allows to only format number with optional NumberFormat options", () => {
const res = render(
<Provider locale="en-GB">
<T count={5000} formatNumbers />
</Provider>,
);
expect(res.getByText("5,000")).toBeInTheDocument();
res.rerender(
<Provider locale="en-GB">
<T count={5000.3456} formatNumbers={{ maximumFractionDigits: 0 }} />
</Provider>,
);
expect(res.getByText("5,000")).toBeInTheDocument();
res.rerender(
<Provider locale="ru">
<T count={5000.12345} formatNumbers={{ maximumFractionDigits: 2 }} />
</Provider>,
);
expect(res.getByText("5 000,12")).toBeInTheDocument();
});
});