-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildren.test.tsx
More file actions
62 lines (56 loc) · 1.78 KB
/
Children.test.tsx
File metadata and controls
62 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { FC } from "react";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { render, screen } from "@testing-library/react";
import { registry } from "@/components/registry";
import { Children } from "./Children";
describe("Children", () => {
beforeEach(() => {
registry.clear();
});
afterEach(() => {
registry.clear();
});
function expectDocumentIsEmpty() {
// Note, the following 3 lines test that document is empty
// but there is always one empty "div" in it.
expect(document.body.firstElementChild).not.toBe(null);
expect(document.body.firstElementChild!.tagName).toBe("DIV");
expect(document.body.firstElementChild!.firstElementChild).toBe(null);
}
it("should not render undefined nodes", () => {
render(<Children onChange={() => {}} />);
expectDocumentIsEmpty();
});
it("should not render empty nodes", () => {
render(<Children nodes={[]} onChange={() => {}} />);
expectDocumentIsEmpty();
});
it("should render all child types", () => {
interface DivProps {
text: string;
}
const Div: FC<DivProps> = ({ text }) => <div>{text}</div>;
registry.register("Div", Div);
const divProps = {
type: "Div",
text: "Hello",
onChange: () => {},
};
render(
<Children
nodes={[
divProps, // ok, regular component
"World", // ok, text
null, // ok, not rendered
undefined, // ok, not rendered
<div />, // not ok, emits warning, not rendered
]}
onChange={() => {}}
/>,
);
// to inspect rendered component, do:
// expect(document.body).toEqual({});
expect(screen.getByText("Hello")).not.toBeUndefined();
expect(screen.getByText("World")).not.toBeUndefined();
});
});