Make if and each blocks reactive on the client
This commit is contained in:
@@ -117,6 +117,55 @@ test("@event (data-on-click) mutates a signal and re-renders", () => {
|
||||
expect(btn.textContent).toBe("2");
|
||||
});
|
||||
|
||||
test("compiled if blocks switch branches after hydration", () => {
|
||||
const definition = Buffer.from(
|
||||
JSON.stringify([
|
||||
{ cond: "open", body: '<p class="open">Open <span data-text="count">{count}</span></p>' },
|
||||
{ cond: null, body: '<p class="closed">Closed</p>' },
|
||||
]),
|
||||
).toString("base64");
|
||||
const win = mount(
|
||||
`<div data-scope="open: false, count: 2">` +
|
||||
`<button data-on-click="open = !open">toggle</button>` +
|
||||
`<button data-on-click="count++">increment</button>` +
|
||||
`<template data-wrn-if="${definition}"></template><p class="closed">Closed</p><template data-wrn-control-end></template>` +
|
||||
`</div>`,
|
||||
);
|
||||
|
||||
win.document.querySelector("button")!.click();
|
||||
expect(win.document.querySelector(".closed")).toBeNull();
|
||||
expect(win.document.querySelector(".open")?.textContent).toBe("Open 2");
|
||||
win.document.querySelectorAll("button")[1]!.click();
|
||||
expect(win.document.querySelector(".open")?.textContent).toBe("Open 3");
|
||||
});
|
||||
|
||||
test("compiled each blocks rerender rows and their empty branch", () => {
|
||||
const definition = Buffer.from(
|
||||
JSON.stringify({
|
||||
list: "items",
|
||||
item: "item",
|
||||
index: "index",
|
||||
body: '<p class="row">{index}:{item}</p>',
|
||||
empty: '<p class="empty">Empty</p>',
|
||||
}),
|
||||
).toString("base64");
|
||||
const win = mount(
|
||||
`<div data-scope="items: ['a']">` +
|
||||
`<button data-on-click="items = ['b', 'c']">more</button>` +
|
||||
`<button data-on-click="items = []">clear</button>` +
|
||||
`<template data-wrn-each="${definition}"></template><p class="row">0:a</p><template data-wrn-control-end></template>` +
|
||||
`</div>`,
|
||||
);
|
||||
|
||||
win.document.querySelectorAll("button")[0]!.click();
|
||||
expect(Array.from(win.document.querySelectorAll(".row")).map((node) => node.textContent)).toEqual(
|
||||
["0:b", "1:c"],
|
||||
);
|
||||
win.document.querySelectorAll("button")[1]!.click();
|
||||
expect(win.document.querySelector(".row")).toBeNull();
|
||||
expect(win.document.querySelector(".empty")?.textContent).toBe("Empty");
|
||||
});
|
||||
|
||||
test("component functions support formatted multiline assignments and ternaries", () => {
|
||||
const behavior = Buffer.from(
|
||||
JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user