Skip to content

Commit 3a479bc

Browse files
authored
Merge pull request #70 from bcdev/forman-33-even_more_tests
Even more tests
2 parents fabe4a5 + ca7b661 commit 3a479bc

23 files changed

+702
-40
lines changed

chartlets.js/package-lock.json

Lines changed: 290 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chartlets.js/packages/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@typescript-eslint/eslint-plugin": "^7.18.0",
4444
"@typescript-eslint/parser": "^7.18.0",
4545
"@vitejs/plugin-react-swc": "^3.7.0",
46-
"@vitest/coverage-v8": "^2.1.1",
46+
"@vitest/coverage-istanbul": "^2.1.8",
4747
"eslint": "^8.57.1",
4848
"eslint-plugin-react-hooks": "^4.6.2",
4949
"eslint-plugin-react-refresh": "^0.4.12",

chartlets.js/packages/lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"@typescript-eslint/eslint-plugin": "^7.18.0",
8383
"@typescript-eslint/parser": "^7.18.0",
8484
"@vitejs/plugin-react-swc": "^3.7.0",
85-
"@vitest/coverage-v8": "^2.1.1",
85+
"@vitest/coverage-istanbul": "^2.1.8",
8686
"canvas": "^2.11.2",
8787
"eslint": "^8.57.1",
8888
"eslint-plugin-react-hooks": "^4.6.2",
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+
interface DivProps extends ComponentProps {
37+
text: string;
38+
}
39+
const Div: FC<DivProps> = ({ text }) => <div>{text}</div>;
40+
registry.register("Div", Div as FC<ComponentProps>);
41+
const divProps = {
42+
type: "Div",
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
2+
import { render, screen } from "@testing-library/react";
3+
import { Component, type ComponentProps } from "./Component";
4+
import { registry } from "@/component/Registry";
5+
import type { FC } from "react";
6+
7+
describe("Component", () => {
8+
beforeEach(() => {
9+
registry.clear();
10+
});
11+
12+
afterEach(() => {
13+
registry.clear();
14+
});
15+
16+
it("should not render unknown Component types", () => {
17+
const boxProps = {
18+
type: "Box",
19+
id: "bx",
20+
children: ["Hello!", "World!"],
21+
onChange: () => {},
22+
};
23+
render(<Component {...boxProps} />);
24+
// to inspect rendered component, do:
25+
// expect(document.body).toEqual({});
26+
expect(document.querySelector("#bx")).toBe(null);
27+
});
28+
29+
it("should render a known component", () => {
30+
interface DivProps extends ComponentProps {
31+
text: string;
32+
}
33+
const Div: FC<DivProps> = ({ text }) => <div>{text}</div>;
34+
registry.register("Div", Div as FC<ComponentProps>);
35+
const divProps = {
36+
type: "Div",
37+
id: "root",
38+
text: "Hello!",
39+
onChange: () => {},
40+
};
41+
render(<Component {...divProps} />);
42+
// to inspect rendered component, do:
43+
// expect(document.body).toEqual({});
44+
expect(screen.getByText("Hello!")).not.toBeUndefined();
45+
});
46+
});

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
}

chartlets.js/packages/lib/src/plugins/mui/Box.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect } from "vitest";
22
import { render, screen } from "@testing-library/react";
3-
import { Box } from "./Box";
43
import type { ComponentChangeHandler } from "@/types/state/event";
4+
import { Box } from "./Box";
55

66
describe("Box", () => {
77
it("should render the Box component", () => {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, it, expect } from "vitest";
2+
import mui from "./index";
3+
4+
describe("mui Plugin", () => {
5+
it("registers components", () => {
6+
const plugin = mui();
7+
expect(plugin).toBeTypeOf("object");
8+
expect(Array.isArray(plugin.components)).toBe(true);
9+
expect(plugin.components?.length).toBeGreaterThan(0);
10+
plugin.components?.forEach((componentRegistration) => {
11+
expect(componentRegistration).toHaveLength(2);
12+
const [name, component] = componentRegistration;
13+
expect(name).toBeTypeOf("string");
14+
expect(component).toBeTypeOf("function");
15+
});
16+
});
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, it, expect } from "vitest";
2+
import vega from "./index";
3+
4+
describe("vega Plugin", () => {
5+
it("registers components", () => {
6+
const plugin = vega();
7+
expect(plugin).toBeTypeOf("object");
8+
expect(Array.isArray(plugin.components)).toBe(true);
9+
expect(plugin.components?.length).toBeGreaterThan(0);
10+
plugin.components?.forEach((componentRegistration) => {
11+
expect(componentRegistration).toHaveLength(2);
12+
const [name, component] = componentRegistration;
13+
expect(name).toBeTypeOf("string");
14+
expect(component).toBeTypeOf("function");
15+
});
16+
});
17+
});

0 commit comments

Comments
 (0)