|
| 1 | +import React from "react"; |
| 2 | +import TestRenderer from "react-test-renderer"; |
| 3 | +import { FluentBundle, FluentResource } from "@fluent/bundle"; |
| 4 | +import { ReactLocalization, LocalizationProvider, useLocalization } from "../esm/index"; |
| 5 | + |
| 6 | +function DummyComponent() { |
| 7 | + const { l10n } = useLocalization(); |
| 8 | + |
| 9 | + return ( |
| 10 | + <p>{l10n.getString('foo')}</p> |
| 11 | + ); |
| 12 | +} |
| 13 | + |
| 14 | +describe("useLocalization", () => { |
| 15 | + test("render inside of a LocalizationProvider", () => { |
| 16 | + const renderer = TestRenderer.create( |
| 17 | + <LocalizationProvider l10n={new ReactLocalization([])}> |
| 18 | + <DummyComponent /> |
| 19 | + </LocalizationProvider> |
| 20 | + ); |
| 21 | + expect(renderer.toJSON()).toMatchInlineSnapshot(` |
| 22 | + <p> |
| 23 | + foo |
| 24 | + </p> |
| 25 | + `); |
| 26 | + }); |
| 27 | + |
| 28 | + test("render outside of a LocalizationProvider", () => { |
| 29 | + const renderer = TestRenderer.create(<DummyComponent />); |
| 30 | + expect(renderer.toJSON()).toMatchInlineSnapshot(` |
| 31 | + <p> |
| 32 | + foo |
| 33 | + </p> |
| 34 | + `); |
| 35 | + }); |
| 36 | + |
| 37 | + test("useLocalization should exposes getString from ReactLocalization", () => { |
| 38 | + const bundle = new FluentBundle("en", { useIsolating: false }); |
| 39 | + bundle.addResource( |
| 40 | + new FluentResource(` |
| 41 | +foo = FOO |
| 42 | +`) |
| 43 | + ); |
| 44 | + |
| 45 | + const renderer = TestRenderer.create( |
| 46 | + <LocalizationProvider l10n={new ReactLocalization([bundle])}> |
| 47 | + <DummyComponent /> |
| 48 | + </LocalizationProvider> |
| 49 | + ); |
| 50 | + |
| 51 | + expect(renderer.toJSON()).toMatchInlineSnapshot(` |
| 52 | + <p> |
| 53 | + FOO |
| 54 | + </p> |
| 55 | + `); |
| 56 | + }); |
| 57 | +}); |
0 commit comments