File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed
Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change 11'use strict' ;
2+ var util = require ( 'util' ) ;
23
34function createTree ( list , rootNodes ) {
45 var tree = [ ] ;
@@ -33,6 +34,9 @@ function orderByParents(list) {
3334}
3435
3536module . exports = function ( obj ) {
37+ if ( ! util . isArray ( obj ) ) {
38+ throw new Error ( 'Expected an object but got an invalid argument' ) ;
39+ }
3640 var cloned = obj . slice ( ) ;
3741 var ordered = orderByParents ( cloned ) ;
3842 return createTree ( ordered , ordered [ 0 ] ) ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ var current;
99describe ( 'parent pointer array to tree' , function ( ) {
1010 describe ( 'expected behavior' , function ( ) {
1111
12- beforeEach ( function ( ) {
12+ before ( function ( ) {
1313 current = toTree ( initial ) ;
1414 } ) ;
1515
@@ -40,5 +40,17 @@ describe('parent pointer array to tree', function() {
4040 expect ( current ) . to . be . deep . equal ( expected ) ;
4141 } ) ;
4242
43+ } ) ;
44+
45+ describe ( 'with incorrect arguments' , function ( ) {
46+ it ( 'should return an empty array if the empty array passed' , function ( ) {
47+ expect ( toTree ( [ ] ) ) . to . be . deep . equal ( [ ] ) ;
48+ } ) ;
49+
50+ it ( 'should throw an error if wrong arguments passed' , function ( ) {
51+ expect ( toTree . bind ( null , 'string' ) ) . to . throw ( / i n v a l i d a r g u m e n t / ) ;
52+ expect ( toTree . bind ( null , { } ) ) . to . throw ( / i n v a l i d a r g u m e n t / ) ;
53+ } ) ;
54+
4355 } )
4456} ) ;
You can’t perform that action at this time.
0 commit comments