1+ import Tree from '../tree' ;
2+ import { networkInterfaces } from 'os' ;
3+
4+ describe ( 'Tree unit test' , ( ) => {
5+ describe ( 'Constructor' , ( ) => {
6+ let newTree = new Tree ( { } ) ;
7+
8+ it ( 'should be able to create a newTree' , ( ) => {
9+ expect ( newTree . state ) . toEqual ( { } ) ;
10+ } )
11+
12+ it ( 'should have 5 properties' , ( ) => {
13+ expect ( newTree ) . toHaveProperty ( 'state' ) ;
14+ expect ( newTree ) . toHaveProperty ( 'name' ) ;
15+ expect ( newTree ) . toHaveProperty ( 'componentData' ) ;
16+ expect ( newTree ) . toHaveProperty ( 'children' ) ;
17+ expect ( newTree ) . toHaveProperty ( 'parent' ) ;
18+ } )
19+
20+ it ( 'has name default value as stateless' , ( ) => {
21+ expect ( newTree . name ) . toBe ( 'nameless' ) ;
22+ } )
23+
24+ it ( 'has children as an empty array' , ( ) => {
25+ expect ( newTree . children ) . toEqual ( [ ] ) ;
26+ } )
27+ } )
28+
29+
30+ describe ( 'Adding children' , ( ) => {
31+ let newTree = new Tree ( { } ) ;
32+ let returnChild = newTree . addChild ( 'stateful' , 'child' , { } ) ;
33+
34+ it ( 'should be able to add a child' , ( ) => {
35+ expect ( typeof newTree . children ) . toEqual ( 'object' ) ;
36+ expect ( Array . isArray ( newTree . children ) ) . toBeTruthy ;
37+ } )
38+
39+ it ( `its parent should be newTree` , ( ) => {
40+ expect ( returnChild . parent ) . toEqual ( newTree ) ;
41+ } )
42+
43+ it ( 'parent now contains an array of children and each children is a valid tree' , ( ) => {
44+ expect ( newTree . children [ 0 ] ) . toHaveProperty ( 'state' ) ;
45+ expect ( newTree . children [ 0 ] ) . toHaveProperty ( 'name' ) ;
46+ expect ( newTree . children [ 0 ] ) . toHaveProperty ( 'componentData' ) ;
47+ } )
48+ } )
49+
50+ describe ( 'Adding sibling' , ( ) => {
51+ let newTree = new Tree ( { } ) ;
52+ let returnChild = newTree . addChild ( 'stateful' , 'child' , { } ) ;
53+ let returnSibling = returnChild . addSibling ( 'stateful' , 'child' , { } ) ;
54+
55+ it ( 'the tree now has 2 children' , ( ) => {
56+ expect ( newTree . children . length ) . toBe ( 2 ) ;
57+ } )
58+
59+ it ( 'both of the children has the parent as this tree' , ( ) => {
60+ expect ( newTree . children [ 0 ] ) . toEqual ( returnChild ) ;
61+ expect ( newTree . children [ 1 ] ) . toEqual ( returnSibling ) ;
62+ } )
63+
64+ it ( 'both of the children has the parent as this tree' , ( ) => {
65+ expect ( returnChild . parent ) . toEqual ( newTree ) ;
66+ expect ( returnSibling . parent ) . toEqual ( newTree ) ;
67+ } )
68+ } )
69+
70+
71+ describe ( 'Adding sibling' , ( ) => {
72+ let newTree = new Tree ( { } ) ;
73+ let returnChild = newTree . addChild ( 'stateful' , 'child' , { } ) ;
74+ let returnSibling = returnChild . addSibling ( 'stateful' , 'child' , { } ) ;
75+ it ( 'the tree now has 2 children' , ( ) => {
76+ expect ( newTree . children . length ) . toBe ( 2 ) ;
77+ } )
78+
79+ it ( 'both of the children has the parent as this tree' , ( ) => {
80+ expect ( newTree . children [ 0 ] ) . toEqual ( returnChild ) ;
81+ expect ( newTree . children [ 1 ] ) . toEqual ( returnSibling ) ;
82+ } )
83+
84+ it ( 'both of the children has the parent as this tree' , ( ) => {
85+ expect ( returnChild . parent ) . toEqual ( newTree ) ;
86+ expect ( returnSibling . parent ) . toEqual ( newTree ) ;
87+ } )
88+ } )
89+
90+
91+ describe ( 'Copy & clean tree' , ( ) => {
92+ let newTree = new Tree ( { } ) ;
93+ let returnChild = newTree . addChild ( 'stateful' , 'child' , { } ) ;
94+ let returnSibling = returnChild . addSibling ( 'stateful' , 'child' , { } ) ;
95+ let copy = newTree . cleanTreeCopy ( ) ;
96+ it ( 'its copy has 2 children' , ( ) => {
97+ expect ( copy . children . length ) . toEqual ( 2 ) ;
98+ } )
99+ } )
100+ } )
0 commit comments