import { App, Component } from "../../src";
import { makeTestFixture, snapshotApp } from "../helpers";
import { xml } from "../../src/tags";
let fixture: HTMLElement;
beforeEach(() => {
fixture = makeTestFixture();
});
describe("translation support", () => {
test("can translate node content", async () => {
class SomeComponent extends Component {
static template = xml`
word
`;
}
const app = new App(SomeComponent);
app.configure({
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
});
const comp = await app.mount(fixture);
const el = comp.el as HTMLElement;
expect(el.outerHTML).toBe("mot
");
snapshotApp(app);
});
test("does not translate node content if disabled", async () => {
class SomeComponent extends Component {
static template = xml`
word
word
`;
}
const app = new App(SomeComponent);
app.configure({
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
});
const comp = await app.mount(fixture);
const el = comp.el as HTMLElement;
expect(el.outerHTML).toBe("motword
");
snapshotApp(app);
});
test("some attributes are translated", async () => {
class SomeComponent extends Component {
static template = xml`
`;
}
const app = new App(SomeComponent);
app.configure({
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
});
const comp = await app.mount(fixture);
const el = comp.el as HTMLElement;
expect(el.outerHTML).toBe(
''
);
snapshotApp(app);
});
test("can set translatable attributes", async () => {
class SomeComponent extends Component {
static template = xml`
text
`;
}
const app = new App(SomeComponent);
app.configure({
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
translatableAttributes: ["potato"],
});
const comp = await app.mount(fixture);
const el = comp.el as HTMLElement;
expect(el.outerHTML).toBe('text
');
snapshotApp(app);
});
test("translation is done on the trimmed text, with extra spaces readded after", async () => {
class SomeComponent extends Component {
static template = xml`
word
`;
}
const translateFn = jest.fn((expr: string) => (expr === "word" ? "mot" : expr));
const app = new App(SomeComponent);
app.configure({ translateFn });
const comp = await app.mount(fixture);
const el = comp.el as HTMLElement;
expect(el.outerHTML).toBe(" mot
");
expect(translateFn).toHaveBeenCalledWith("word");
snapshotApp(app);
});
});