Skip to content

Commit

Permalink
Graph2.js : Convert live test into Jest test.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlte committed Oct 11, 2021
1 parent e18718b commit 036ac90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
32 changes: 3 additions & 29 deletions Data-Structures/Graph/Graph2.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Graph {
}

// Prints the vertex and adjacency list
printGraph () {
printGraph (output = value => console.log(value)) {
// get all the vertices
const getKeys = this.AdjList.keys()

Expand All @@ -54,35 +54,9 @@ class Graph {
}

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

// adding vertices
for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i])
}

// adding edges
graph.addEdge('A', 'B')
graph.addEdge('A', 'D')
graph.addEdge('A', 'E')
graph.addEdge('B', 'C')
graph.addEdge('D', 'E')
graph.addEdge('E', 'F')
graph.addEdge('E', 'C')
graph.addEdge('C', 'F')

// prints all vertex and
// its adjacency list
// A -> B D E
// B -> A C
// C -> B E F
// D -> A E
// E -> A D F C
// F -> E C
graph.printGraph()
export { Graph }
41 changes: 41 additions & 0 deletions Data-Structures/Graph/test/Graph2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Graph } from '../Graph2'

describe('Test Graph2', () => {
const vertices = ['A', 'B', 'C', 'D', 'E', 'F']
const graph = new Graph(vertices.length)

// adding vertices
for (let i = 0; i < vertices.length; i++) {
graph.addVertex(vertices[i])
}

// adding edges
graph.addEdge('A', 'B')
graph.addEdge('A', 'D')
graph.addEdge('A', 'E')
graph.addEdge('B', 'C')
graph.addEdge('D', 'E')
graph.addEdge('E', 'F')
graph.addEdge('E', 'C')
graph.addEdge('C', 'F')

it('Check adjacency lists', () => {
const mockFn = jest.fn()
graph.printGraph(mockFn)

// Expect one call per vertex
expect(mockFn.mock.calls.length).toBe(vertices.length)

// Collect adjacency lists from output (call args)
const adjListArr = mockFn.mock.calls.map(v => v[0])

expect(adjListArr).toEqual([
'A -> B D E ',
'B -> A C ',
'C -> B E F ',
'D -> A E ',
'E -> A D F C ',
'F -> E C '
])
})
})

0 comments on commit 036ac90

Please sign in to comment.