import React from "react";
import TestRenderer from "react-test-renderer";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { createParseMarkup } from "../src/markup.ts";
import {
ReactLocalization,
LocalizationProvider,
Localized,
} from "../src/index.ts";
describe("Localized - overlay", () => {
test("< in text", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
true = 0 < 3 is true.
`)
);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
0 < 3 is true.
`);
});
test("& in text", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
megaman = Jumping & Shooting
`)
);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
Jumping & Shooting
`);
});
test("HTML entity", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
two = First · Second
`)
);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
First ยท Second
`);
});
test("one element is matched", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Click !
`)
);
const renderer = TestRenderer.create(
}}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
Click
!
`);
});
test("an element of different case is lowercased and matched", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Click !
`)
);
// The Button prop is capitalized whereas the }}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
Click
!
`);
});
test("two elements are matched", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Sign in or cancel.
`)
);
const renderer = TestRenderer.create(
,
cancel: ,
}}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
or
.
`);
});
test("unexpected child is reduced to text", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Sign in or cancel.
`)
);
const renderer = TestRenderer.create(
,
}}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
or
cancel
.
`);
});
test("element not found in the translation is removed", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Sign in.
`)
);
const renderer = TestRenderer.create(
,
cancel: ,
}}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
.
`);
});
test("attributes on translated children are ignored", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Click !
`)
);
const renderer = TestRenderer.create(
}}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
Click
!
`);
});
test("nested children are ignored", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Click !
`)
);
const renderer = TestRenderer.create(
}}
>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
Click
!
`);
});
test("non-React element prop is used in markup", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = Sign in.
`)
);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
Sign in
.
`);
});
});
describe("Localized - overlay of void elements", () => {
let parseMarkup = createParseMarkup();
test("void prop name, void prop value, void translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("void prop name, void prop value, empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("void prop name, void prop value, non-empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE Foo AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
// The opening tag is parsed as an HTMLInputElement and the closing
// is ignored. "Foo" is then parsed as a regular text node.
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
Foo AFTER
`);
});
test("void prop name, non-empty prop value, void translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("void prop name, non-empty prop value, empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("void prop name, non-empty prop value, non-empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE Foo AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
// The opening tag is parsed as an HTMLInputElement and the closing
// is ignored. "Foo" is then parsed as a regular text node.
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
Foo AFTER
`);
});
test("non-void prop name, void prop value, void translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
// XXX HTML parser breaks self-closing elements
// https://github.com/projectfluent/fluent.js/issues/188
// is parsed as an unclosed element. Everything that follows
// it becomes its children and is ignored because the passed as a
// prop is known to be void.
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
`);
});
test("non-void prop name, void prop value, empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("non-void prop name, void prop value, non-empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE Foo AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("non-void prop name, non-empty prop value, void translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
// XXX HTML parser breaks self-closing elements
// https://github.com/projectfluent/fluent.js/issues/188
// is parsed as an unclosed element. Everything that follows
// it becomes its children and is inserted into the passed as a prop.
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("non-void prop name, non-empty prop value, empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("non-void prop name, non-empty prop value, non-empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE Foo AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
Foo
AFTER
`);
});
test("custom prop name, void prop value, void translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
// XXX HTML parser breaks self-closing elements
// https://github.com/projectfluent/fluent.js/issues/188
// is parsed as an unclosed custom element.
// Everything that follows it becomes its children which are ignored because
// the passed as a prop is known to be void.
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
`);
});
test("custom prop name, void prop value, empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("custom prop name, void prop value, non-empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE Foo AFTER
`)
);
const renderer = TestRenderer.create(
}}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("custom prop name, non-empty prop value, void translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
// XXX HTML parser breaks self-closing elements
// https://github.com/projectfluent/fluent.js/issues/188
// is parsed as an unclosed custom element.
// Everything that follows it becomes its children which are inserted into
// the passed as a prop.
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("custom prop name, non-empty prop value, empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
AFTER
`);
});
test("custom prop name, non-empty prop value, non-empty translation", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = BEFORE Foo AFTER
`)
);
const renderer = TestRenderer.create(
Hardcoded }}>
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
BEFORE
Foo
AFTER
`);
});
});
describe("Localized - custom parseMarkup", () => {
test("disables the overlay logic if null", () => {
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
foo = test null markup parser
`)
);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
test <em>null markup parser</em>
`);
});
test("is called if defined in the context", () => {
let parseMarkupCalls = [];
function parseMarkup(str) {
parseMarkupCalls.push(str);
return createParseMarkup()(str);
}
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
# We must use an HTML tag to trigger the overlay logic.
foo = test custom markup parser
`)
);
const renderer = TestRenderer.create(
);
expect(parseMarkupCalls).toEqual(["test custom markup parser"]);
});
test("custom sanitization logic", () => {
function parseMarkup(str) {
return [
{
nodeName: "#text",
textContent: str.toUpperCase(),
},
];
}
const bundle = new FluentBundle();
bundle.addResource(
new FluentResource(`
# We must use an HTML tag to trigger the overlay logic.
foo = test custom markup parser
`)
);
const renderer = TestRenderer.create(
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`