Skip to content

Commit 036ac90

Browse files
committed
Graph2.js : Convert live test into Jest test.
1 parent e18718b commit 036ac90

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

Data-Structures/Graph/Graph2.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Graph {
3636
}
3737

3838
// Prints the vertex and adjacency list
39-
printGraph () {
39+
printGraph (output = value => console.log(value)) {
4040
// get all the vertices
4141
const getKeys = this.AdjList.keys()
4242

@@ -54,35 +54,9 @@ class Graph {
5454
}
5555

5656
// print the vertex and its adjacency list
57-
console.log(i + ' -> ' + conc)
57+
output(i + ' -> ' + conc)
5858
}
5959
}
6060
}
61-
// Example
62-
const graph = new Graph(6)
63-
const vertices = ['A', 'B', 'C', 'D', 'E', 'F']
6461

65-
// adding vertices
66-
for (let i = 0; i < vertices.length; i++) {
67-
graph.addVertex(vertices[i])
68-
}
69-
70-
// adding edges
71-
graph.addEdge('A', 'B')
72-
graph.addEdge('A', 'D')
73-
graph.addEdge('A', 'E')
74-
graph.addEdge('B', 'C')
75-
graph.addEdge('D', 'E')
76-
graph.addEdge('E', 'F')
77-
graph.addEdge('E', 'C')
78-
graph.addEdge('C', 'F')
79-
80-
// prints all vertex and
81-
// its adjacency list
82-
// A -> B D E
83-
// B -> A C
84-
// C -> B E F
85-
// D -> A E
86-
// E -> A D F C
87-
// F -> E C
88-
graph.printGraph()
62+
export { Graph }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Graph } from '../Graph2'
2+
3+
describe('Test Graph2', () => {
4+
const vertices = ['A', 'B', 'C', 'D', 'E', 'F']
5+
const graph = new Graph(vertices.length)
6+
7+
// adding vertices
8+
for (let i = 0; i < vertices.length; i++) {
9+
graph.addVertex(vertices[i])
10+
}
11+
12+
// adding edges
13+
graph.addEdge('A', 'B')
14+
graph.addEdge('A', 'D')
15+
graph.addEdge('A', 'E')
16+
graph.addEdge('B', 'C')
17+
graph.addEdge('D', 'E')
18+
graph.addEdge('E', 'F')
19+
graph.addEdge('E', 'C')
20+
graph.addEdge('C', 'F')
21+
22+
it('Check adjacency lists', () => {
23+
const mockFn = jest.fn()
24+
graph.printGraph(mockFn)
25+
26+
// Expect one call per vertex
27+
expect(mockFn.mock.calls.length).toBe(vertices.length)
28+
29+
// Collect adjacency lists from output (call args)
30+
const adjListArr = mockFn.mock.calls.map(v => v[0])
31+
32+
expect(adjListArr).toEqual([
33+
'A -> B D E ',
34+
'B -> A C ',
35+
'C -> B E F ',
36+
'D -> A E ',
37+
'E -> A D F C ',
38+
'F -> E C '
39+
])
40+
})
41+
})

0 commit comments

Comments
 (0)