@@ -35,7 +35,7 @@ describe('basic graph functionality', () => {
35
35
test ( 'add vertex method exists' , ( ) => {
36
36
expect ( typeof graph . addVertex ) . toBe ( 'function' ) ;
37
37
} ) ;
38
- test ( ' can add vertex and it will appear as a key value pair in the adjacency list, with edges value initialized as empty array' , ( ) => {
38
+ test ( ` can add vertex and it will appear as a key value pair in the adjacency list, with edges value initialized as empty array` , ( ) => {
39
39
graph . addVertex ( 'Alice' ) ;
40
40
expect ( graph . adjacencyList [ 'Alice' ] ) . toEqual ( [ ] ) ;
41
41
} ) ;
@@ -55,7 +55,7 @@ describe('basic graph functionality', () => {
55
55
expect ( graph . addEdge ( 'Alice' , 'Cheshire Cat' ) ) . toBeNull ( ) ; // Cheshire cat still invisible
56
56
graph . addVertex ( 'Cheshire Cat' ) ; // appears!
57
57
expect ( typeof graph . addEdge ( 'Alice' , 'Cheshire Cat' ) ) . toBe ( 'object' ) ;
58
- } )
58
+ } ) ;
59
59
test ( 'adding a vertex using existing key name will return the existing list of edges' , ( ) => {
60
60
graph . addVertex ( 'Alice' ) ;
61
61
graph . addVertex ( 'The Duchess' ) ;
@@ -81,6 +81,97 @@ describe('basic graph functionality', () => {
81
81
expect ( graph . adjacencyList [ 'March Hare' ] ) . toEqual ( [ 'Mad Hatter' , 'Dormouse' ] ) ;
82
82
expect ( graph . adjacencyList [ 'Dormouse' ] ) . toEqual ( [ 'Mad Hatter' , 'March Hare' ] )
83
83
} ) ;
84
+ test ( 'can remove multiple vertices by key[] with removeVertices method' , ( ) => {
85
+ populateGraph ( graph ) ;
86
+ graph . removeVertices ( [ 'Dormouse' , 'Mad Hatter' , 'March Hare' ] ) ;
87
+ expect ( graph . adjacencyList ) . toEqual ( {
88
+ "Alice" : [ "Cheshire Cat" ] ,
89
+ "Cheshire Cat" : [ "Alice" ] , "Time" : [ ]
90
+ } ) ;
91
+ expect ( graph . vertices ) . toEqual ( {
92
+ "Alice" : { "key" : "Alice" , "value" : null } ,
93
+ "Cheshire Cat" : { "key" : "Cheshire Cat" , "value" : null } ,
94
+ "Time" : { "key" : "Time" , "value" : null }
95
+ } ) ;
96
+ } ) ;
97
+ test ( 'removing a non-existent key from graph throws reference error' , ( ) => {
98
+ populateGraph ( graph ) ;
99
+ try {
100
+ graph . removeVertex ( 'White Rabbit' ) ;
101
+ } catch ( e ) {
102
+ expect ( typeof e ) . toEqual ( 'object' ) ;
103
+ expect ( e . message ) . toBe ( "Key: White Rabbit does not exist as a vertex." ) ;
104
+ }
105
+ try {
106
+ graph . removeVertices ( [ 'White Rabbit' , 'Queen of Hearts' ] ) ;
107
+ } catch ( e ) {
108
+ expect ( typeof e ) . toEqual ( 'object' ) ;
109
+ expect ( e . message ) . toBe ( "Key: White Rabbit does not exist as a vertex." ) ;
110
+ }
111
+ } )
112
+ } ) ;
113
+
114
+ describe ( `can pass in array values to graph's crud methods` , ( ) => {
115
+ test ( 'can initiate a graph with values by passing string[] to constructor' , ( ) => {
116
+ graph = new Graph ( { direction : 'bi' } , [ 'Alice' , 'The Caterpillar' , 'The Cheshire Cat' ] ) ;
117
+ expect ( graph . adjacencyList ) . toEqual ( { "Alice" : [ ] , "The Caterpillar" : [ ] , "The Cheshire Cat" : [ ] } ) ;
118
+ expect ( graph . vertices ) . toEqual ( {
119
+ "Alice" : { "key" : "Alice" , "value" : null } ,
120
+ "The Caterpillar" : { "key" : "The Caterpillar" , "value" : null } ,
121
+ "The Cheshire Cat" : { "key" : "The Cheshire Cat" , "value" : null }
122
+ } ) ;
123
+ } ) ;
124
+ test ( 'can initiate a graph with values by passing vertex[] to constructor' , ( ) => {
125
+ graph = new Graph ( { direction : 'bi' } , [ { key : 'Alice' , value : "Curiouser and curiouser!" } ,
126
+ { key : 'The Eaglet' , value : "Speak English!" } ,
127
+ { key : 'The Dodo' , value : "The best way to explain it is to do it." } ] ) ;
128
+ expect ( graph . adjacencyList ) . toEqual ( { "Alice" : [ ] , "The Dodo" : [ ] , "The Eaglet" : [ ] } ) ;
129
+ expect ( graph . vertices ) . toEqual ( {
130
+ "Alice" : { "key" : "Alice" , "value" : "Curiouser and curiouser!" } ,
131
+ "The Dodo" : { "key" : "The Dodo" , "value" : "The best way to explain it is to do it." } ,
132
+ "The Eaglet" : { "key" : "The Eaglet" , "value" : "Speak English!" }
133
+ } ) ;
134
+ } ) ;
135
+ test ( 'addVertices method exists' , ( ) => {
136
+ expect ( typeof graph . addVertices ) . toBe ( 'function' ) ;
137
+ } ) ;
138
+ test ( 'can pass string array to addVertices and get proper result' , ( ) => {
139
+ graph . addVertices ( [ 'Alice' , 'The Duck' , 'The Lory' , 'The Eaglet' ] ) ;
140
+ expect ( graph . adjacencyList ) . toEqual ( {
141
+ "Alice" : [ ] , "The Duck" : [ ] ,
142
+ "The Eaglet" : [ ] , "The Lory" : [ ]
143
+ } ) ;
144
+ expect ( graph . vertices ) . toEqual ( {
145
+ "Alice" : { "key" : "Alice" , "value" : null } ,
146
+ "The Duck" : { "key" : "The Duck" , "value" : null } ,
147
+ "The Eaglet" : { "key" : "The Eaglet" , "value" : null } ,
148
+ "The Lory" : { "key" : "The Lory" , "value" : null }
149
+ } ) ;
150
+ } ) ;
151
+
152
+ test ( 'addEdges method exists' , ( ) => {
153
+ expect ( typeof graph . addEdges ) . toBe ( 'function' ) ;
154
+ } ) ;
155
+ test ( 'bidirectional graph: can pass string array to addEdges, which it will interpret as a tuple: [fromKey, toKey]' , ( ) => {
156
+ graph = new Graph ( { direction : 'bi' } , [ { key : 'Alice' , value : "Curiouser and curiouser!" } ,
157
+ { key : 'The Eaglet' , value : "Speak English!" } ,
158
+ { key : 'The Dodo' , value : "The best way to explain it is to do it." } ] ) ;
159
+ graph . addEdges ( [ [ 'Alice' , 'The Eaglet' ] , [ 'Alice' , 'The Dodo' ] , [ 'The Eaglet' , 'The Dodo' ] ] ) ;
160
+ expect ( graph . adjacencyList ) . toEqual ( {
161
+ "Alice" : [ "The Eaglet" , "The Dodo" ] ,
162
+ "The Dodo" : [ "Alice" , "The Eaglet" ] , "The Eaglet" : [ "Alice" , "The Dodo" ]
163
+ } ) ;
164
+ } ) ;
165
+ test ( 'monodirectional graph: can pass string array to addEdges, which it will interpret as a tuple: [fromKey, toKey]' , ( ) => {
166
+ graph = new Graph ( { direction : 'mono' } , [ { key : 'Alice' , value : "Curiouser and curiouser!" } ,
167
+ { key : 'The Eaglet' , value : "Speak English!" } ,
168
+ { key : 'The Dodo' , value : "The best way to explain it is to do it." } ] ) ;
169
+ graph . addEdges ( [ [ 'Alice' , 'The Eaglet' ] , [ 'Alice' , 'The Dodo' ] , [ 'The Eaglet' , 'The Dodo' ] ] ) ;
170
+ expect ( graph . adjacencyList ) . toEqual ( {
171
+ "Alice" : [ "The Eaglet" , "The Dodo" ] ,
172
+ "The Dodo" : [ ] , "The Eaglet" : [ "The Dodo" ]
173
+ } ) ;
174
+ } )
84
175
} ) ;
85
176
86
177
describe ( 'depth first traversal of graphs' , ( ) => {
0 commit comments