import { Component, mount, xml } from "../../src";
import { makeTestFixture, nextTick, snapshotEverything } from "../helpers";
snapshotEverything();
let fixture: HTMLElement;
beforeEach(() => {
fixture = makeTestFixture();
});
// -----------------------------------------------------------------------------
// t-set
// -----------------------------------------------------------------------------
describe("t-set", () => {
test("t-set outside modified in t-if", async () => {
class Comp extends Component {
static template = xml`
`;
state = { flag: "if" };
}
const comp = await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
comp.state.flag = "elif";
comp.render();
await nextTick();
expect(fixture.innerHTML).toBe("");
comp.state.flag = "false";
comp.render();
await nextTick();
expect(fixture.innerHTML).toBe("");
});
test("t-set in t-if", async () => {
// Weird that code block within 'if' leaks outside of it
// Python does the same
class Comp extends Component {
static template = xml`
`;
state = { flag: "if" };
}
const comp = await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
comp.state.flag = "elif";
comp.render();
await nextTick();
expect(fixture.innerHTML).toBe("");
comp.state.flag = "false";
comp.render();
await nextTick();
expect(fixture.innerHTML).toBe("");
});
test("t-set can't alter component even if key in component", async () => {
class Comp extends Component {
static template = xml`
`;
iter = 1;
}
const comp = await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
expect(comp.iter).toBe(1);
});
test("t-set can't alter component if key not in component", async () => {
class Comp extends Component {
static template = xml`
`;
}
const comp = await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
expect((comp as any).iter).toBeUndefined();
});
test("slot setted value (with t-set) not accessible with t-esc", async () => {
class Childcomp extends Component {
static template = xml`
`;
}
class Comp extends Component {
static components = { Childcomp };
static template = xml`
`;
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
});
test("t-set not altered by child comp", async () => {
let child;
class Childcomp extends Component {
static template = xml`
`;
iter = "child";
setup() {
super.setup();
child = this;
}
}
class Comp extends Component {
static components = { Childcomp };
static template = xml`
`;
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
expect((child as any).iter).toBe("child");
});
test("t-set with something in body", async () => {
class Comp extends Component {
static template = xml`
`;
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
});
test("t-set with a component in body", async () => {
class Child extends Component {
static template = xml`Child`;
}
class Comp extends Component {
static template = xml`
`;
static components = { Child };
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("");
});
test("slots with an unused t-set with a component in body", async () => {
class Child extends Component {
static template = xml`Child `;
}
class Comp extends Component {
static template = xml`
in slot
`;
static components = { Child };
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("Child in slot ");
});
test("slots with a t-set with a component in body", async () => {
class C extends Component {
static template = xml`C`;
}
class Child extends Component {
static template = xml`Child `;
}
class Comp extends Component {
static template = xml`
in slot
`;
static components = { Child, C };
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("Child in slot C");
});
test("slots with an t-set with a component in body", async () => {
class Child extends Component {
static template = xml`Child`;
}
class Blorg extends Component {
static template = xml`Blorg `;
}
class Comp extends Component {
static template = xml`
coffee
tea
`;
static components = { Child, Blorg };
}
await mount(Comp, fixture);
expect(fixture.innerHTML).toBe("Blorg tea Childcoffee
");
});
});