@gravity-ui/graph
Version:
Modern graph editor component
61 lines (60 loc) • 2.46 kB
JavaScript
import React, { createRef } from "react";
import { act, render, waitFor } from "@testing-library/react";
import { Graph } from "../graph";
import { Layer } from "../services/Layer";
import { GraphCanvas } from "./GraphCanvas";
import { GraphLayer } from "./GraphLayer";
// Mock Layer for testing
class MockLayer extends Layer {
testMethod() {
return "test method called";
}
}
describe("GraphLayer", () => {
let graph;
beforeEach(() => {
graph = new Graph({});
});
afterEach(() => {
act(() => {
graph?.unmount();
});
});
it("should provide layer instance through ref", async () => {
const ref = createRef();
render(React.createElement(GraphCanvas, { graph: graph, renderBlock: () => React.createElement("div", null, "Block") },
React.createElement(GraphLayer, { ref: ref, layer: MockLayer })));
// Start the graph to make it ready
await act(async () => {
graph.start();
});
// Wait for layer to be created
await waitFor(() => {
expect(ref.current).toBeDefined();
expect(ref.current).toBeInstanceOf(MockLayer);
expect(ref.current?.testMethod()).toBe("test method called");
});
});
it("should not render any visible content", () => {
const { container } = render(React.createElement(GraphCanvas, { graph: graph, renderBlock: () => React.createElement("div", null, "Block") },
React.createElement(GraphLayer, { layer: MockLayer })));
// GraphLayer should not add any DOM elements
expect(container.children).toHaveLength(1); // Only GraphCanvas
});
it("should create layer with correct props", async () => {
const ref = createRef();
const customProps = { zIndex: 200 };
render(React.createElement(GraphCanvas, { graph: graph, renderBlock: () => React.createElement("div", null, "Block") },
React.createElement(GraphLayer, { ref: ref, layer: MockLayer, ...customProps })));
// Start the graph to make it ready
await act(async () => {
graph.start();
});
// Wait for layer to be created
await waitFor(() => {
expect(ref.current).toBeDefined();
// Layer props should be passed correctly (checked through layer existence)
expect(ref.current).toBeInstanceOf(MockLayer);
});
});
});