Skip to content

Commit d287830

Browse files
committed
more components tests
1 parent 372e489 commit d287830

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { FC } from "react";
2+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
3+
import { render, screen } from "@testing-library/react";
4+
import { registry } from "@/component/Registry";
5+
import { type ComponentProps } from "./Component";
6+
import { Children } from "./Children";
7+
8+
describe("Children", () => {
9+
beforeEach(() => {
10+
registry.clear();
11+
});
12+
13+
afterEach(() => {
14+
registry.clear();
15+
});
16+
17+
function expectDocumentIsEmpty() {
18+
// Note, the following 3 lines test that document is empty
19+
// but there is always one empty "div" in it.
20+
expect(document.body.firstElementChild).not.toBe(null);
21+
expect(document.body.firstElementChild.tagName).toBe("DIV");
22+
expect(document.body.firstElementChild.firstElementChild).toBe(null);
23+
}
24+
25+
it("should not render undefined nodes", () => {
26+
render(<Children onChange={() => {}} />);
27+
expectDocumentIsEmpty();
28+
});
29+
30+
it("should not render empty nodes", () => {
31+
render(<Children nodes={[]} onChange={() => {}} />);
32+
expectDocumentIsEmpty();
33+
});
34+
35+
it("should render all child types", () => {
36+
const Div: FC<ComponentProps & { text: string }> = ({ text }) => (
37+
<div>{text}</div>
38+
);
39+
registry.register("Div", Div);
40+
const divProps = {
41+
type: "Div",
42+
id: "root",
43+
text: "Hello",
44+
onChange: () => {},
45+
};
46+
render(
47+
<Children
48+
nodes={[
49+
divProps, // ok, regular component
50+
"World", // ok, text
51+
null, // ok, not rendered
52+
undefined, // ok, not rendered
53+
<div />, // not ok, emits warning, not rendered
54+
]}
55+
onChange={() => {}}
56+
/>,
57+
);
58+
// to inspect rendered component, do:
59+
// expect(document.body).toEqual({});
60+
expect(screen.getByText("Hello")).not.toBeUndefined();
61+
expect(screen.getByText("World")).not.toBeUndefined();
62+
});
63+
});
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
22
import { render, screen } from "@testing-library/react";
3-
import { Component } from "./Component";
3+
import { Component, type ComponentProps } from "./Component";
4+
import { registry } from "@/component/Registry";
5+
import type { FC } from "react";
46

57
describe("Component", () => {
6-
it("should render the Component component", () => {
8+
beforeEach(() => {
9+
registry.clear();
10+
});
11+
12+
afterEach(() => {
13+
registry.clear();
14+
});
15+
16+
it("should not render unknown Component types", () => {
717
const boxProps = {
818
type: "Box",
919
id: "bx",
@@ -13,6 +23,23 @@ describe("Component", () => {
1323
render(<Component {...boxProps} />);
1424
// to inspect rendered component, do:
1525
// expect(document.body).toEqual({});
16-
expect(screen.getByText("World!")).toBeUndefined();
26+
expect(document.querySelector("#bx")).toBe(null);
27+
});
28+
29+
it("should render a known component", () => {
30+
const Div: FC<ComponentProps & { text: string }> = ({ text }) => (
31+
<div>{text}</div>
32+
);
33+
registry.register("Div", Div);
34+
const divProps = {
35+
type: "Div",
36+
id: "root",
37+
text: "Hello!",
38+
onChange: () => {},
39+
};
40+
render(<Component {...divProps} />);
41+
// to inspect rendered component, do:
42+
// expect(document.body).toEqual({});
43+
expect(screen.getByText("Hello!")).not.toBeUndefined();
1744
});
1845
});

chartlets.js/packages/lib/src/component/Registry.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,17 @@ describe("Test that RegistryImpl", () => {
5050
expect(registry.lookup("C")).toBeUndefined();
5151
expect(registry.types).toEqual([]);
5252
});
53+
54+
it("clears", () => {
55+
const registry = new RegistryImpl();
56+
const A = () => void 0;
57+
const B = () => void 0;
58+
const C = () => void 0;
59+
registry.register("A", A);
60+
registry.register("B", B);
61+
registry.register("C", C);
62+
expect(registry.types.length).toBe(3);
63+
registry.clear();
64+
expect(registry.types).toEqual([]);
65+
});
5366
});

chartlets.js/packages/lib/src/component/Registry.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export interface Registry {
3232
*/
3333
lookup(type: string): ComponentType<ComponentProps> | undefined;
3434

35+
/**
36+
* Clears the registry.
37+
* For testing only.
38+
*/
39+
clear(): void;
40+
3541
/**
3642
* Get the type names of all registered components.
3743
*/
@@ -58,6 +64,10 @@ export class RegistryImpl implements Registry {
5864
return this.components.get(type);
5965
}
6066

67+
clear() {
68+
this.components.clear();
69+
}
70+
6171
get types(): string[] {
6272
return Array.from(this.components.keys());
6373
}

0 commit comments

Comments
 (0)