|
1 | | -import { describe, expectTypeOf, it } from 'vitest'; |
| 1 | +import { describe, expectTypeOf, it, expect } from 'vitest'; |
2 | 2 | import { Graph } from '../Graph.js'; |
3 | 3 | import { checkSerialized } from '../test-utils.js'; |
| 4 | +import { Serialized } from '../types.js'; |
4 | 5 | import { deserializeGraph } from './deserializeGraph.js'; |
5 | 6 | import { serializeGraph } from './serializeGraph.js'; |
6 | 7 |
|
7 | 8 | describe('serializeGraph', () => { |
8 | | - it('Should deserialize a graph.', function () { |
| 9 | + it('should deserialize a graph', () => { |
9 | 10 | const g = new Graph<string>().addEdge('a', 'b').addEdge('b', 'c'); |
10 | 11 | const serialized = serializeGraph(g); |
11 | 12 |
|
12 | 13 | const graph = deserializeGraph(serialized); |
13 | 14 | checkSerialized(serializeGraph(graph)); |
14 | 15 | }); |
15 | 16 |
|
16 | | - it('Should deserialize a graph passed to constructor.', function () { |
| 17 | + it('should deserialize a graph passed to constructor', () => { |
17 | 18 | const g = new Graph<string>().addEdge('a', 'b').addEdge('b', 'c'); |
18 | 19 | const serialized = serializeGraph(g); |
19 | 20 | const graph = new Graph(serialized); |
20 | 21 | checkSerialized(serializeGraph(graph)); |
21 | 22 | }); |
22 | 23 |
|
23 | | - it.skip('should return a graph with type inferred from the input', function () { |
24 | | - const nodeA = { title: 'a' }; |
25 | | - const nodeB = { title: 'b' }; |
| 24 | + it('should not duplicate nodes when they are objects', () => { |
| 25 | + const nodeA = { id: 1, title: 'a' }; |
| 26 | + const nodeB = { id: 2, title: 'b' }; |
| 27 | + |
| 28 | + const stringData = JSON.stringify({ |
| 29 | + nodes: [nodeA, nodeB], |
| 30 | + links: [{ source: nodeA, target: nodeB, props: { type: 'foo' } }], |
| 31 | + }); |
| 32 | + |
| 33 | + const serializedGraph: Serialized<{ id: number; title: string }, { type: string }> = |
| 34 | + JSON.parse(stringData); |
| 35 | + |
| 36 | + const graph = deserializeGraph(serializedGraph, (n) => n.id); |
| 37 | + |
| 38 | + expect(graph.nodes).toHaveLength(2); |
| 39 | + }); |
| 40 | + |
| 41 | + it.skip('should return a graph with type inferred from the input', () => { |
| 42 | + const nodeA = { id: 1, title: 'a' }; |
| 43 | + const nodeB = { id: 2, title: 'b' }; |
| 44 | + const serialized = { |
| 45 | + nodes: [nodeA, nodeB], |
| 46 | + links: [{ source: nodeA, target: nodeB, props: { type: 'foo' } }], |
| 47 | + }; |
| 48 | + |
| 49 | + const graph = deserializeGraph(serialized, (n) => n.id); |
| 50 | + expectTypeOf(graph).toEqualTypeOf< |
| 51 | + Graph<{ id: number; title: string }, { type: string }> |
| 52 | + >(); |
| 53 | + }); |
| 54 | + |
| 55 | + it.skip('should require an identity function when nodes are objects', () => { |
| 56 | + const nodeA = { id: 1, title: 'a' }; |
| 57 | + const nodeB = { id: 2, title: 'b' }; |
26 | 58 | const serialized = { |
27 | 59 | nodes: [nodeA, nodeB], |
28 | 60 | links: [{ source: nodeA, target: nodeB, props: { type: 'foo' } }], |
29 | 61 | }; |
30 | 62 |
|
| 63 | + // @ts-expect-error Missing identity function |
31 | 64 | const graph = deserializeGraph(serialized); |
32 | | - expectTypeOf(graph).toEqualTypeOf<Graph<{ title: string }, { type: string }>>(); |
33 | 65 | }); |
34 | 66 | }); |
0 commit comments