@@ -21,10 +21,24 @@ import {
21
21
VertexTypeConfig ,
22
22
} from "@/core" ;
23
23
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
+ } ) ;
24
34
25
35
test ( "should add one node" , async ( ) => {
26
36
const vertex = createRandomVertex ( ) ;
27
37
38
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
39
+ Promise . resolve ( toNodeMap ( [ vertex ] ) )
40
+ ) ;
41
+
28
42
const { result } = renderHookWithRecoilRoot ( ( ) => {
29
43
const callback = useAddToGraph ( ) ;
30
44
const vertices = useRecoilValue ( nodesAtom ) ;
@@ -43,11 +57,43 @@ test("should add one node", async () => {
43
57
} ) ;
44
58
} ) ;
45
59
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
+
46
88
test ( "should add one edge" , async ( ) => {
47
89
const node1 = createRandomVertex ( ) ;
48
90
const node2 = createRandomVertex ( ) ;
49
91
const edge = createRandomEdge ( node1 , node2 ) ;
50
92
93
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
94
+ Promise . resolve ( toNodeMap ( [ node1 , node2 ] ) )
95
+ ) ;
96
+
51
97
const { result } = renderHookWithRecoilRoot (
52
98
( ) => {
53
99
const callback = useAddToGraph ( ) ;
@@ -74,6 +120,10 @@ test("should add one edge", async () => {
74
120
test ( "should add multiple nodes and edges" , async ( ) => {
75
121
const randomEntities = createRandomEntities ( ) ;
76
122
123
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
124
+ Promise . resolve ( randomEntities . nodes )
125
+ ) ;
126
+
77
127
const { result } = renderHookWithRecoilRoot ( ( ) => {
78
128
const callback = useAddToGraph ( ) ;
79
129
const vertices = useRecoilValue ( nodesAtom ) ;
@@ -105,6 +155,10 @@ test("should update schema when adding a node", async () => {
105
155
const expectedVertexType = extractConfigFromEntity ( vertex ) ;
106
156
const dbState = new DbState ( ) ;
107
157
158
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
159
+ Promise . resolve ( toNodeMap ( [ vertex ] ) )
160
+ ) ;
161
+
108
162
const { result } = renderHookWithRecoilRoot (
109
163
( ) => {
110
164
const callback = useAddToGraph ( ) ;
@@ -136,6 +190,10 @@ test("should update schema when adding a node with no label", async () => {
136
190
const expectedVertexType = extractConfigFromEntity ( vertex ) ;
137
191
const dbState = new DbState ( ) ;
138
192
193
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
194
+ Promise . resolve ( toNodeMap ( [ vertex ] ) )
195
+ ) ;
196
+
139
197
const { result } = renderHookWithRecoilRoot (
140
198
( ) => {
141
199
const callback = useAddToGraph ( ) ;
@@ -169,6 +227,10 @@ test("should update schema when adding an edge", async () => {
169
227
dbState . addVertexToGraph ( node1 ) ;
170
228
dbState . addVertexToGraph ( node2 ) ;
171
229
230
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
231
+ Promise . resolve ( toNodeMap ( [ node1 , node2 ] ) )
232
+ ) ;
233
+
172
234
const { result } = renderHookWithRecoilRoot (
173
235
( ) => {
174
236
const callback = useAddToGraph ( ) ;
@@ -205,6 +267,10 @@ test("should add missing attributes to the schema when adding a node", async ()
205
267
} ;
206
268
dbState . activeSchema . vertices . push ( initialVtConfig ) ;
207
269
270
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
271
+ Promise . resolve ( toNodeMap ( [ vertex ] ) )
272
+ ) ;
273
+
208
274
const { result } = renderHookWithRecoilRoot (
209
275
( ) => {
210
276
const callback = useAddToGraph ( ) ;
@@ -245,6 +311,10 @@ test("should add missing attributes to the schema when adding an edge", async ()
245
311
} ;
246
312
dbState . activeSchema . edges . push ( initialEtConfig ) ;
247
313
314
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
315
+ Promise . resolve ( toNodeMap ( [ node1 , node2 ] ) )
316
+ ) ;
317
+
248
318
const { result } = renderHookWithRecoilRoot (
249
319
( ) => {
250
320
const callback = useAddToGraph ( ) ;
@@ -270,8 +340,13 @@ test("should add missing attributes to the schema when adding an edge", async ()
270
340
} ) ;
271
341
272
342
test ( "should update graph storage when adding a node" , async ( ) => {
343
+ const vertex = createRandomVertex ( ) ;
273
344
const dbState = new DbState ( ) ;
274
345
346
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
347
+ Promise . resolve ( toNodeMap ( [ vertex ] ) )
348
+ ) ;
349
+
275
350
const { result } = renderHookWithRecoilRoot (
276
351
( ) => {
277
352
const callback = useAddToGraph ( ) ;
@@ -281,7 +356,6 @@ test("should update graph storage when adding a node", async () => {
281
356
snapshot => dbState . applyTo ( snapshot )
282
357
) ;
283
358
284
- const vertex = createRandomVertex ( ) ;
285
359
act ( ( ) => {
286
360
result . current . callback ( { vertices : [ vertex ] } ) ;
287
361
} ) ;
@@ -305,6 +379,10 @@ test("should update graph storage when adding an edge", async () => {
305
379
dbState . addVertexToGraph ( node1 ) ;
306
380
dbState . addVertexToGraph ( node2 ) ;
307
381
382
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
383
+ Promise . resolve ( toNodeMap ( [ node1 , node2 ] ) )
384
+ ) ;
385
+
308
386
const { result } = renderHookWithRecoilRoot (
309
387
( ) => {
310
388
const callback = useAddToGraph ( ) ;
@@ -329,8 +407,17 @@ test("should update graph storage when adding an edge", async () => {
329
407
} ) ;
330
408
331
409
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
+
332
415
const dbState = new DbState ( ) ;
333
416
417
+ vi . mocked ( useMaterializeVertices ) . mockReturnValue ( ( ) =>
418
+ Promise . resolve ( toNodeMap ( [ vertex , blankNode ] ) )
419
+ ) ;
420
+
334
421
const { result } = renderHookWithRecoilRoot (
335
422
( ) => {
336
423
const callback = useAddToGraph ( ) ;
@@ -340,11 +427,6 @@ test("should ignore blank nodes when updating graph storage", async () => {
340
427
snapshot => dbState . applyTo ( snapshot )
341
428
) ;
342
429
343
- const vertex = createRandomVertexForRdf ( ) ;
344
- const blankNode = createRandomVertexForRdf ( ) ;
345
- blankNode . __isBlank = true ;
346
- const edge = createRandomEdge ( vertex , blankNode ) ;
347
-
348
430
act ( ( ) => {
349
431
result . current . callback ( { vertices : [ vertex , blankNode ] , edges : [ edge ] } ) ;
350
432
} ) ;
0 commit comments