import { render, CustomElement, Host, defineElement, state, prop } from "@cerxes/csx";
import { testContainer } from "../utils/test-container";
import { nextAnimationFrame } from "../utils/next-animation-frame";
describe("Basic tests", () => {
/**
* Assert that a basic component renders as expected
*/
test("Simple example-component", async () => {
@defineElement('basic-component')
class BasicComponent extends CustomElement{
render(){
return (
This page is an example!
These are the contents.
)
}
}
let container = testContainer(
render()
);
document.body.appendChild(container);// Components need to be added to the DOM or their connectecCallback will not be called
expect(
container.innerHTML
).toBe([
``,
`This page is an example!
These are the contents.
`,
``,
].join(''));
document.body.removeChild(container);
});
/**
* Assert that a component rerenders when one of it's state variables changes
*/
test("Component state", async () => {
@defineElement('state-test')
class StateComponent extends CustomElement{
@state()
loading = false;
render(){
return (
{this.loading? "loading" : "not-loading"}
)
}
}
let component = render();
let container = testContainer(component);
document.body.appendChild(container);// Components need to be added to the DOM or their connectecCallback will not be called
// Initial
expect(container.innerHTML).toBe(`not-loading`);
// Change state and wait for the next animation frame to passs
component.loading = true;
await nextAnimationFrame();
// Updated by state
expect(container.innerHTML).toBe(`loading`);
document.body.removeChild(container);
});
/**
* Assert that a component rerenders when one of it's properties changes, and in doing so, that it's properties can be set
* through the CSX render function
*/
test("Component properties", async () => {
@defineElement('prop-test')
class PropComponent extends CustomElement{
@prop()
title = "Default title";
render(){
return (
{this.title}
)
}
}
let initialVSpec = ;
let component = render(initialVSpec);
let container = testContainer(component);
document.body.appendChild(container);// Components need to be added to the DOM or their connectecCallback will not be called
// Initial
expect(container.innerHTML).toBe(`Default title
`);
// Change the prop through CSX and verify that it has been set
let updatedVSpec = ;
render(updatedVSpec, {host: component, old: initialVSpec});
expect(component.title).toBe(`Updated title`);
// Wait for the next animation frame for the changes to be rendered
await nextAnimationFrame();
// Updated by state
expect(container.innerHTML).toBe(`Updated title
`);
document.body.removeChild(container);
});
});