-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinytranslate.spec.tsx
71 lines (64 loc) · 1.65 KB
/
tinytranslate.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
import { render, screen } from "@testing-library/react";
import React from "react";
import {
TranslationContext,
TranslationProvider,
useTranslation,
} from "../src/tinytranslate";
const mockTranslations = {
en: {
locale: "en-US",
messages: {
hello: "hello en",
hello_name: "hello en {name}",
},
},
zh: {
locale: "CN",
messages: {
hello: "hello cn",
hello_name: "hello cn {name}",
},
},
};
describe("tinytranslate", () => {
it("translate provider will mount its children", () => {
render(
<TranslationProvider translations={mockTranslations} locale="en">
<div>hello world</div>
</TranslationProvider>
);
expect(screen.getByText(/hello world/)).toBeTruthy();
});
it("will render translation based on locale", () => {
const Child = () => {
const translate = useTranslation();
return (
<>
{translate("hello")}
<div>{translate("hello_name", { name: "foobar" })}</div>
</>
);
};
render(
<TranslationProvider translations={mockTranslations} locale="en">
<Child />
</TranslationProvider>
);
expect(screen.getByText("hello en")).toBeTruthy();
expect(screen.getByText("hello en foobar")).toBeTruthy();
});
it("will throw error if context value is null", () => {
const Child = () => {
useTranslation();
return <></>;
};
const renderProvider = () =>
render(
<TranslationContext.Provider value={null}>
<Child />
</TranslationContext.Provider>
);
expect(renderProvider).toThrowError("Missing TranslationProvider");
});
});