import { App, Component, mount, useState, xml } from "../../src/index";
import { isDirectChildOf, makeTestFixture, nextTick, snapshotEverything } from "../helpers";
snapshotEverything();
let fixture: HTMLElement;
beforeEach(() => {
fixture = makeTestFixture();
});
describe("t-call", () => {
test("dynamic t-call", async () => {
class Root extends Component {
static template = xml`
owl
`;
current = useState({ template: "foo" });
}
const root = await mount(Root, fixture, {
templates: `
foo
bar
`,
});
expect(fixture.innerHTML).toBe("
foo
");
root.current.template = "bar";
await nextTick();
expect(fixture.innerHTML).toBe("bar");
});
test("sub components in two t-calls", async () => {
class Child extends Component {
static template = xml``;
}
class Parent extends Component {
static template = xml`
`;
static components = { Child };
state = useState({ val: 1 });
}
const app = new App(Parent);
app.addTemplate("sub", ``);
const parent = await app.mount(fixture);
expect(fixture.innerHTML).toBe("1");
parent.state.val = 2;
await nextTick();
expect(fixture.innerHTML).toBe("2
");
});
test("handlers are properly bound through a t-call", async () => {
let parent: any;
const subTemplate = xml`lucas
`;
class Parent extends Component {
static template = xml`
`;
counter = 0;
update() {
expect(this).toBe(parent);
this.counter++;
this.render();
}
}
parent = await mount(Parent, fixture);
expect(fixture.innerHTML).toBe("");
fixture.querySelector("p")!.click();
await nextTick();
expect(fixture.innerHTML).toBe("");
});
test("handlers are properly bound through a dynamic t-call", async () => {
let parent: any;
const subTemplate = xml`lucas
`;
class Parent extends Component {
static template = xml`
`;
counter = 0;
update() {
expect(this).toBe(parent);
this.counter++;
this.render();
}
}
parent = await mount(Parent, fixture);
expect(fixture.innerHTML).toBe("");
fixture.querySelector("p")!.click();
await nextTick();
expect(fixture.innerHTML).toBe("");
});
test("parent is set within t-call", async () => {
const sub = xml``;
let child: any = null;
class Child extends Component {
static template = xml`lucas`;
setup() {
child = this;
}
}
class Parent extends Component {
static components = { Child };
static template = xml`
`;
}
const parent = await mount(Parent, fixture);
expect(fixture.innerHTML).toBe("lucas
");
expect(isDirectChildOf(child, parent)).toBeTruthy();
});
test("t-call in t-foreach and children component", async () => {
const sub = xml``;
class Child extends Component {
static template = xml``;
}
class Parent extends Component {
static components = { Child };
static template = xml`
`;
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe("abc
");
});
test("parent is set within t-call with no parentNode", async () => {
const sub = xml``;
let child: any = null;
class Child extends Component {
setup() {
child = this;
}
static template = xml`lucas`;
}
class Parent extends Component {
static components = { Child };
static template = xml``;
}
const parent = await mount(Parent, fixture);
expect(fixture.innerHTML).toBe("lucas");
expect(isDirectChildOf(child, parent)).toBe(true);
});
test("handlers with arguments are properly bound through a t-call", async () => {
const sub = xml`lucas
`;
let value: any = null;
class Parent extends Component {
static template = xml`
`;
update(a: any) {
expect(this).toBe(parent);
value = a;
}
a = 3;
}
const parent = await mount(Parent, fixture);
fixture.querySelector("p")!.click();
expect(value).toBe(3);
});
test("dynamic t-call: key is propagated", async () => {
let childId = 0;
class Child extends Component {
static template = xml``;
id: any;
setup() {
this.id = childId++;
}
}
const sub = xml``;
class Parent extends Component {
static template = xml``;
static components = { Child };
sub = sub;
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(``);
});
test("recursive t-call binding this -- static t-call", async () => {
let clickCount = 0;
class Parent extends Component {
onClicked?: Function;
setup() {
const instance = this;
this.onClicked = function () {
clickCount++;
expect(this).toBe(instance);
};
}
static template = xml`
`;
}
const app = new App(Parent);
app.addTemplate(
"recursive",
`
`
);
await app.mount(fixture);
for (const div of fixture.querySelectorAll("div")) {
div.click();
}
expect(clickCount).toBe(2);
});
test("t-call with t-call-context, simple use", async () => {
class Root extends Component {
static template = xml`
`;
subctx = { aab: "aaron", lpe: "lucas" };
}
await mount(Root, fixture, {
templates: `
`,
});
expect(fixture.innerHTML).toBe("aaronlucas");
});
test("t-call with t-call-context and subcomponent", async () => {
class Child extends Component {
static template = xml`child`;
}
class Root extends Component {
static template = xml`
`;
static components = { Child };
subctx = { aab: "aaron", lpe: "lucas" };
}
await mount(Root, fixture, {
templates: `
`,
});
expect(fixture.innerHTML).toBe("childaaronchildlucas");
});
test("t-call with t-call-context and subcomponent, in dev mode", async () => {
class Child extends Component {
static template = xml`child`;
static props = ["name"];
}
class Root extends Component {
static template = xml`
`;
static components = { Child };
subctx = { aab: "aaron", lpe: "lucas" };
}
await mount(Root, fixture, {
test: true,
templates: `
`,
});
expect(fixture.innerHTML).toBe("childaaronchildlucas");
});
test("t-call-context: ComponentNode is not looked up in the context", async () => {
let child: any;
class Child extends Component {
static template = xml``;
static props = ["name"];
setup() {
child = this;
}
}
class Root extends Component {
static template = xml`
`;
static components = { Child };
}
// The following things need a reference to the ComponentNode, historically
// we used to do this with ctx.__owl__, but this cannot work inside t-call-context
// - t-ref: node.refs[refName] = something
// - t-set: caused a call to capture, which used to use node.component
// - .bind: used to bind to node.component
const root = await mount(Root, fixture, {
templates: `
outside slot
I'm the default slot
`,
});
expect(fixture.innerHTML).toBe(
"outside slot
I'm the default slot
3
"
);
expect(Object.keys(child.__owl__.refs)).toEqual([]);
expect(Object.keys(root.__owl__.refs)).toEqual(["myRef", "myRef2"]);
});
test("t-call-context: slots don't make component available again when context is captured", async () => {
class Child extends Component {
static template = xml``;
}
class Root extends Component {
static template = xml``;
static components = { Child };
someValue = "Hello";
}
await mount(Root, fixture, {
test: true,
templates: `
`,
});
expect(fixture.innerHTML).toBe("");
});
test("t-call-context: this is not available inside t-call-context", async () => {
class Root extends Component {
static template = xml``;
}
await mount(Root, fixture, {
templates: `
`,
});
expect(fixture.innerHTML).toBe("");
});
test("dynamic t-call with same sub component", async () => {
class Child extends Component {
static template = xml`child`;
}
class Root extends Component {
static template = xml`
`;
static components = { Child };
current = useState({ template: "A" });
}
const root = await mount(Root, fixture, {
templates: `
`,
});
expect(fixture.innerHTML).toBe("Achild");
root.current.template = "B";
await nextTick();
expect(fixture.innerHTML).toBe("Bchild");
});
});