import { App, Component, mount, useState, xml } from "../../src/index"; import { addTemplate, isDirectChildOf, makeTestFixture, nextTick, snapshotEverything, } from "../helpers"; snapshotEverything(); let fixture: HTMLElement; beforeEach(() => { fixture = makeTestFixture(); }); describe("t-call", () => { test("dynamic t-call", async () => { class App extends Component { static template = xml` owl `; current = useState({ template: "foo" }); } addTemplate("foo", "
foo
"); addTemplate("bar", "bar"); const app = await mount(App, fixture); expect(fixture.innerHTML).toBe("
foo
"); app.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("

lucas

0
"); fixture.querySelector("p")!.click(); await nextTick(); expect(fixture.innerHTML).toBe("

lucas

1
"); }); 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("

lucas

0
"); fixture.querySelector("p")!.click(); await nextTick(); expect(fixture.innerHTML).toBe("

lucas

1
"); }); 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); }); });