2024-11-07 12:26:45 +01:00
|
|
|
import { App, Component, xml } from "../../src";
|
|
|
|
|
import { makeTestFixture, snapshotEverything } from "../helpers";
|
|
|
|
|
|
|
|
|
|
let fixture: HTMLElement;
|
|
|
|
|
|
|
|
|
|
snapshotEverything();
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
fixture = makeTestFixture();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("t-custom", () => {
|
|
|
|
|
test("can use t-custom directive on a node", async () => {
|
|
|
|
|
const steps: string[] = [];
|
|
|
|
|
class SomeComponent extends Component {
|
|
|
|
|
static template = xml`<div t-custom-plop="click" class="my-div"/>`;
|
|
|
|
|
click() {
|
|
|
|
|
steps.push("clicked");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const app = new App(SomeComponent, {
|
|
|
|
|
customDirectives: {
|
|
|
|
|
plop: (node, value) => {
|
|
|
|
|
node.setAttribute("t-on-click", value);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await app.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
|
|
|
|
fixture.querySelector("div")!.click();
|
|
|
|
|
expect(steps).toEqual(["clicked"]);
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-25 15:27:27 +01:00
|
|
|
test("can use t-custom directive with modifiers on a node", async () => {
|
2024-11-07 12:26:45 +01:00
|
|
|
const steps: string[] = [];
|
|
|
|
|
class SomeComponent extends Component {
|
2024-11-25 15:27:27 +01:00
|
|
|
static template = xml`<div t-custom-plop.mouse.stop="click" class="my-div"/>`;
|
2024-11-07 12:26:45 +01:00
|
|
|
click() {
|
|
|
|
|
steps.push("clicked");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const app = new App(SomeComponent, {
|
|
|
|
|
customDirectives: {
|
2024-11-25 15:27:27 +01:00
|
|
|
plop: (node, value, modifiers) => {
|
2024-11-07 12:26:45 +01:00
|
|
|
node.setAttribute("t-on-click", value);
|
2024-11-25 15:27:27 +01:00
|
|
|
for (let mod of modifiers) {
|
|
|
|
|
steps.push(mod);
|
|
|
|
|
}
|
2024-11-07 12:26:45 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await app.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
|
|
|
|
|
fixture.querySelector("div")!.click();
|
2024-11-25 15:27:27 +01:00
|
|
|
expect(steps).toEqual(["mouse", "stop", "clicked"]);
|
2024-11-07 12:26:45 +01:00
|
|
|
});
|
|
|
|
|
});
|