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("", () => {
const Provider: React.SFC<{ locale?: string }> = ({
children,
locale = "ru",
}) => (
{children}
);
const StatefulProvider: React.SFC<{ locale?: string }> = ({
children,
locale = "ru",
}) => (
{children}
);
test("translates message inside TargemProvider", () => {
const res1 = render(
,
);
expect(res1.getByText("Текущая локаль")).toBeInTheDocument();
const res2 = render(
Hello, World!
,
);
expect(res2.getByText("Привет, Мир!")).toBeInTheDocument();
});
test("allows wrapping translation with a component", () => {
const res = render(
,
);
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(
,
);
expect(
res.getByText("Дорогой Alex. У вас 2 непрочитанных сообщения."),
).toBeInTheDocument();
});
test("translates pluralized message with count=0", () => {
const res = render(
,
);
expect(
res.getByText("Dear, Alex. You have 0 unread messages"),
).toBeInTheDocument();
});
test("translates message inside TargemStatefulProvider", () => {
const res = render(
,
);
expect(res.getByText("Я люблю тебя, Alex")).toBeInTheDocument();
});
test("translates on the fly when locale changes", () => {
const res = render(
,
);
expect(res.getByText("Hello, World!")).toBeInTheDocument();
res.rerender(
,
);
expect(res.getByText("Привет, Мир!")).toBeInTheDocument();
res.rerender(
,
);
expect(res.getByText("שלום עולם!")).toBeInTheDocument();
});
test("formats count and scope numbers when props.formatNumbers is passed", () => {
const res = render(
,
);
expect(
res.getByText("You have 5,000 answers, your score is: 1,000.57"),
).toBeInTheDocument();
res.rerender(
,
);
// `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(
,
);
expect(res.getByText("5,000")).toBeInTheDocument();
res.rerender(
,
);
expect(res.getByText("5,000")).toBeInTheDocument();
res.rerender(
,
);
expect(res.getByText("5 000,12")).toBeInTheDocument();
});
});