import { Component, mount, useState, xml } from "../../src";
import { makeTestFixture, nextTick, snapshotEverything } from "../helpers";
snapshotEverything();
let fixture: HTMLElement;
beforeEach(() => {
fixture = makeTestFixture();
});
describe("style and class handling", () => {
test("can set style and class inside component", async () => {
class Test extends Component {
static template = xml`
world
`;
}
await mount(Test, fixture);
expect(fixture.innerHTML).toBe(`world
`);
});
test("can set class on sub component, as prop", async () => {
class Child extends Component {
static template = xml`child
`;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`child
`);
});
test("no class is set is parent does not give it as prop", async () => {
class Child extends Component {
static template = xml`child
`;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`child
`);
});
test("no class is set is child ignores it", async () => {
class Child extends Component {
static template = xml`child
`;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`child
`);
});
test("empty class attribute is not added on widget root el", async () => {
class Child extends Component {
static template = xml``;
}
class Parent extends Component {
static template = xml`
`;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`
`);
});
test("can set more than one class on sub component", async () => {
class Child extends Component {
static template = xml`child
`;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`child
`);
});
test("component class and parent class combine together", async () => {
class Child extends Component {
static template = xml`child
`;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`child
`);
});
test("can set class on sub sub component", async () => {
class ChildChild extends Component {
static template = xml`childchild
`;
}
class Child extends Component {
static template = xml``;
static components = { ChildChild };
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`childchild
`);
});
test("can set class on multi root component", async () => {
class Child extends Component {
static template = xml`a
b`;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`a
b`);
});
test("class on sub component, which is switched to another", async () => {
class ChildA extends Component {
static template = xml`a
`;
}
class ChildB extends Component {
static template = xml`b`;
}
class Child extends Component {
static template = xml``;
static components = { ChildA, ChildB };
}
class Parent extends Component {
static template = xml``;
static components = { Child };
state = useState({ child: "a" });
}
const parent = await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(`a
`);
parent.state.child = "b";
await nextTick();
expect(fixture.innerHTML).toBe(`b`);
});
// test("t-att-class is properly added/removed on widget root el", async () => {
// class Child extends Component {
// static template = xml``;
// }
// class Parent extends Component {
// static template = xml`
`;
// static components = { Child };
// state = useState({ a: true, b: false });
// }
// const widget = await mount(Parent, fixture);
// expect(fixture.innerHTML).toBe(``);
// widget.state.a = false;
// widget.state.b = true;
// await nextTick();
// expect(fixture.innerHTML).toBe(``);
// });
test("class with extra whitespaces", async () => {
class Child extends Component {
static template = xml``;
}
class Parent extends Component {
static template = xml``;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(``);
});
test("class with extra whitespaces (variation)", async () => {
class Child extends Component {
static template = xml``;
}
class Parent extends Component {
static template = xml`
`;
static components = { Child };
}
await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(``);
});
test("t-att-class is properly added/removed on widget root el (v2)", async () => {
let child: any = null;
class Child extends Component {
static template = xml``;
state = useState({ d: true });
setup() {
child = this;
}
}
class Parent extends Component {
static template = xml`
`;
static components = { Child };
state = useState({ b: true });
}
const widget = await mount(Parent, fixture);
const span = fixture.querySelector("span")!;
expect(span.className).toBe("c d b");
widget.state.b = false;
await nextTick();
expect(span.className).toBe("c d");
child.state.d = false;
await nextTick();
expect(span.className).toBe("c");
widget.state.b = true;
await nextTick();
expect(span.className).toBe("c b");
child.state.d = true;
await nextTick();
expect(span.className).toBe("c b d");
});
});