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(`
`); }); test("block-class attributes with preexisting class attribute", async () => { const block = createBlock('
'); const tree = block([{ class: "eagle" }]); mount(tree, fixture); expect(fixture.innerHTML).toBe(`
`); patch(tree, block([{ class: "falcon" }])); expect(fixture.innerHTML).toBe(`
`); patch(tree, block([{}])); expect(fixture.innerHTML).toBe(`
`); }); test("block-class attributes (array syntax) with preexisting class attribute", async () => { const block = createBlock('
'); const tree = block([["class", "eagle"]]); mount(tree, fixture); expect(fixture.innerHTML).toBe(`
`); patch(tree, block([["class", "falcon"]])); expect(fixture.innerHTML).toBe(`
`); patch(tree, block([["class", ""]])); expect(fixture.innerHTML).toBe(`
`); patch(tree, block([["class", "buzzard"]])); expect(fixture.innerHTML).toBe(`
`); });