prototype for hydration

This commit is contained in:
Géry Debongnie
2022-02-16 09:19:07 +01:00
parent 73c339fff1
commit 79a8139697
8 changed files with 289 additions and 2 deletions
+78
View File
@@ -0,0 +1,78 @@
import { hydrate, patch, text, createBlock } from "../../src/blockdom";
import { makeTestFixture } from "./helpers";
//------------------------------------------------------------------------------
// Setup and helpers
//------------------------------------------------------------------------------
let fixture: HTMLElement;
beforeEach(() => {
fixture = makeTestFixture();
});
afterEach(() => {
fixture.remove();
});
describe("hydration", () => {
test("simple text node", async () => {
fixture.innerHTML = "some text";
const target = fixture.firstChild as any;
const tree = text("some text");
expect(tree.el).toBe(undefined);
hydrate(tree, target);
expect(fixture.innerHTML).toBe("some text");
expect(tree.el).toBe(target);
patch(tree, text("checkmate"));
expect(fixture.innerHTML).toBe("checkmate");
});
test("simple static block", async () => {
fixture.innerHTML = "<div>some text</div>";
const target = fixture.firstChild as any;
const block = createBlock("<div>some text</div>");
const tree = block();
expect(tree.el).toBe(undefined);
hydrate(tree, target);
expect(fixture.innerHTML).toBe("<div>some text</div>");
expect(tree.el).toBe(target);
});
test("simple dynamic block", async () => {
fixture.innerHTML = "<div>some text</div>";
const target = fixture.firstChild as any;
const block = createBlock("<div><block-text-0/></div>");
const tree = block(["some text"]);
expect(tree.el).toBe(undefined);
hydrate(tree, target);
expect(fixture.innerHTML).toBe("<div>some text</div>");
expect(tree.el).toBe(target);
patch(tree, block(["giuoco piano"]));
expect(fixture.innerHTML).toBe("<div>giuoco piano</div>");
});
test("block with sub block", async () => {
fixture.innerHTML = "<div>queen<p>gambit</p></div>";
const target = fixture.firstChild as any;
const block1 = createBlock("<div><block-text-0/><block-child-0/></div>");
const block2 = createBlock("<p><block-text-0/></p>");
const tree = block1(["queen"], [block2(["gambit"])]);
expect(tree.el).toBe(undefined);
hydrate(tree, target);
expect(fixture.innerHTML).toBe("<div>queen<p>gambit</p></div>");
expect(tree.el).toBe(target);
patch(tree, block1(["king"], [block2(["pawn"])]));
expect(fixture.innerHTML).toBe("<div>king<p>pawn</p></div>");
});
});
@@ -0,0 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`hydration can hydrate a component with a handler 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<div block-handler-0=\\"click\\"><block-text-1/></div>\`);
return function template(ctx, node, key = \\"\\") {
let hdlr1 = [ctx['inc'], ctx];
let txt1 = ctx['state'].value;
return block1([hdlr1, txt1]);
}
}"
`;
exports[`hydration can hydrate a component with a sub component 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<p><block-child-0/></p>\`);
return function template(ctx, node, key = \\"\\") {
let b2 = component(\`Counter\`, {}, key + \`__1\`, node, ctx);
return block1([], [b2]);
}
}"
`;
exports[`hydration can hydrate a component with a sub component 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<button block-handler-0=\\"click\\"><block-text-1/></button>\`);
return function template(ctx, node, key = \\"\\") {
let hdlr1 = [ctx['inc'], ctx];
let txt1 = ctx['state'].value;
return block1([hdlr1, txt1]);
}
}"
`;
exports[`hydration can hydrate a simple static component 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<div>giuoco pianissimo</div>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
+79
View File
@@ -0,0 +1,79 @@
import { Component, mount, useState, xml } from "../../src";
import { makeTestFixture, nextTick, snapshotEverything } from "../helpers";
let fixture: HTMLElement;
snapshotEverything();
beforeEach(() => {
fixture = makeTestFixture();
});
describe("hydration", () => {
test("can hydrate a simple static component", async () => {
fixture.innerHTML = "<div>giuoco pianissimo</div>";
const target = fixture.firstChild as any;
class Test extends Component {
static template = xml`<div>giuoco pianissimo</div>`;
}
await mount(Test, target, { hydrate: true });
expect(fixture.innerHTML).toBe("<div>giuoco pianissimo</div>");
expect(fixture.firstChild).toBe(target);
});
test("can hydrate a component with a handler", async () => {
fixture.innerHTML = "<div>0</div>";
const target = fixture.firstChild as any;
class Counter extends Component {
static template = xml`<div t-on-click="inc"><t t-esc="state.value"/></div>`;
state = useState({ value: 0 });
inc() {
this.state.value++;
}
}
await mount(Counter, target, { hydrate: true });
expect(fixture.innerHTML).toBe("<div>0</div>");
expect(fixture.firstChild).toBe(target);
target.click();
await nextTick();
expect(fixture.innerHTML).toBe("<div>1</div>");
});
test("can hydrate a component with a sub component", async () => {
fixture.innerHTML = "<p><button>0</button></p>";
const target = fixture.firstChild as any;
class Counter extends Component {
static template = xml`<button t-on-click="inc"><t t-esc="state.value"/></button>`;
state = useState({ value: 0 });
inc() {
this.state.value++;
}
}
class Parent extends Component {
static template = xml`<p><Counter/></p>`;
static components = { Counter };
}
await mount(Parent, target, { hydrate: true });
expect(fixture.innerHTML).toBe("<p><button>0</button></p>");
expect(fixture.firstChild).toBe(target);
target.querySelector("button")!.click();
await nextTick();
expect(fixture.innerHTML).toBe("<p><button>1</button></p>");
});
});