@@ -252,4 +252,115 @@ describe('TreeStateModifiers', () => {
252
252
} ) ;
253
253
} ) ;
254
254
} ) ;
255
+
256
+ describe ( 'deleteNodeAt' , ( ) => {
257
+ test ( 'should fail when invalid state is supplied' , ( ) => {
258
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( 'state' , 0 ) ) . toThrowError (
259
+ 'Expected a State instance but got string' ,
260
+ ) ;
261
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( 1225 , 0 ) ) . toThrowError ( 'Expected a State instance but got number' ) ;
262
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( [ ] , 0 ) ) . toThrowError ( 'Expected a State instance but got object' ) ;
263
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( { } , 0 ) ) . toThrowError ( 'Expected a State instance but got object' ) ;
264
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( true , 0 ) ) . toThrowError ( 'Expected a State instance but got boolean' ) ;
265
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( ( ) => { } , 0 ) ) . toThrowError (
266
+ 'Expected a State instance but got function' ,
267
+ ) ;
268
+ } ) ;
269
+
270
+ test ( 'should fail with descriptive error when node at index does not exist' , ( ) => {
271
+ expect ( ( ) => TreeStateModifiers . deleteNodeAt ( TreeState . createFromTree ( Nodes ) , 20 ) ) . toThrowErrorMatchingSnapshot ( ) ;
272
+ } ) ;
273
+
274
+ describe ( 'flattened tree' , ( ) => {
275
+ test ( 'should delete a root node with expanded children' , ( ) => {
276
+ const state = TreeState . createFromTree ( Nodes ) ;
277
+
278
+ deepFreeze ( state ) ;
279
+
280
+ const { flattenedTree} = TreeStateModifiers . deleteNodeAt ( state , 0 ) ;
281
+
282
+ expect ( flattenedTree ) . toMatchSnapshot ( ) ;
283
+ } ) ;
284
+
285
+ test ( 'should delete a root node without expanded children' , ( ) => {
286
+ const state = TreeState . createFromTree ( Nodes ) ;
287
+
288
+ deepFreeze ( state ) ;
289
+
290
+ const { flattenedTree} = TreeStateModifiers . deleteNodeAt ( state , 5 ) ;
291
+
292
+ expect ( flattenedTree ) . toMatchSnapshot ( ) ;
293
+ } ) ;
294
+
295
+ test ( 'should delete a child node with expanded children' , ( ) => {
296
+ const state = TreeState . createFromTree ( Nodes ) ;
297
+
298
+ deepFreeze ( state ) ;
299
+
300
+ const { flattenedTree} = TreeStateModifiers . deleteNodeAt ( state , 1 ) ;
301
+
302
+ expect ( flattenedTree ) . toMatchSnapshot ( ) ;
303
+ } ) ;
304
+
305
+ test ( 'should delete a child node without expanded children' , ( ) => {
306
+ const state = TreeState . createFromTree ( Nodes ) ;
307
+
308
+ deepFreeze ( state ) ;
309
+
310
+ const { flattenedTree} = TreeStateModifiers . deleteNodeAt ( state , 2 ) ;
311
+
312
+ expect ( flattenedTree ) . toMatchSnapshot ( ) ;
313
+ } ) ;
314
+ } ) ;
315
+
316
+ describe ( 'tree' , ( ) => {
317
+ test ( 'should delete a root node with expanded children' , ( ) => {
318
+ const state = TreeState . createFromTree ( Nodes ) ;
319
+
320
+ deepFreeze ( state ) ;
321
+
322
+ const { tree} = TreeStateModifiers . deleteNodeAt ( state , 0 ) ;
323
+
324
+ const changes = diff ( state . tree , tree ) ;
325
+
326
+ expect ( changes ) . toMatchSnapshot ( ) ;
327
+ } ) ;
328
+
329
+ test ( 'should delete a root node without expanded children' , ( ) => {
330
+ const state = TreeState . createFromTree ( Nodes ) ;
331
+
332
+ deepFreeze ( state ) ;
333
+
334
+ const { tree} = TreeStateModifiers . deleteNodeAt ( state , 6 ) ;
335
+
336
+ const changes = diff ( state . tree , tree ) ;
337
+
338
+ expect ( changes ) . toMatchSnapshot ( ) ;
339
+ } ) ;
340
+
341
+ test ( 'should delete a child node without expanded children' , ( ) => {
342
+ const state = TreeState . createFromTree ( Nodes ) ;
343
+
344
+ deepFreeze ( state ) ;
345
+
346
+ const { tree} = TreeStateModifiers . deleteNodeAt ( state , 2 ) ;
347
+
348
+ const changes = diff ( state . tree , tree ) ;
349
+
350
+ expect ( changes ) . toMatchSnapshot ( ) ;
351
+ } ) ;
352
+
353
+ test ( 'should delete a child node with expanded children' , ( ) => {
354
+ const state = TreeState . createFromTree ( Nodes ) ;
355
+
356
+ deepFreeze ( state ) ;
357
+
358
+ const { tree} = TreeStateModifiers . deleteNodeAt ( state , 1 ) ;
359
+
360
+ const changes = diff ( state . tree , tree ) ;
361
+
362
+ expect ( changes ) . toMatchSnapshot ( ) ;
363
+ } ) ;
364
+ } ) ;
365
+ } ) ;
255
366
} ) ;
0 commit comments