@@ -56,27 +56,37 @@ describe("widget", function() {
56
56
57
57
58
58
describe ( 'ng:include' , inject ( function ( $rootScope , $compile ) {
59
- it ( 'should include on external file' , inject ( function ( $rootScope , $compile , $cacheFactory ) {
59
+
60
+ function putIntoCache ( url , content ) {
61
+ return function ( $templateCache ) {
62
+ $templateCache . put ( url , [ 200 , content , { } ] ) ;
63
+ } ;
64
+ }
65
+
66
+
67
+ it ( 'should include on external file' , inject ( putIntoCache ( 'myUrl' , '{{name}}' ) ,
68
+ function ( $rootScope , $compile , $browser ) {
60
69
var element = jqLite ( '<ng:include src="url" scope="childScope"></ng:include>' ) ;
61
70
var element = $compile ( element ) ( $rootScope ) ;
62
71
$rootScope . childScope = $rootScope . $new ( ) ;
63
72
$rootScope . childScope . name = 'misko' ;
64
73
$rootScope . url = 'myUrl' ;
65
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl' , '{{name}}' ) ;
66
74
$rootScope . $digest ( ) ;
75
+ $browser . defer . flush ( ) ;
67
76
expect ( element . text ( ) ) . toEqual ( 'misko' ) ;
68
77
} ) ) ;
69
78
70
-
71
- it ( 'should remove previously included text if a falsy value is bound to src' ,
72
- inject ( function ( $rootScope , $compile , $cacheFactory ) {
79
+
80
+ it ( 'should remove previously included text if a falsy value is bound to src' , inject (
81
+ putIntoCache ( 'myUrl' , '{{name}}' ) ,
82
+ function ( $rootScope , $compile , $browser ) {
73
83
var element = jqLite ( '<ng:include src="url" scope="childScope"></ng:include>' ) ;
74
84
var element = $compile ( element ) ( $rootScope ) ;
75
85
$rootScope . childScope = $rootScope . $new ( ) ;
76
86
$rootScope . childScope . name = 'igor' ;
77
87
$rootScope . url = 'myUrl' ;
78
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl' , '{{name}}' ) ;
79
88
$rootScope . $digest ( ) ;
89
+ $browser . defer . flush ( ) ;
80
90
81
91
expect ( element . text ( ) ) . toEqual ( 'igor' ) ;
82
92
@@ -86,13 +96,15 @@ describe("widget", function() {
86
96
expect ( element . text ( ) ) . toEqual ( '' ) ;
87
97
} ) ) ;
88
98
89
-
90
- it ( 'should allow this for scope' , inject ( function ( $rootScope , $compile , $cacheFactory ) {
99
+
100
+ it ( 'should allow this for scope' , inject ( putIntoCache ( 'myUrl' , '{{"abc"}}' ) ,
101
+ function ( $rootScope , $compile , $browser ) {
91
102
var element = jqLite ( '<ng:include src="url" scope="this"></ng:include>' ) ;
92
103
var element = $compile ( element ) ( $rootScope ) ;
93
104
$rootScope . url = 'myUrl' ;
94
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl' , '{{"abc"}}' ) ;
95
105
$rootScope . $digest ( ) ;
106
+ $browser . defer . flush ( ) ;
107
+
96
108
// TODO(misko): because we are using scope==this, the eval gets registered
97
109
// during the flush phase and hence does not get called.
98
110
// I don't think passing 'this' makes sense. Does having scope on ng:include makes sense?
@@ -102,39 +114,43 @@ describe("widget", function() {
102
114
expect ( element . text ( ) ) . toEqual ( 'abc' ) ;
103
115
} ) ) ;
104
116
105
-
106
- it ( 'should evaluate onload expression when a partial is loaded' ,
107
- inject ( function ( $rootScope , $compile , $cacheFactory ) {
117
+
118
+ it ( 'should evaluate onload expression when a partial is loaded' , inject (
119
+ putIntoCache ( 'myUrl' , 'my partial' ) ,
120
+ function ( $rootScope , $compile , $browser ) {
108
121
var element = jqLite ( '<ng:include src="url" onload="loaded = true"></ng:include>' ) ;
109
122
var element = $compile ( element ) ( $rootScope ) ;
110
123
111
124
expect ( $rootScope . loaded ) . not . toBeDefined ( ) ;
112
125
113
126
$rootScope . url = 'myUrl' ;
114
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl' , 'my partial' ) ;
115
127
$rootScope . $digest ( ) ;
128
+ $browser . defer . flush ( ) ;
129
+
116
130
expect ( element . text ( ) ) . toEqual ( 'my partial' ) ;
117
131
expect ( $rootScope . loaded ) . toBe ( true ) ;
118
132
} ) ) ;
119
133
120
-
121
- it ( 'should destroy old scope' , inject ( function ( $rootScope , $compile , $cacheFactory ) {
134
+
135
+ it ( 'should destroy old scope' , inject ( putIntoCache ( 'myUrl' , 'my partial' ) ,
136
+ function ( $rootScope , $compile , $browser ) {
122
137
var element = jqLite ( '<ng:include src="url"></ng:include>' ) ;
123
138
var element = $compile ( element ) ( $rootScope ) ;
124
139
125
140
expect ( $rootScope . $$childHead ) . toBeFalsy ( ) ;
126
141
127
142
$rootScope . url = 'myUrl' ;
128
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl' , 'my partial' ) ;
129
143
$rootScope . $digest ( ) ;
144
+ $browser . defer . flush ( ) ;
130
145
expect ( $rootScope . $$childHead ) . toBeTruthy ( ) ;
131
146
132
147
$rootScope . url = null ;
133
148
$rootScope . $digest ( ) ;
134
149
expect ( $rootScope . $$childHead ) . toBeFalsy ( ) ;
135
150
} ) ) ;
136
151
137
- it ( 'should do xhr request and cache it' , inject ( function ( $rootScope , $httpBackend , $compile ) {
152
+ it ( 'should do xhr request and cache it' ,
153
+ inject ( function ( $rootScope , $httpBackend , $compile , $browser ) {
138
154
var element = $compile ( '<ng:include src="url"></ng:include>' ) ( $rootScope ) ;
139
155
$httpBackend . expect ( 'GET' , 'myUrl' ) . respond ( 'my partial' ) ;
140
156
@@ -149,6 +165,7 @@ describe("widget", function() {
149
165
150
166
$rootScope . url = 'myUrl' ;
151
167
$rootScope . $digest ( ) ;
168
+ $browser . defer . flush ( ) ;
152
169
expect ( element . text ( ) ) . toEqual ( 'my partial' ) ;
153
170
dealoc ( $rootScope ) ;
154
171
} ) ) ;
@@ -165,11 +182,12 @@ describe("widget", function() {
165
182
expect ( element . text ( ) ) . toBe ( '' ) ;
166
183
} ) ) ;
167
184
168
- it ( 'should be async even if served from cache' , inject ( function ( $rootScope , $compile , $cacheFactory ) {
185
+ it ( 'should be async even if served from cache' , inject (
186
+ putIntoCache ( 'myUrl' , 'my partial' ) ,
187
+ function ( $rootScope , $compile , $browser ) {
169
188
var element = $compile ( '<ng:include src="url"></ng:include>' ) ( $rootScope ) ;
170
189
171
190
$rootScope . url = 'myUrl' ;
172
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl' , 'my partial' ) ;
173
191
174
192
var called = 0 ;
175
193
// we want to assert only during first watch
@@ -178,6 +196,7 @@ describe("widget", function() {
178
196
} ) ;
179
197
180
198
$rootScope . $digest ( ) ;
199
+ $browser . defer . flush ( ) ;
181
200
expect ( element . text ( ) ) . toBe ( 'my partial' ) ;
182
201
} ) ) ;
183
202
} ) ) ;
@@ -574,7 +593,8 @@ describe("widget", function() {
574
593
} ) ) ;
575
594
576
595
it ( 'should initialize view template after the view controller was initialized even when ' +
577
- 'templates were cached' , inject ( function ( $rootScope , $compile , $location , $httpBackend , $route ) {
596
+ 'templates were cached' ,
597
+ inject ( function ( $rootScope , $compile , $location , $httpBackend , $route , $browser ) {
578
598
//this is a test for a regression that was introduced by making the ng:view cache sync
579
599
580
600
$route . when ( '/foo' , { controller : ParentCtrl , template : 'viewPartial.html' } ) ;
@@ -605,6 +625,7 @@ describe("widget", function() {
605
625
$rootScope . log = [ ] ;
606
626
$location . path ( '/foo' ) ;
607
627
$rootScope . $apply ( ) ;
628
+ $browser . defer . flush ( ) ;
608
629
609
630
expect ( $rootScope . log ) . toEqual ( [ 'parent' , 'init' , 'child' ] ) ;
610
631
} ) ) ;
@@ -644,9 +665,9 @@ describe("widget", function() {
644
665
} ) ) ;
645
666
646
667
it ( 'should be async even if served from cache' ,
647
- inject ( function ( $route , $rootScope , $location , $cacheFactory ) {
668
+ inject ( function ( $route , $rootScope , $location , $templateCache , $browser ) {
669
+ $templateCache . put ( 'myUrl1' , [ 200 , 'my partial' , { } ] ) ;
648
670
$route . when ( '/foo' , { controller : noop , template : 'myUrl1' } ) ;
649
- $cacheFactory . get ( 'templates' ) . put ( 'myUrl1' , 'my partial' ) ;
650
671
$location . path ( '/foo' ) ;
651
672
652
673
var called = 0 ;
@@ -656,6 +677,7 @@ describe("widget", function() {
656
677
} ) ;
657
678
658
679
$rootScope . $digest ( ) ;
680
+ $browser . defer . flush ( ) ;
659
681
expect ( element . text ( ) ) . toBe ( 'my partial' ) ;
660
682
} ) ) ;
661
683
} ) ;
0 commit comments