Graph2.js : Convert live test into Jest test.

This commit is contained in:
Eric Lavault
2021-10-11 14:23:24 +02:00
parent e18718b7d5
commit 036ac907ae
2 changed files with 44 additions and 29 deletions

View File

@ -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()
@ -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 }

View File

@ -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 '
])
})
})