@@ -2,7 +2,7 @@ const Graph = require('./graph');
22
33describe ( 'Graph' , ( ) => {
44 let graph ;
5- const getValue = node => node . value ;
5+ const getValues = node => ( Array . isArray ( node ) ? node . map ( a => getValues ( a ) ) : node . value ) ;
66
77 beforeEach ( ( ) => {
88 graph = new Graph ( ) ;
@@ -43,18 +43,18 @@ describe('Graph', () => {
4343
4444 it ( 'should add node a as adjacent of b' , ( ) => {
4545 const [ a , b ] = graph . addEdge ( 'a' , 'b' ) ;
46- expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ 'b' ] ) ;
47- expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
46+ expect ( a . adjacents . map ( getValues ) ) . toEqual ( [ 'b' ] ) ;
47+ expect ( b . adjacents . map ( getValues ) ) . toEqual ( [ ] ) ;
4848
4949 graph . addEdge ( 'b' , 'a' ) ;
50- expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ 'a' ] ) ;
50+ expect ( b . adjacents . map ( getValues ) ) . toEqual ( [ 'a' ] ) ;
5151 } ) ;
5252
5353 it ( 'should add both connection on undirected graph' , ( ) => {
5454 graph = new Graph ( Graph . UNDIRECTED ) ;
5555 const [ a , b ] = graph . addEdge ( 'a' , 'b' ) ;
56- expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ 'b' ] ) ;
57- expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ 'a' ] ) ;
56+ expect ( a . adjacents . map ( getValues ) ) . toEqual ( [ 'b' ] ) ;
57+ expect ( b . adjacents . map ( getValues ) ) . toEqual ( [ 'a' ] ) ;
5858 } ) ;
5959
6060 it ( 'should add falsy values' , ( ) => {
@@ -71,23 +71,23 @@ describe('Graph', () => {
7171
7272 it ( 'should remove edges if they exist' , ( ) => {
7373 const [ a , b ] = graph . removeEdge ( 'a' , 'b' ) ;
74- expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
75- expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
74+ expect ( a . adjacents . map ( getValues ) ) . toEqual ( [ ] ) ;
75+ expect ( b . adjacents . map ( getValues ) ) . toEqual ( [ ] ) ;
7676 } ) ;
7777
7878 it ( 'should remove edges with falsy values' , ( ) => {
7979 const [ a , b ] = graph . addEdge ( 0 , false ) ;
80- expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ false ] ) ;
81- expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
80+ expect ( a . adjacents . map ( getValues ) ) . toEqual ( [ false ] ) ;
81+ expect ( b . adjacents . map ( getValues ) ) . toEqual ( [ ] ) ;
8282 graph . removeEdge ( 0 , false ) ;
83- expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
84- expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
83+ expect ( a . adjacents . map ( getValues ) ) . toEqual ( [ ] ) ;
84+ expect ( b . adjacents . map ( getValues ) ) . toEqual ( [ ] ) ;
8585 } ) ;
8686
8787 it ( 'should not create node when removing unexisting target' , ( ) => {
8888 const [ a , c ] = graph . removeEdge ( 'a' , 'c' ) ;
8989 expect ( graph . nodes . size ) . toBe ( 2 ) ;
90- expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ 'b' ] ) ;
90+ expect ( a . adjacents . map ( getValues ) ) . toEqual ( [ 'b' ] ) ;
9191 expect ( c ) . toBe ( undefined ) ;
9292 } ) ;
9393
@@ -143,11 +143,11 @@ describe('Graph', () => {
143143 } ) ;
144144
145145 it ( 'should remove nodes from adjacent list' , ( ) => {
146- expect ( n4 . getAdjacents ( ) . map ( getValue ) ) . toEqual ( [ 1 , 3 ] ) ;
147- expect ( n2 . getAdjacents ( ) . map ( getValue ) ) . toEqual ( [ 1 ] ) ;
146+ expect ( n4 . getAdjacents ( ) . map ( getValues ) ) . toEqual ( [ 1 , 3 ] ) ;
147+ expect ( n2 . getAdjacents ( ) . map ( getValues ) ) . toEqual ( [ 1 ] ) ;
148148 expect ( graph . nodes . has ( n1 . value ) ) . toBe ( true ) ;
149149 graph . removeVertex ( 1 ) ;
150- expect ( n4 . getAdjacents ( ) . map ( getValue ) ) . toEqual ( [ 3 ] ) ;
150+ expect ( n4 . getAdjacents ( ) . map ( getValues ) ) . toEqual ( [ 3 ] ) ;
151151 expect ( graph . nodes . has ( n1 . value ) ) . toBe ( false ) ;
152152 } ) ;
153153 } ) ;
@@ -171,14 +171,14 @@ describe('Graph', () => {
171171
172172 describe ( '#dfs' , ( ) => {
173173 it ( 'should visit nodes using depth-first search' , ( ) => {
174- const visitedOrder = Array . from ( Graph . dfs ( first ) ) . map ( getValue ) ;
174+ const visitedOrder = Array . from ( Graph . dfs ( first ) ) . map ( getValues ) ;
175175 expect ( visitedOrder ) . toEqual ( [ 0 , 1 , 3 , 2 , 4 , 5 ] ) ;
176176 } ) ;
177177 } ) ;
178178
179179 describe ( '#bfs' , ( ) => {
180180 it ( 'should visit nodes using breadth-first search' , ( ) => {
181- const visitedOrder = Array . from ( Graph . bfs ( first ) ) . map ( getValue ) ;
181+ const visitedOrder = Array . from ( Graph . bfs ( first ) ) . map ( getValues ) ;
182182 expect ( visitedOrder ) . toEqual ( [ 0 , 5 , 4 , 1 , 3 , 2 ] ) ;
183183 } ) ;
184184 } ) ;
@@ -203,7 +203,7 @@ describe('Graph', () => {
203203
204204 describe ( '#dfs' , ( ) => {
205205 it ( 'should visit nodes using depth-first search' , ( ) => {
206- const visitedOrder = Array . from ( Graph . dfs ( first ) ) . map ( getValue ) ;
206+ const visitedOrder = Array . from ( Graph . dfs ( first ) ) . map ( getValues ) ;
207207 expect ( visitedOrder ) . toEqual ( [ 1 , 4 , 8 , 3 , 7 , 6 , 10 , 2 , 5 , 9 ] ) ;
208208 } ) ;
209209
@@ -228,7 +228,7 @@ describe('Graph', () => {
228228
229229 describe ( '#bfs' , ( ) => {
230230 it ( 'should visit nodes using breadth-first search' , ( ) => {
231- const visitedOrder = Array . from ( Graph . bfs ( first ) ) . map ( getValue ) ;
231+ const visitedOrder = Array . from ( Graph . bfs ( first ) ) . map ( getValues ) ;
232232 expect ( visitedOrder ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
233233 } ) ;
234234 } ) ;
@@ -241,51 +241,59 @@ describe('Graph', () => {
241241
242242 beforeEach ( ( ) => {
243243 graph = new Graph ( Graph . UNDIRECTED ) ;
244- [ you ] = graph . addEdge ( 'You ' , 'Mary ' ) ;
245- [ mary , barbara ] = graph . addEdge ( 'Mary ' , 'Barbara ' ) ;
244+ [ you ] = graph . addEdge ( 'you ' , 'mary ' ) ;
245+ [ mary , barbara ] = graph . addEdge ( 'mary ' , 'barbara ' ) ;
246246 } ) ;
247247
248248 describe ( '#areConnected' , ( ) => {
249249 it ( 'should return true if two nodes are connected' , ( ) => {
250- expect ( graph . areConnected ( 'You ' , 'Barbara ' ) ) . toBe ( true ) ;
250+ expect ( graph . areConnected ( 'you ' , 'barbara ' ) ) . toBe ( true ) ;
251251 } ) ;
252252
253253 it ( 'should return true if two nodes are connected' , ( ) => {
254- expect ( graph . areConnected ( 'You ' , 'You ' ) ) . toBe ( true ) ;
254+ expect ( graph . areConnected ( 'you ' , 'you ' ) ) . toBe ( true ) ;
255255 } ) ;
256256
257257 it ( 'should return true if two nodes are connected' , ( ) => {
258- expect ( graph . areConnected ( 'You ' , 'John' ) ) . toBe ( false ) ;
258+ expect ( graph . areConnected ( 'you ' , 'John' ) ) . toBe ( false ) ;
259259 } ) ;
260260 } ) ;
261261
262262 describe ( '#findPath' , ( ) => {
263263 it ( 'should handle source === destination' , ( ) => {
264- expect ( graph . findPath ( 'You ' , 'You ' ) ) . toEqual ( [ you ] ) ;
264+ expect ( graph . findPath ( 'you ' , 'you ' ) ) . toEqual ( [ you ] ) ;
265265 } ) ;
266266
267267 it ( 'should get connecting path' , ( ) => {
268- expect ( graph . findPath ( 'You ' , 'Barbara ' ) . map ( getValue ) ) . toEqual ( [ 'You ' , 'Mary ' , 'Barbara ' ] ) ;
268+ expect ( graph . findPath ( 'you ' , 'barbara ' ) . map ( getValues ) ) . toEqual ( [ 'you ' , 'mary ' , 'barbara ' ] ) ;
269269 } ) ;
270270
271271 it ( 'should get adjacent connecting path' , ( ) => {
272- expect ( graph . findPath ( 'Mary ' , 'Barbara ' ) . map ( getValue ) ) . toEqual ( [ 'Mary ' , 'Barbara ' ] ) ;
272+ expect ( graph . findPath ( 'mary ' , 'barbara ' ) . map ( getValues ) ) . toEqual ( [ 'mary ' , 'barbara ' ] ) ;
273273 } ) ;
274274
275275 it ( 'should return empty if there is no connection' , ( ) => {
276- expect ( graph . findPath ( 'You ' , 'Obama' ) . map ( getValue ) ) . toEqual ( [ ] ) ;
276+ expect ( graph . findPath ( 'you ' , 'Obama' ) . map ( getValues ) ) . toEqual ( [ ] ) ;
277277 } ) ;
278278 } ) ;
279279
280- xdescribe ( '#findAllPaths' , ( ) => {
280+ describe ( '#findAllPaths' , ( ) => {
281281 it ( 'should handle source === destination' , ( ) => {
282- expect ( graph . findAllPaths ( 'You' , 'You' ) ) . toEqual ( [ [ you ] ] ) ;
282+ expect ( graph . findAllPaths ( 'you' , 'you' ) ) . toEqual ( [ [ you ] ] ) ;
283+ expect ( getValues ( graph . findAllPaths ( 'you' , 'you' ) ) ) . toEqual ( [ [ 'you' ] ] ) ;
284+ } ) ;
285+
286+ it ( 'should find all paths when only one' , ( ) => {
287+ expect ( getValues ( graph . findAllPaths ( 'mary' , 'barbara' ) ) ) . toEqual ( [
288+ [ 'mary' , 'barbara' ] ,
289+ ] ) ;
283290 } ) ;
284291
285292 it ( 'should find all paths' , ( ) => {
286- expect ( graph . findAllPaths ( 'Mary' , 'Barbara' ) ) . toEqual ( [
287- [ 'Mary' , 'You' , 'Barbara' ] ,
288- [ 'Mary' , 'Barbara' ] ,
293+ graph . addEdge ( 'you' , 'barbara' ) ;
294+ expect ( getValues ( graph . findAllPaths ( 'you' , 'barbara' ) ) ) . toEqual ( [
295+ [ 'you' , 'mary' , 'barbara' ] ,
296+ [ 'you' , 'barbara' ] ,
289297 ] ) ;
290298 } ) ;
291299 } ) ;
0 commit comments