Files
owl/tests/compiler/translation.test.ts
T

90 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-12-20 16:19:07 +01:00
import { Component, mount, xml } from "../../src";
2021-12-20 11:19:59 +01:00
import { makeTestFixture, snapshotEverything } from "../helpers";
2021-10-13 16:49:34 +02:00
let fixture: HTMLElement;
2021-12-20 11:19:59 +01:00
snapshotEverything();
2021-10-13 16:49:34 +02:00
beforeEach(() => {
fixture = makeTestFixture();
});
describe("translation support", () => {
test("can translate node content", async () => {
class SomeComponent extends Component {
static template = xml`<div>word</div>`;
}
2021-12-20 11:19:59 +01:00
await mount(SomeComponent, fixture, {
2021-10-13 16:49:34 +02:00
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
});
2021-12-20 11:19:59 +01:00
expect(fixture.innerHTML).toBe("<div>mot</div>");
2021-10-13 16:49:34 +02:00
});
test("does not translate node content if disabled", async () => {
class SomeComponent extends Component {
static template = xml`
<div>
<span>word</span>
<span t-translation="off">word</span>
</div>
`;
}
2021-12-20 11:19:59 +01:00
await mount(SomeComponent, fixture, {
2021-10-13 16:49:34 +02:00
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
});
2021-12-20 11:19:59 +01:00
expect(fixture.innerHTML).toBe("<div><span>mot</span><span>word</span></div>");
2021-10-13 16:49:34 +02:00
});
test("some attributes are translated", async () => {
class SomeComponent extends Component {
static template = xml`
<div>
<p label="word">word</p>
<p title="word">word</p>
<p placeholder="word">word</p>
<p alt="word">word</p>
<p something="word">word</p>
</div>
`;
}
2021-12-20 11:19:59 +01:00
await mount(SomeComponent, fixture, {
2021-10-13 16:49:34 +02:00
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
});
2021-12-20 11:19:59 +01:00
expect(fixture.innerHTML).toBe(
2021-10-13 16:49:34 +02:00
'<div><p label="mot">mot</p><p title="mot">mot</p><p placeholder="mot">mot</p><p alt="mot">mot</p><p something="word">mot</p></div>'
);
});
test("can set translatable attributes", async () => {
class SomeComponent extends Component {
static template = xml`
<div tomato="word" potato="word" title="word">text</div>
`;
}
2021-12-20 11:19:59 +01:00
await mount(SomeComponent, fixture, {
2021-10-13 16:49:34 +02:00
translateFn: (expr: string) => (expr === "word" ? "mot" : expr),
translatableAttributes: ["potato"],
});
2021-12-20 11:19:59 +01:00
expect(fixture.innerHTML).toBe('<div tomato="word" potato="mot" title="word">text</div>');
2021-10-13 16:49:34 +02:00
});
test("translation is done on the trimmed text, with extra spaces readded after", async () => {
class SomeComponent extends Component {
static template = xml`
<div> word </div>
`;
}
const translateFn = jest.fn((expr: string) => (expr === "word" ? "mot" : expr));
2021-12-20 11:19:59 +01:00
await mount(SomeComponent, fixture, { translateFn });
expect(fixture.innerHTML).toBe("<div> mot </div>");
2021-10-13 16:49:34 +02:00
expect(translateFn).toHaveBeenCalledWith("word");
});
});