Skip to content

Commit 6ee2121

Browse files
committed
Fix tests
1 parent 88a535a commit 6ee2121

File tree

3 files changed

+122
-38
lines changed

3 files changed

+122
-38
lines changed

packages/graph-explorer/src/hooks/useAddToGraph.test.ts

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ import {
2121
VertexTypeConfig,
2222
} from "@/core";
2323
import { waitFor } from "@testing-library/react";
24+
import { useMaterializeVertices } from "./useMaterializeVertices";
25+
import { cloneDeep } from "lodash";
26+
27+
vi.mock("./useMaterializeVertices", () => ({
28+
useMaterializeVertices: vi.fn(),
29+
}));
30+
31+
beforeEach(() => {
32+
vi.resetAllMocks();
33+
});
2434

2535
test("should add one node", async () => {
2636
const vertex = createRandomVertex();
2737

38+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
39+
Promise.resolve(toNodeMap([vertex]))
40+
);
41+
2842
const { result } = renderHookWithRecoilRoot(() => {
2943
const callback = useAddToGraph();
3044
const vertices = useRecoilValue(nodesAtom);
@@ -43,11 +57,43 @@ test("should add one node", async () => {
4357
});
4458
});
4559

60+
test("should materialize fragment vertices", async () => {
61+
const vertex = createRandomVertex();
62+
const clonedVertex = cloneDeep(vertex);
63+
vertex.__isFragment = true;
64+
vertex.attributes = {};
65+
66+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
67+
Promise.resolve(toNodeMap([clonedVertex]))
68+
);
69+
70+
const { result } = renderHookWithRecoilRoot(() => {
71+
const callback = useAddToGraph();
72+
const vertices = useRecoilValue(nodesAtom);
73+
const edges = useRecoilValue(edgesAtom);
74+
75+
return { callback, vertices, edges };
76+
});
77+
78+
act(() => {
79+
result.current.callback({ vertices: [vertex] });
80+
});
81+
82+
await waitFor(() => {
83+
const actual = result.current.vertices.get(vertex.id);
84+
expect(actual).toEqual(clonedVertex);
85+
});
86+
});
87+
4688
test("should add one edge", async () => {
4789
const node1 = createRandomVertex();
4890
const node2 = createRandomVertex();
4991
const edge = createRandomEdge(node1, node2);
5092

93+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
94+
Promise.resolve(toNodeMap([node1, node2]))
95+
);
96+
5197
const { result } = renderHookWithRecoilRoot(
5298
() => {
5399
const callback = useAddToGraph();
@@ -74,6 +120,10 @@ test("should add one edge", async () => {
74120
test("should add multiple nodes and edges", async () => {
75121
const randomEntities = createRandomEntities();
76122

123+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
124+
Promise.resolve(randomEntities.nodes)
125+
);
126+
77127
const { result } = renderHookWithRecoilRoot(() => {
78128
const callback = useAddToGraph();
79129
const vertices = useRecoilValue(nodesAtom);
@@ -105,6 +155,10 @@ test("should update schema when adding a node", async () => {
105155
const expectedVertexType = extractConfigFromEntity(vertex);
106156
const dbState = new DbState();
107157

158+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
159+
Promise.resolve(toNodeMap([vertex]))
160+
);
161+
108162
const { result } = renderHookWithRecoilRoot(
109163
() => {
110164
const callback = useAddToGraph();
@@ -136,6 +190,10 @@ test("should update schema when adding a node with no label", async () => {
136190
const expectedVertexType = extractConfigFromEntity(vertex);
137191
const dbState = new DbState();
138192

193+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
194+
Promise.resolve(toNodeMap([vertex]))
195+
);
196+
139197
const { result } = renderHookWithRecoilRoot(
140198
() => {
141199
const callback = useAddToGraph();
@@ -169,6 +227,10 @@ test("should update schema when adding an edge", async () => {
169227
dbState.addVertexToGraph(node1);
170228
dbState.addVertexToGraph(node2);
171229

230+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
231+
Promise.resolve(toNodeMap([node1, node2]))
232+
);
233+
172234
const { result } = renderHookWithRecoilRoot(
173235
() => {
174236
const callback = useAddToGraph();
@@ -205,6 +267,10 @@ test("should add missing attributes to the schema when adding a node", async ()
205267
};
206268
dbState.activeSchema.vertices.push(initialVtConfig);
207269

270+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
271+
Promise.resolve(toNodeMap([vertex]))
272+
);
273+
208274
const { result } = renderHookWithRecoilRoot(
209275
() => {
210276
const callback = useAddToGraph();
@@ -245,6 +311,10 @@ test("should add missing attributes to the schema when adding an edge", async ()
245311
};
246312
dbState.activeSchema.edges.push(initialEtConfig);
247313

314+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
315+
Promise.resolve(toNodeMap([node1, node2]))
316+
);
317+
248318
const { result } = renderHookWithRecoilRoot(
249319
() => {
250320
const callback = useAddToGraph();
@@ -270,8 +340,13 @@ test("should add missing attributes to the schema when adding an edge", async ()
270340
});
271341

272342
test("should update graph storage when adding a node", async () => {
343+
const vertex = createRandomVertex();
273344
const dbState = new DbState();
274345

346+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
347+
Promise.resolve(toNodeMap([vertex]))
348+
);
349+
275350
const { result } = renderHookWithRecoilRoot(
276351
() => {
277352
const callback = useAddToGraph();
@@ -281,7 +356,6 @@ test("should update graph storage when adding a node", async () => {
281356
snapshot => dbState.applyTo(snapshot)
282357
);
283358

284-
const vertex = createRandomVertex();
285359
act(() => {
286360
result.current.callback({ vertices: [vertex] });
287361
});
@@ -305,6 +379,10 @@ test("should update graph storage when adding an edge", async () => {
305379
dbState.addVertexToGraph(node1);
306380
dbState.addVertexToGraph(node2);
307381

382+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
383+
Promise.resolve(toNodeMap([node1, node2]))
384+
);
385+
308386
const { result } = renderHookWithRecoilRoot(
309387
() => {
310388
const callback = useAddToGraph();
@@ -329,8 +407,17 @@ test("should update graph storage when adding an edge", async () => {
329407
});
330408

331409
test("should ignore blank nodes when updating graph storage", async () => {
410+
const vertex = createRandomVertexForRdf();
411+
const blankNode = createRandomVertexForRdf();
412+
blankNode.__isBlank = true;
413+
const edge = createRandomEdge(vertex, blankNode);
414+
332415
const dbState = new DbState();
333416

417+
vi.mocked(useMaterializeVertices).mockReturnValue(() =>
418+
Promise.resolve(toNodeMap([vertex, blankNode]))
419+
);
420+
334421
const { result } = renderHookWithRecoilRoot(
335422
() => {
336423
const callback = useAddToGraph();
@@ -340,11 +427,6 @@ test("should ignore blank nodes when updating graph storage", async () => {
340427
snapshot => dbState.applyTo(snapshot)
341428
);
342429

343-
const vertex = createRandomVertexForRdf();
344-
const blankNode = createRandomVertexForRdf();
345-
blankNode.__isBlank = true;
346-
const edge = createRandomEdge(vertex, blankNode);
347-
348430
act(() => {
349431
result.current.callback({ vertices: [vertex, blankNode], edges: [edge] });
350432
});

packages/graph-explorer/src/hooks/useAddToGraph.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useNotification } from "@/components/NotificationProvider";
2-
import { vertexDetailsQuery, VertexDetailsRequest } from "@/connector";
32
import {
43
activeSchemaSelector,
54
createVertex,
@@ -9,16 +8,15 @@ import {
98
toEdgeMap,
109
toNodeMap,
1110
updateSchemaFromEntities,
12-
useExplorer,
1311
useUpdateGraphSession,
1412
Vertex,
15-
VertexId,
1613
} from "@/core";
1714
import { logger } from "@/utils";
1815
import { createDisplayError } from "@/utils/createDisplayError";
19-
import { useMutation, useQueryClient } from "@tanstack/react-query";
16+
import { useMutation } from "@tanstack/react-query";
2017
import { startTransition, useCallback } from "react";
2118
import { useSetRecoilState } from "recoil";
19+
import { useMaterializeVertices } from "./useMaterializeVertices";
2220

2321
/** Returns a callback that adds an array of nodes and edges to the graph. */
2422
export function useAddToGraph() {
@@ -93,34 +91,6 @@ export function useAddToGraph() {
9391
);
9492
}
9593

96-
/** Fetch the details if the vertex is a fragment. */
97-
function useMaterializeVertices() {
98-
const queryClient = useQueryClient();
99-
const explorer = useExplorer();
100-
101-
return useCallback(
102-
async (vertices: Map<VertexId, Vertex>) => {
103-
const responses = await Promise.all(
104-
vertices.values().map(async vertex => {
105-
if (!vertex.__isFragment) {
106-
return vertex;
107-
}
108-
109-
const request: VertexDetailsRequest = {
110-
vertexId: vertex.id,
111-
};
112-
const response = await queryClient.ensureQueryData(
113-
vertexDetailsQuery(request, explorer)
114-
);
115-
return response.vertex;
116-
})
117-
);
118-
return toNodeMap(responses.filter(vertex => vertex != null));
119-
},
120-
[queryClient, explorer]
121-
);
122-
}
123-
12494
/** Returns a callback the given vertex to the graph. */
12595
export function useAddVertexToGraph(vertex: Vertex) {
12696
const callback = useAddToGraph();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { VertexDetailsRequest, vertexDetailsQuery } from "@/connector";
2+
import { useExplorer, VertexId, Vertex, toNodeMap } from "@/core";
3+
import { useQueryClient } from "@tanstack/react-query";
4+
import { useCallback } from "react";
5+
6+
/** Fetch the details if the vertex is a fragment. */
7+
export function useMaterializeVertices() {
8+
const queryClient = useQueryClient();
9+
const explorer = useExplorer();
10+
11+
return useCallback(
12+
async (vertices: Map<VertexId, Vertex>) => {
13+
const responses = await Promise.all(
14+
vertices.values().map(async vertex => {
15+
if (!vertex.__isFragment) {
16+
return vertex;
17+
}
18+
19+
const request: VertexDetailsRequest = {
20+
vertexId: vertex.id,
21+
};
22+
const response = await queryClient.ensureQueryData(
23+
vertexDetailsQuery(request, explorer)
24+
);
25+
return response.vertex;
26+
})
27+
);
28+
return toNodeMap(responses.filter(vertex => vertex != null));
29+
},
30+
[queryClient, explorer]
31+
);
32+
}

0 commit comments

Comments
 (0)