Skip to content

Commit 677e6ce

Browse files
committed
test: increases DirectedGraph coverage to 100%
1 parent 427adfb commit 677e6ce

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/data-structures/directed-graph/src/directed-graph.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class DirectedGraph {
6161
console.log(printedMessage)
6262
}
6363

64-
breadthFirstSearch(startingNode, searchingForNode, callback) {
64+
breadthFirstSearch(startingNode, searchingForNode, callback, showLogs) {
6565
const visitedNodes = {}
6666
const nodesToVisitQueue = []
6767

@@ -79,20 +79,20 @@ export class DirectedGraph {
7979
}
8080

8181
const currentNodeNeighbors = this.nodes.get(currentNode)
82-
console.log(
83-
'currentNode:',
84-
currentNode,
85-
'currentNodeNeighbors:',
86-
currentNodeNeighbors.join(', ')
87-
)
82+
83+
/* istanbul ignore next */
84+
showLogs &&
85+
console.log(
86+
'currentNode:',
87+
currentNode,
88+
'currentNodeNeighbors:',
89+
currentNodeNeighbors.join(', ')
90+
)
8891

8992
currentNodeNeighbors.forEach(neighborNode => {
9093
nodesToVisitQueue.push(neighborNode)
9194
})
9295
}
9396
}
9497
}
95-
96-
// eslint-disable-next-line no-unused-vars
97-
depthFirstSearch(startingNode, searchingForNode, callback) {}
9898
}

src/data-structures/directed-graph/src/directed-graph.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ describe('DirectedGraph', () => {
8686
'Node does not exist in the graph.'
8787
)
8888
})
89+
90+
it('returns an array of node neghbors for an existing node with some neighbors', () => {
91+
const graph = new DirectedGraph()
92+
graph.addNode('A')
93+
graph.addNode('B')
94+
graph.addNode('C')
95+
96+
graph.addEdge('A', 'B')
97+
graph.addEdge('A', 'C')
98+
99+
expect(graph.getNodeNeighbors('A')).toEqual(['B', 'C'])
100+
})
101+
102+
it('returns an empty array of node neghbors for an existing node that does not have any neighbors', () => {
103+
const graph = new DirectedGraph()
104+
graph.addNode('A')
105+
graph.addNode('B')
106+
graph.addNode('C')
107+
108+
graph.addEdge('A', 'B')
109+
graph.addEdge('A', 'C')
110+
111+
expect(graph.getNodeNeighbors('C')).toEqual([])
112+
})
89113
})
90114

91115
describe('printGraph', () => {

0 commit comments

Comments
 (0)