import { mount, patch, createBlock } from "../../src/runtime/blockdom"; import { makeTestFixture } from "./helpers"; //------------------------------------------------------------------------------ // Setup and helpers //------------------------------------------------------------------------------ let fixture: HTMLElement; beforeEach(() => { fixture = makeTestFixture(); }); afterEach(() => { fixture.remove(); }); test("simple attribute", async () => { const block = createBlock('
'); const tree = block(["world"]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block(["owl"])); expect(fixture.innerHTML).toBe(``); }); test("updating attribute with falsy value", async () => { const block = createBlock(''); const tree = block([false]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block(["owl"])); expect(fixture.innerHTML).toBe(``); patch(tree, block([false])); expect(fixture.innerHTML).toBe(``); patch(tree, block(["owl"])); expect(fixture.innerHTML).toBe(``); patch(tree, block([undefined])); expect(fixture.innerHTML).toBe(``); }); test("dynamic attribute (pair)", async () => { const block = createBlock(''); const tree = block([["hello", "world"]]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block([["ola", "mundo"]])); expect(fixture.innerHTML).toBe(``); }); test("dynamic attribute (pair, with false value)", async () => { const block = createBlock(''); const tree = block([["hello", false]]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block([["hello", "world"]])); expect(fixture.innerHTML).toBe(``); patch(tree, block([["hello", false]])); expect(fixture.innerHTML).toBe(``); }); test("dynamic attribute (object)", async () => { const block = createBlock(''); const tree = block([{ hello: "world" }]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block([{ ola: "mundo" }])); expect(fixture.innerHTML).toBe(``); }); test("dynamic attribute (object), with falsy values", async () => { const block = createBlock(''); const tree = block([{ hello: "world", blip: false }]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block([{ ola: "mundo", blip: undefined }])); expect(fixture.innerHTML).toBe(``); patch(tree, block([{ ola: false, blip: 1 }])); expect(fixture.innerHTML).toBe(``); patch(tree, block([{ ola: undefined, blip: undefined }])); expect(fixture.innerHTML).toBe(``); }); test("class attribute", async () => { const block = createBlock(''); const tree = block(["fire"]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block(["water"])); expect(fixture.innerHTML).toBe(``); patch(tree, block([""])); expect(fixture.innerHTML).toBe(``); patch(tree, block([0])); expect(fixture.innerHTML).toBe(``); }); test("attribute with undefined value", async () => { const block = createBlock(''); const tree = block([undefined]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); }); test("class attribute with undefined value", async () => { const block = createBlock(''); const tree = block([undefined]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); }); test("class attribute (with a preexisting value", async () => { const block = createBlock(''); const tree = block(["potato"]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); patch(tree, block(["squash"])); expect(fixture.innerHTML).toBe(``); patch(tree, block([""])); expect(fixture.innerHTML).toBe(``); }); describe("properties", () => { test("input with value attribute", () => { // render input with initial value const block = createBlock(``); const tree = block(["zucchini"]); mount(tree, fixture); // const bnode1 = renderToBdom(template, { v: "zucchini" }); // const fixture = makeTestFixture(); // mount(bnode1, fixture); const input = fixture.querySelector("input")!; expect(input.value).toBe("zucchini"); // change value manually in input, to simulate user input input.value = "tomato"; expect(input.value).toBe("tomato"); // rerender with a different value, and patch actual dom, to check that // input value was properly reset by owl patch(tree, block(["potato"])); expect(input.value).toBe("potato"); }); test("input with value attribute, and falsy value given", () => { const block = createBlock(``); const tree = block([undefined]); mount(tree, fixture); const input = fixture.querySelector("input")!; expect(input.value).toBe(""); patch(tree, block([null])); expect(input.value).toBe(""); patch(tree, block([0])); expect(input.value).toBe("0"); patch(tree, block([""])); expect(input.value).toBe(""); patch(tree, block([false])); expect(input.value).toBe(""); }); test("input type=checkbox with checked attribute", () => { // render input with initial value const block = createBlock(``); const tree = block([true]); mount(tree, fixture); expect(fixture.innerHTML).toBe(``); const input = fixture.querySelector("input")!; expect(input.checked).toBe(true); }); });