import React from "react";
import TestRenderer from "react-test-renderer";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import {
ReactLocalization,
LocalizationProvider,
withLocalization,
} from "../src/index.ts";
import { vi } from "vitest";
function DummyComponent() {
return
;
}
describe("withLocalization", () => {
afterEach(() => {
vi.restoreAllMocks();
});
test("render inside of a LocalizationProvider", () => {
const EnhancedComponent = withLocalization(DummyComponent);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(``);
});
test("thows an error when rendered outside of a LocalizationProvider", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
const EnhancedComponent = withLocalization(DummyComponent);
expect(() => {
TestRenderer.create();
}).toThrow(
"withLocalization was used without wrapping it in a ."
);
// React also does a console.error.
expect(console.error).toHaveBeenCalled();
});
test("getString with access to the l10n context", () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
const bundle = new FluentBundle("en", { useIsolating: false });
const EnhancedComponent = withLocalization(DummyComponent);
bundle.addResource(
new FluentResource(`
foo = FOO
bar = BAR {$arg}
`)
);
const renderer = TestRenderer.create(
);
const { getString } = renderer.root.findByType(DummyComponent).props;
// Returns the translation.
expect(getString("foo", {})).toBe("FOO");
expect(getString("bar", { arg: "ARG" })).toBe("BAR ARG");
// It reports an error on formatting errors, but doesn't throw.
expect(getString("bar", {})).toBe("BAR {$arg}");
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
[
[
"[@fluent/react] ReferenceError: Unknown variable: $arg",
],
]
`);
});
test("getString with access to the l10n context, with fallback value", () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
const bundle = new FluentBundle("en", { useIsolating: false });
const EnhancedComponent = withLocalization(DummyComponent);
bundle.addResource(
new FluentResource(`
foo = FOO
bar = BAR {$arg}
`)
);
const renderer = TestRenderer.create(
);
const { getString } = renderer.root.findByType(DummyComponent).props;
// Returns the translation, even if fallback value provided.
expect(getString("foo", {}, "fallback")).toBe("FOO");
// Returns the fallback.
expect(getString("missing", {}, "fallback")).toBe("fallback");
expect(getString("bar", { arg: "ARG" })).toBe("BAR ARG");
// Doesn't throw on formatting errors.
expect(getString("bar", {})).toBe("BAR {$arg}");
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
[
[
"[@fluent/react] Error: The id "missing" did not match any messages in the localization bundles.",
],
[
"[@fluent/react] ReferenceError: Unknown variable: $arg",
],
]
`);
});
test("getString with an empty bundle list", () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
const EnhancedComponent = withLocalization(DummyComponent);
const renderer = TestRenderer.create(
);
const { getString } = renderer.root.findByType(DummyComponent).props;
// Returns the id if no fallback.
expect(getString("foo", { arg: 1 })).toBe("foo");
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
[
[
"[@fluent/react] Error: Attempting to get a string when no localization bundles are present.",
],
]
`);
});
test("getString with an empty bundle list, with fallback value", () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
const EnhancedComponent = withLocalization(DummyComponent);
const renderer = TestRenderer.create(
);
const { getString } = renderer.root.findByType(DummyComponent).props;
// Returns the fallback if provided.
expect(getString("foo", { arg: 1 }, "fallback message")).toBe(
"fallback message"
);
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
[
[
"[@fluent/react] Error: Attempting to get a string when no localization bundles are present.",
],
]
`);
});
test("getString with access to the l10n context, with message changes", () => {
const initialBundle = new FluentBundle();
const EnhancedComponent = withLocalization(({ getString }) =>
getString("foo")
);
initialBundle.addResource(new FluentResource("foo = FOO"));
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`"FOO"`);
const newBundle = new FluentBundle();
newBundle.addResource(new FluentResource("foo = BAR"));
renderer.update(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`"BAR"`);
});
});