Adding TreeView & ScrollSpy tests

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev committed 2021-03-09 14:50:38 +03:00
1 parent 8339ecc5f1
commit 66994bdf4c
6 files changed
+209 -11

No files matched your search

@@ -80,9 +80,9 @@ export class PageLayout extends React.Component<PageLayoutProps> {
<div className="content-scrollable-area">
<div className="content-wrapper">
{ showNavigation && (
<div className="content-navigation">
<nav className="content-navigation">
<RecursiveTreeView data={navigation}/>
</div>
</nav>
)}
<div className={cssNames("content", contentGaps && "flex column gaps", contentClass)}>
{children}
@@ -0,0 +1,186 @@
import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { render, waitFor } from "@testing-library/react";
import { ScrollSpy } from "../scroll-spy";
import { RecursiveTreeView } from "../../tree-view";
const observe = jest.fn();
Object.defineProperty(window, "IntersectionObserver", {
writable: true,
value: jest.fn().mockImplementation(() => ({
observe,
unobserve: jest.fn(),
})),
});
describe("<ScrollSpy/>", () => {
it("renders w/o errors", () => {
const { container } = render(<ScrollSpy render={() => (
<div>
<section id="application">
<h1>Application</h1>
</section>
</div>
)}/>);
expect(container).toBeInstanceOf(HTMLElement);
});
it("calls intersection observer", () => {
render(<ScrollSpy render={() => (
<div>
<section id="application">
<h1>Application</h1>
</section>
</div>
)}/>);
expect(observe).toHaveBeenCalled();
});
it("renders navigation component", async () => {
const { queryByTestId } = render(<ScrollSpy render={navigation => (
<div>
<nav>
<RecursiveTreeView data={navigation}/>
</nav>
<section id="application">
<h1>Application</h1>
</section>
</div>
)}/>);
await waitFor(() => {
expect(queryByTestId("TreeView")).toBeInTheDocument();
});
});
it("throws if no sections founded", () => {
// Prevent writing to stderr during this render.
const err = console.error;
console.error = jest.fn();
expect(() => render(<ScrollSpy render={() => (
<div>
Content
</div>
)}/>)).toThrow();
// Restore writing to stderr.
console.error = err;
});
});
describe("<TreeView/> navigation inside <ScrollSpy/>", () => {
it("contains links to all sections", async () => {
const { queryByTitle } = render(<ScrollSpy render={navigation => (
<div>
<nav>
<RecursiveTreeView data={navigation}/>
</nav>
<section id="application">
<h1>Application</h1>
<section id="appearance">
<h2>Appearance</h2>
</section>
<section id="theme">
<h2>Theme</h2>
<div>description</div>
</section>
</section>
</div>
)}/>);
await waitFor(() => {
expect(queryByTitle("Application")).toBeInTheDocument();
expect(queryByTitle("Appearance")).toBeInTheDocument();
expect(queryByTitle("Theme")).toBeInTheDocument();
});
});
it("not showing links to sections without id", async () => {
const { queryByTitle } = render(<ScrollSpy render={navigation => (
<div>
<nav>
<RecursiveTreeView data={navigation}/>
</nav>
<section id="application">
<h1>Application</h1>
<section>
<h2>Kubectl</h2>
</section>
<section id="appearance">
<h2>Appearance</h2>
</section>
</section>
</div>
)}/>);
await waitFor(() => {
expect(queryByTitle("Application")).toBeInTheDocument();
expect(queryByTitle("Appearance")).toBeInTheDocument();
expect(queryByTitle("Kubectl")).not.toBeInTheDocument();
});
});
it("expands parent sections", async () => {
const { queryByTitle } = render(<ScrollSpy render={navigation => (
<div>
<nav>
<RecursiveTreeView data={navigation}/>
</nav>
<section id="application">
<h1>Application</h1>
<section id="appearance">
<h2>Appearance</h2>
</section>
<section id="theme">
<h2>Theme</h2>
<div>description</div>
</section>
</section>
<section id="Kubernetes">
<h1>Kubernetes</h1>
<section id="kubectl">
<h2>Kubectl</h2>
</section>
</section>
</div>
)}/>);
await waitFor(() => {
expect(queryByTitle("Application")).toHaveAttribute("aria-expanded");
expect(queryByTitle("Kubernetes")).toHaveAttribute("aria-expanded");
});
// console.log(prettyDOM());
});
it("skips sections without headings", async () => {
const { queryByTitle } = render(<ScrollSpy render={navigation => (
<div>
<nav>
<RecursiveTreeView data={navigation}/>
</nav>
<section id="application">
<h1>Application</h1>
<section id="appearance">
<p>Appearance</p>
</section>
<section id="theme">
<h2>Theme</h2>
</section>
</section>
</div>
)}/>);
await waitFor(() => {
expect(queryByTitle("Application")).toBeInTheDocument();
expect(queryByTitle("appearance")).not.toBeInTheDocument();
expect(queryByTitle("Appearance")).not.toBeInTheDocument();
});
});
});
@@ -31,7 +31,7 @@ export function ScrollSpy(props: Props) {
sections.forEach(section => {
const id = section.getAttribute("id");
const parentId = section.parentElement.id;
const name = section.querySelector("h1, h2, h3, h4, h5, h6").textContent;
const name = section.querySelector("h1, h2, h3, h4, h5, h6")?.textContent;
const selected = id === activeElementId;
if (!name || !id) {
@@ -5,7 +5,11 @@ import { Icon } from "../icon";
import TreeView from "@material-ui/lab/TreeView";
import TreeItem from "@material-ui/lab/TreeItem";
import { cssNames } from "../../utils";
import findDeep from "deepdash-es/findDeep";
import _ from "lodash";
import getDeepDash from "deepdash";
const deepDash = getDeepDash(_);
export interface NavigationTree {
id: string;
@@ -26,7 +30,7 @@ const scrollToItem = (id: string) => {
};
const getSelectedNode = (data: NavigationTree[]) => {
return findDeep(data, (value, key) => key === "selected" && value === true)?.parent;
return deepDash.findDeep(data, (value, key) => key === "selected" && value === true)?.parent;
};
export function RecursiveTreeView({ data }: Props) {
@@ -63,6 +67,7 @@ export function RecursiveTreeView({ data }: Props) {
label={node.name}
onLabelClick={(event) => onLabelClick(event, node.id)}
className={cssNames({selected: node.selected})}
title={node.name}
>
{Array.isArray(node.children) ? node.children.map((node) => renderTree([node])) : null}
</TreeItem>
@@ -84,6 +89,7 @@ export function RecursiveTreeView({ data }: Props) {
return (
<TreeView
data-testid="TreeView"
className="TreeView"
expanded={expanded}
onNodeToggle={handleToggle}