Skip to content

Commit d12b1d7

Browse files
committed
Update tests to increase coverage
1 parent 85ce056 commit d12b1d7

4 files changed

Lines changed: 121 additions & 73 deletions

File tree

src/van-element.test.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

tests/internals.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import van from "vanjs-core";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { define } from "../src/van-element";
4+
5+
const { button, div } = van.tags;
6+
7+
const mountFn = vi.fn();
8+
const unmountFn = vi.fn();
9+
const clickFn = vi.fn();
10+
11+
define("internals-test", ({ attr, $this, children, mount }) => {
12+
const attribute = attr("attribute", "default");
13+
14+
const count = van.state(0);
15+
16+
mount(() => {
17+
mountFn();
18+
return () => unmountFn();
19+
});
20+
21+
const onClick = () => {
22+
clickFn();
23+
count.val++;
24+
$this.dispatchEvent(new CustomEvent("count", { detail: count.val }));
25+
};
26+
return [
27+
div("Attribute: ", attribute),
28+
button({ onclick: () => onClick() }, "Count: ", count),
29+
children,
30+
];
31+
});
32+
33+
describe("check that a Van Element", async () => {
34+
function getComponent() {
35+
return document.body.querySelector("internals-test");
36+
}
37+
38+
function queryInShadow<K extends keyof HTMLElementTagNameMap>(
39+
selector: K
40+
): HTMLElementTagNameMap[K] | null | undefined {
41+
return getComponent()?.shadowRoot?.querySelector(selector);
42+
}
43+
44+
function mountComponent(name = "Peter", children = "") {
45+
document.body.innerHTML = `<internals-test attribute="${name}">${children}</internals-test>`;
46+
}
47+
48+
beforeEach(() => {
49+
// Because VanJS uses setTimeout() to queue procedures in the event loop, we need to fake that otherwise the updates don't render.
50+
vi.useFakeTimers();
51+
vi.clearAllMocks();
52+
});
53+
54+
it("has VanJS behavior", async () => {
55+
mountComponent();
56+
queryInShadow("button")?.click();
57+
expect(clickFn).toHaveBeenCalled();
58+
vi.runAllTimers();
59+
expect(queryInShadow("button")?.textContent).toContain("1");
60+
});
61+
62+
it("has attribute reactivity", () => {
63+
mountComponent("Jack");
64+
expect(queryInShadow("div")?.textContent).toContain("Jack");
65+
getComponent()!.setAttribute("attribute", "John");
66+
vi.runAllTimers();
67+
expect(queryInShadow("div")?.textContent).toContain("John");
68+
});
69+
70+
it("mounts and unmounts properly", () => {
71+
expect(mountFn).not.toHaveBeenCalled();
72+
mountComponent();
73+
expect(mountFn).toHaveBeenCalled();
74+
getComponent()!.remove();
75+
expect(unmountFn).toHaveBeenCalled();
76+
});
77+
78+
it("propagates events out", () => {
79+
const spyClick = vi.fn();
80+
mountComponent();
81+
getComponent()!.addEventListener("count", spyClick);
82+
queryInShadow("button")?.click();
83+
expect(spyClick).toHaveBeenCalled();
84+
});
85+
86+
it("has a slot as children", () => {
87+
mountComponent("Bob", "Hi mom");
88+
const slotted = queryInShadow("slot")?.assignedNodes();
89+
expect(slotted?.[0].textContent).toContain("Hi mom");
90+
});
91+
});

tests/light-dom.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import van from "vanjs-core";
2+
import { define } from "../src/van-element";
3+
import { describe, expect, it } from "vitest";
4+
5+
const { div } = van.tags;
6+
7+
define("light-dom", ({ children }) => div({ class: "light" }, children), false);
8+
define("shadow-dom", () => div({ class: "shadow" }, "World"));
9+
10+
describe("Shadow DOM options", () => {
11+
it("should expose light Van Element", () => {
12+
document.body.innerHTML = `<light-dom></light-dom>`;
13+
expect(document.querySelector(".light")).toBeTruthy();
14+
});
15+
16+
it("should encapsulate shadow Van Element", () => {
17+
document.body.innerHTML = `<shadow-dom></shadow-dom>`;
18+
expect(
19+
document.querySelector("shadow-dom")?.shadowRoot?.querySelector(".shadow")
20+
).toBeTruthy();
21+
expect(document.querySelector(".shadow")).toBeFalsy();
22+
});
23+
24+
it("should polyfill light children", () => {
25+
const stringToTest = "Hello world";
26+
document.body.innerHTML = `<light-dom>${stringToTest}</light-dom>`;
27+
expect(document.querySelector(".light")?.textContent).toBe(stringToTest);
28+
});
29+
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
"noUnusedParameters": true,
2020
"noFallthroughCasesInSwitch": true
2121
},
22-
"include": ["src", "types", "docs"]
22+
"include": ["src", "types", "docs", "tests"]
2323
}

0 commit comments

Comments
 (0)