@@ -101,13 +101,15 @@ describe("subgraph()", () => {
101101} ) ;
102102
103103describe ( "add()" , ( ) => {
104- test ( "logs an error when a node with the same key already exists in the object graph" , ( ) => {
105- const consoleErrorSpy = vi . spyOn ( console , "error" ) ;
104+ test ( "throws an error when a node with the same key already exists in the object graph" , ( ) => {
105+ const shirtToAdd : Shirt = { sku : "1" , color : "purple" , size : "large" } ;
106106 const shirtsGraph = new ObjectGraph < Shirt > ( shirtsMock , ( shirt ) => shirt . sku ) ;
107107
108- shirtsGraph . add ( shirtsMock [ 0 ] ) ;
109-
110- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
108+ expect ( shirtsGraph . get ( "1" ) ?. color ) . toBe ( "red" ) ;
109+ expect ( ( ) => {
110+ shirtsGraph . add ( shirtToAdd ) ;
111+ } ) . toThrowError ( ) ;
112+ expect ( shirtsGraph . get ( "1" ) ?. color ) . toBe ( "red" ) ;
111113 } ) ;
112114
113115 test ( "adds a node to the object graph" , ( ) => {
@@ -122,33 +124,35 @@ describe("add()", () => {
122124describe ( "toAdded()" , ( ) => {
123125 test ( "gets a copy of the original object graph with a received node added" , ( ) => {
124126 const consoleErrorSpy = vi . spyOn ( console , "error" ) ;
125- const shirtsGraph = new ObjectGraph < Shirt > ( [ ] , ( shirt ) => shirt . sku ) ;
127+ const shirtsGraph = new ObjectGraph < Shirt > ( shirtsMock , ( shirt ) => shirt . sku ) ;
126128
127- const copiedShirtsGraph = shirtsGraph . toAdded ( shirtsMock [ 0 ] ) ;
129+ const copiedShirtsGraph = shirtsGraph . toAdded ( extraShirtsMock [ 0 ] ) ;
128130
129- expect ( shirtsGraph . size ) . toBe ( 0 ) ;
130- expect ( copiedShirtsGraph . size ) . toBe ( 1 ) ;
131+ expect ( shirtsGraph . size ) . toBe ( 8 ) ;
132+ expect ( copiedShirtsGraph . size ) . toBe ( 9 ) ;
131133
132- const returnedNodeFromCopy = copiedShirtsGraph . get ( "1 " ) ;
134+ const returnedNodeFromCopy = copiedShirtsGraph . get ( "9 " ) ;
133135
134136 expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( ) ;
135137 expect ( returnedNodeFromCopy ) . toBeDefined ( ) ;
136138
137- const returnedNodeFromOriginal = shirtsGraph . get ( "1 " ) ;
139+ const returnedNodeFromOriginal = shirtsGraph . get ( "9 " ) ;
138140
139141 expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
140142 expect ( returnedNodeFromOriginal ) . toBeUndefined ( ) ;
141143 } ) ;
142144} ) ;
143145
144146describe ( "update()" , ( ) => {
145- test ( "logs an error when there is no node with the same key in the object graph" , ( ) => {
146- const consoleErrorSpy = vi . spyOn ( console , "error" ) ;
147+ test ( "throws an error when there is no node with the same key in the object graph" , ( ) => {
148+ const shirtToUpdate : Shirt = { sku : "9" , color : "orange" , size : "small" } ;
147149 const shirtsGraph = new ObjectGraph < Shirt > ( shirtsMock , ( shirt ) => shirt . sku ) ;
148150
149- shirtsGraph . update ( extraShirtsMock [ 0 ] ) ;
150-
151- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
151+ expect ( shirtsGraph . size ) . toBe ( 8 ) ;
152+ expect ( ( ) => {
153+ shirtsGraph . update ( shirtToUpdate ) ;
154+ } ) . toThrowError ( ) ;
155+ expect ( shirtsGraph . size ) . toBe ( 8 ) ;
152156 } ) ;
153157
154158 test ( "updates a node in the object graph" , ( ) => {
@@ -176,33 +180,14 @@ describe("toUpdated()", () => {
176180} ) ;
177181
178182describe ( "remove()" , ( ) => {
179- test ( "logs an error when there is no node with the provided key in the object graph" , ( ) => {
180- const consoleErrorSpy = vi . spyOn ( console , "error" ) ;
181- const shirtsGraph = new ObjectGraph < Shirt > ( shirtsMock , ( shirt ) => shirt . sku ) ;
182-
183- const returnedNode = shirtsGraph . remove ( "9" ) ;
184-
185- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
186- expect ( returnedNode ) . toBeUndefined ( ) ;
187- } ) ;
188-
189183 test ( "removes a node from the object graph" , ( ) => {
190- const consoleErrorSpy = vi . spyOn ( console , "error" ) ;
191184 const shirtsGraph = new ObjectGraph < Shirt > ( shirtsMock , ( shirt ) => shirt . sku ) ;
192185
193- const returnedNodeFromFirstAttempt = shirtsGraph . get ( "1" ) ;
194-
195- expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( ) ;
196- expect ( returnedNodeFromFirstAttempt ) . toBeDefined ( ) ;
186+ expect ( shirtsGraph . get ( "1" ) ) . toBeDefined ( ) ;
197187
198188 shirtsGraph . remove ( "1" ) ;
199189
200- expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( ) ;
201-
202- const returnedNodeFromSecondAttempt = shirtsGraph . get ( "1" ) ;
203-
204- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
205- expect ( returnedNodeFromSecondAttempt ) . toBeUndefined ( ) ;
190+ expect ( shirtsGraph . get ( "1" ) ) . toBeUndefined ( ) ;
206191 } ) ;
207192} ) ;
208193
@@ -223,8 +208,8 @@ describe("toRemoved()", () => {
223208
224209 const returnedNodeFromCopy = copiedShirtsGraph . get ( "1" ) ;
225210
226- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
227211 expect ( returnedNodeFromCopy ) . toBeUndefined ( ) ;
212+ expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
228213 } ) ;
229214} ) ;
230215
0 commit comments