// import { mountBlock } from "../../src/blockdom/block";
import { mount } from "../../src/runtime/blockdom";
import { makeTestFixture, renderToBdom, renderToString, snapshotEverything } from "../helpers";
snapshotEverything();
// -----------------------------------------------------------------------------
// t-if
// -----------------------------------------------------------------------------
describe("t-if", () => {
test("t-if in a div", () => {
const template = `
ok
`;
expect(renderToString(template, { condition: true })).toBe("ok
");
expect(renderToString(template, { condition: false })).toBe("");
expect(renderToString(template, {})).toBe("");
});
test("t-if with empty content", () => {
const template = `hello`;
expect(renderToString(template, { condition: true })).toBe("hello");
expect(renderToString(template, { condition: false })).toBe("hello");
});
test("boolean value condition missing", () => {
const template = `fail`;
expect(renderToString(template)).toBe("");
});
test("just a t-if", () => {
const template = `ok`;
expect(renderToString(template, { condition: true })).toBe("ok");
expect(renderToString(template, { condition: false })).toBe("");
});
test("a t-if with two inner nodes", () => {
const template = `yipyip
`;
expect(renderToString(template, { condition: true })).toBe("yipyip
");
expect(renderToString(template, { condition: false })).toBe("");
});
test("div containing a t-if with two inner nodes", () => {
const template = ``;
expect(renderToString(template, { condition: true })).toBe(
""
);
expect(renderToString(template, { condition: false })).toBe("");
});
test("two consecutive t-if", () => {
const template = `12`;
expect(renderToString(template, { cond1: true, cond2: true })).toBe("12");
expect(renderToString(template, { cond1: false, cond2: true })).toBe("2");
});
test("a t-if next to a div", () => {
const template = `foo
1`;
expect(renderToString(template, { cond: true })).toBe("foo
1");
expect(renderToString(template, { cond: false })).toBe("foo
");
});
test("two consecutive t-if in a div", () => {
const template = `12
`;
expect(renderToString(template, { cond1: true, cond2: true })).toBe("12
");
expect(renderToString(template, { cond1: false, cond2: true })).toBe("2
");
});
test("simple t-if/t-else", () => {
const template = `12`;
expect(renderToString(template, { condition: true })).toBe("1");
expect(renderToString(template, { condition: false })).toBe("2");
});
test("simple t-if/t-else in a div", () => {
const template = `12
`;
expect(renderToString(template, { condition: true })).toBe("1
");
expect(renderToString(template, { condition: false })).toBe("2
");
});
test("t-if/t-else with more content", () => {
const template = `asfcoucou`;
expect(renderToString(template, { condition: true })).toBe("asf");
expect(renderToString(template, { condition: false })).toBe("coucou");
});
test("boolean value condition elif", () => {
const template = `
black pearl
yellow submarine
red is dead
beer
`;
expect(renderToString(template, { color: "red" })).toBe("red is dead
");
});
test("boolean value condition elif (no outside node)", () => {
const template = `
black pearl
yellow submarine
red is dead
beer`;
expect(renderToString(template, { color: "red" })).toBe("red is dead");
});
test("boolean value condition else", () => {
const template = `
begin
ok
ok-else
end
`;
expect(renderToString(template, { condition: true })).toBe(
"beginokend
"
);
});
test("boolean value condition false else", () => {
const template = `
beginfail
fail-elseend
`;
expect(renderToString(template, { condition: false })).toBe(
"beginfail-elseend
"
);
});
test("can use some boolean operators in expressions", () => {
const template = `
and
nope
or
nope
mgt
ngt
mlt
nlt
`;
const context = {
cond1: true,
cond2: true,
cond3: false,
cond4: false,
m: 5,
n: 2,
};
expect(renderToString(template, context)).toBe("andormgtnlt
");
});
test("t-esc with t-if", () => {
const template = `
`;
expect(renderToString(template)).toBe("x
");
});
test("t-esc with t-elif", () => {
const template = `abc
`;
expect(renderToString(template)).toBe("x
");
});
test("t-set, then t-if", () => {
const template = `
`;
expect(renderToString(template)).toBe("test
");
});
test("t-set, then t-if, part 2", () => {
const template = `
COUCOU
`;
expect(renderToString(template)).toBe("COUCOU
");
});
test("t-set, then t-if, part 3", () => {
const template = `
AAA
BBB
`;
expect(renderToString(template)).toBe("BBB
");
});
test("t-if in a t-if", () => {
const template = `12
`;
expect(renderToString(template, { cond1: true, cond2: true })).toBe(
"12
"
);
expect(renderToString(template, { cond1: true, cond2: false })).toBe(
"1
"
);
expect(renderToString(template, { cond1: false, cond2: true })).toBe("");
expect(renderToString(template, { cond1: false, cond2: false })).toBe("");
});
test("t-if and t-else with two nodes", () => {
const template = `1ab`;
expect(renderToString(template, { condition: true })).toBe("1");
expect(renderToString(template, { condition: false })).toBe("ab");
});
test("dynamic content after t-if with two children nodes", () => {
const template = ``;
// need to do it with bdom to go through the update path
const bdom = renderToBdom(template, { condition: true, text: "owl" });
const fixture = makeTestFixture();
mount(bdom, fixture);
expect(fixture.innerHTML).toBe("");
const bdom2 = renderToBdom(template, { condition: false, text: "halloween" });
bdom.patch(bdom2, true);
expect(fixture.innerHTML).toBe("halloween
");
});
test("two t-ifs next to each other", () => {
const template = ``;
// need to do it with bdom to go through the update path
const bdom = renderToBdom(template, { condition: true, text: "owl" });
const fixture = makeTestFixture();
mount(bdom, fixture);
expect(fixture.innerHTML).toBe("");
const bdom2 = renderToBdom(template, { condition: false, text: "halloween" });
bdom.patch(bdom2, true);
expect(fixture.innerHTML).toBe("");
});
});