1+ /* globals: beforeEach, describe, it, module, inject, expect */
2+ describe ( 'RecursionHelper' , function ( ) {
3+ var $compile , parent , scope , link ;
4+
5+ angular . module ( 'Tree' , [ ] ) . directive ( "tree" , function ( RecursionHelper ) {
6+ return {
7+ restrict : "E" ,
8+ scope : {
9+ family : '='
10+ } ,
11+ replace : true ,
12+ template : '' +
13+ '<div class="tree">' +
14+ ' <p>{{ family.name }}</p>' +
15+ ' <ul>' +
16+ ' <li ng-repeat="child in family.children">' +
17+ ' <tree family="child"></tree>' +
18+ ' </li>' +
19+ ' </ul>' +
20+ '</div>' ,
21+ compile : function ( element ) {
22+ return RecursionHelper . compile ( element , link ) ;
23+ }
24+ } ;
25+ } ) ;
26+
27+ beforeEach ( module ( 'RecursionHelper' , 'Tree' ) ) ;
28+ beforeEach ( inject ( function ( _$compile_ , $rootScope ) {
29+ $compile = _$compile_ ;
30+ scope = $rootScope . $new ( ) ;
31+ scope . treeFamily = {
32+ name : "Parent" ,
33+ children : [
34+ {
35+ name : "Child1" ,
36+ children : [
37+ {
38+ name : "Grandchild1" ,
39+ children : [ ]
40+ } ,
41+ {
42+ name : "Grandchild2" ,
43+ children : [ ]
44+ } ,
45+ {
46+ name : "Grandchild3" ,
47+ children : [ ]
48+ }
49+ ]
50+ } ,
51+ {
52+ name : "Child2" ,
53+ children : [ ]
54+ }
55+ ]
56+ } ;
57+ } ) ) ;
58+
59+ function compileTree ( ) {
60+ parent = $compile ( '<tree family="treeFamily"></tree>' ) ( scope ) ;
61+ scope . $apply ( ) ;
62+ }
63+
64+
65+ it ( 'should render the whole tree' , function ( ) {
66+ compileTree ( ) ;
67+ var children = parent . children ( 'ul' ) . children ( ) ;
68+ var grandChildren = children . find ( ':first-child' ) . children ( 'ul' ) . children ( ) ;
69+ expect ( children . length ) . toBe ( 2 ) ;
70+ expect ( grandChildren . length ) . toBe ( 3 ) ;
71+ } ) ;
72+
73+ it ( 'should call the pre and post linking functions, when passed as object in the link parameter' , function ( ) {
74+ link = {
75+ pre : jasmine . createSpy ( 'pre' ) ,
76+ post : jasmine . createSpy ( 'post' )
77+ } ;
78+
79+ compileTree ( ) ;
80+ expect ( link . pre ) . toHaveBeenCalled ( ) ;
81+ expect ( link . post ) . toHaveBeenCalled ( ) ;
82+
83+ } ) ;
84+
85+ it ( 'should call the post linking function, when passed as function in the link parameter' , function ( ) {
86+ link = jasmine . createSpy ( 'post' ) ;
87+
88+ compileTree ( ) ;
89+ expect ( link ) . toHaveBeenCalled ( ) ;
90+ } ) ;
91+ } ) ;
0 commit comments