Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { h } from "@stencil/core";
import { newSpecPage } from "@stencil/core/testing";
import { h, VNode } from "@stencil/core";
import { constrainHeadingLevel, Heading } from "./Heading";

describe("constrainHeadingLevel", () => {
Expand All @@ -13,26 +12,55 @@ describe("constrainHeadingLevel", () => {
});
});

describe("Heading", () => {
it("should render", async () => {
const page = await newSpecPage({
components: [],
template: () => (
<Heading class="test" level={1}>
My Heading
</Heading>
/**
* simple VNode assertion util to help get rid of newSpecPage usage
*
* @param vnode
* @param expected
* @param expected.tag
* @param expected.attrs
* @param expected.children
*/
function assertVNode(
vnode: VNode,
expected: {
tag: string;
attrs: Record<string, any>;
children: (VNode | string)[];
},
) {
expect(vnode).toEqual(
expect.objectContaining({
Copy link
Copy Markdown
Member

@maxpatiiuk maxpatiiuk Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: I didn't know this was a thing!

supported in vitest too

$tag$: expected.tag,
$attrs$: expect.objectContaining(expected.attrs),
$children$: expected.children.map((child) =>
typeof child === "string"
? expect.objectContaining({ $text$: child })
: expect.objectContaining(child),
),
});
}),
);
}

expect(page.root).toEqualHtml(`<h1 class="test">My Heading</h1>`);
describe("Heading", () => {
it("should render", async () => {
assertVNode(
<Heading class="test" level={1}>
My Heading
</Heading>,
{
tag: "h1",
attrs: { class: "test" },
children: ["My Heading"],
},
);
});

it("should render a div", async () => {
const page = await newSpecPage({
components: [],
template: () => <Heading class="test">My Heading</Heading>,
assertVNode(<Heading class="test">My Heading</Heading>, {
tag: "div",
attrs: { class: "test" },
children: ["My Heading"],
});

expect(page.root).toEqualHtml(`<div class="test">My Heading</div>`);
});
});