import { App, Component, mount, useState } from "../../src"; import { xml } from "../../src/tags"; import { makeTestFixture, nextTick, snapshotEverything } from "../helpers"; let fixture: HTMLElement; snapshotEverything(); beforeEach(() => { fixture = makeTestFixture(); }); describe("basics", () => { test("basic use", async () => { class Child extends Component { static template = xml`child`; } class Parent extends Component { static template = xml``; static components = { Child }; } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("child1"); }); test("sub widget is interactive", async () => { class Child extends Component { static template = xml` child `; state = useState({ val: 1 }); inc() { this.state.val++; } } class Parent extends Component { static template = xml``; static components = { Child }; } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("child1"); const button = fixture.querySelector("button")!; button.click(); await nextTick(); expect(fixture.innerHTML).toBe("child2"); }); test("can select a sub widget ", async () => { class Child extends Component { static template = xml`CHILD 1`; } class OtherChild extends Component { static template = xml`
CHILD 2
`; } class Parent extends Component { static template = xml` `; static components = { Child, OtherChild }; } const env = { flag: true }; const app = new App(Parent); app.configure({ env }); const parent = await app.mount(fixture); expect(fixture.innerHTML).toBe("CHILD 1"); env.flag = false; await parent.render(); expect(fixture.innerHTML).toBe("
CHILD 2
"); }); test("can select a sub widget, part 2", async () => { class Child extends Component { static template = xml`CHILD 1`; } class OtherChild extends Component { static template = xml`
CHILD 2
`; } class Parent extends Component { static template = xml` `; state = useState({ flag: true }); static components = { Child, OtherChild }; } let parent = await mount(Parent, fixture); expect(fixture.innerHTML).toBe("CHILD 1"); parent.state.flag = false; await nextTick(); expect(fixture.innerHTML).toBe("
CHILD 2
"); }); test("top level sub widget with a parent", async () => { class ComponentC extends Component { static template = xml`Hello`; } class ComponentB extends Component { static template = xml``; static components = { ComponentC }; } class ComponentA extends Component { static template = xml`
`; static components = { ComponentB }; } await mount(ComponentA, fixture); expect(fixture.innerHTML).toBe("
Hello
"); }); });