import { Component, mount, onWillUpdateProps, useState, xml } from "../../src"; import { makeTestFixture, nextTick, snapshotEverything, steps, useLogLifecycle } from "../helpers"; let fixture: HTMLElement; snapshotEverything(); beforeEach(() => { fixture = makeTestFixture(); }); describe("basics", () => { test("explicit object prop", async () => { class Child extends Component { static template = xml``; state: any; setup() { this.state = useState({ someval: this.props.value }); } } class Parent extends Component { static template = xml`
`; static components = { Child }; state = useState({ val: 42 }); } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("
42
"); }); test("prop names can contain -", async () => { class Child extends Component { static template = xml`
`; } class Parent extends Component { static template = xml``; static components = { Child }; } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("
7
"); }); test("accept ES6-like syntax for props (with getters)", async () => { class Child extends Component { static template = xml``; } class Parent extends Component { static template = xml`
`; static components = { Child }; get greetings() { const name = "aaron"; return `hello ${name}`; } } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("
hello aaron
"); }); test("t-set works ", async () => { class Child extends Component { static template = xml``; } class Parent extends Component { static template = xml`
`; static components = { Child }; } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("
42
"); }); test("t-set with a body expression can be used as textual prop", async () => { class Child extends Component { static template = xml``; } class Parent extends Component { static components = { Child }; static template = xml`
42
`; } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("
42
"); }); test("t-set with a body expression can be passed in props, and then t-out", async () => { class Child extends Component { static template = xml` `; } class Parent extends Component { static components = { Child }; static template = xml`

43

`; } await mount(Parent, fixture); expect(fixture.innerHTML).toBe("
<p>43</p>

43

"); }); test("arrow functions as prop correctly capture their scope", async () => { class Child extends Component { static template = xml``; setup() { useLogLifecycle(); } } class Parent extends Component { static template = xml` `; static components = { Todo }; state = useState({ elems: [ { id: 1, isChecked: false }, { id: 2, isChecked: true }, ], }); setup() { useLogLifecycle(); } toggle(id: number) { const todo = this.state.elems.find((el) => el.id === id)!; todo.isChecked = !todo.isChecked; } } await mount(Parent, fixture); expect(steps.splice(0)).toMatchInlineSnapshot(` Array [ "Parent:setup", "Parent:willStart", "Parent:willRender", "Todo:setup", "Todo:willStart", "Todo:setup", "Todo:willStart", "Parent:rendered", "Todo:willRender", "Todo:rendered", "Todo:willRender", "Todo:rendered", "Todo:mounted", "Todo:mounted", "Parent:mounted", ] `); expect(fixture.innerHTML).toBe(""); fixture.querySelector("button")?.click(); await nextTick(); expect(fixture.innerHTML).toBe(""); expect(steps.splice(0)).toMatchInlineSnapshot(` Array [ "Todo:willRender", "Todo:rendered", "Todo:willPatch", "Todo:patched", ] `); });