2
2
3
3
describe ( 'ngRef' , function ( ) {
4
4
5
- describe ( 'given a component' , function ( ) {
5
+ describe ( 'on a component' , function ( ) {
6
6
7
7
var myComponentController , $rootScope , $compile ;
8
8
9
9
beforeEach ( module ( function ( $compileProvider ) {
10
10
$compileProvider . component ( 'myComponent' , {
11
11
template : 'foo' ,
12
12
controller : function ( ) {
13
+ console . log ( 'ctrl' ) ;
13
14
myComponentController = this ;
14
15
}
15
16
} ) ;
16
17
} ) ) ;
17
18
18
- beforeEach ( module ( function ( $exceptionHandlerProvider ) {
19
- $exceptionHandlerProvider . mode ( 'log' ) ;
20
- } ) ) ;
21
-
22
19
beforeEach ( inject ( function ( _$compile_ , _$rootScope_ ) {
23
20
$rootScope = _$rootScope_ ;
24
21
$compile = _$compile_ ;
25
22
} ) ) ;
26
23
27
- afterEach ( inject ( function ( $exceptionHandler ) {
28
- if ( $exceptionHandler . errors . length ) {
29
- dump ( jasmine . getEnv ( ) . currentSpec . getFullName ( ) ) ;
30
- dump ( '$exceptionHandler has errors' ) ;
31
- dump ( $exceptionHandler . errors ) ;
32
- expect ( $exceptionHandler . errors ) . toBe ( [ ] ) ;
33
- }
34
- } ) ) ;
35
-
36
24
it ( 'should bind in the current scope the controller of a component' , function ( ) {
37
25
$rootScope . $ctrl = 'undamaged' ;
38
26
@@ -41,6 +29,12 @@ describe('ngRef', function() {
41
29
expect ( $rootScope . myComponent ) . toBe ( myComponentController ) ;
42
30
} ) ;
43
31
32
+ it ( 'should throw if the expression is not assignable' , function ( ) {
33
+ expect ( function ( ) {
34
+ $compile ( '<my-component ng-ref="\'hello\'"></my-component>' ) ( $rootScope ) ;
35
+ } ) . toThrowMinErr ( 'ngRef' , 'nonassign' , 'Expression in ngRef="\'hello\'" is non-assignable!' ) ;
36
+ } ) ;
37
+
44
38
it ( 'should work with non:normalized entity name' , function ( ) {
45
39
$compile ( '<my:component ng-ref="myComponent1"></my:component>' ) ( $rootScope ) ;
46
40
expect ( $rootScope . myComponent1 ) . toBe ( myComponentController ) ;
@@ -114,15 +108,6 @@ describe('ngRef', function() {
114
108
expect ( $rootScope . $ctrl . myComponent ) . toBe ( myComponentController ) ;
115
109
} ) ;
116
110
117
- it ( 'should bind the element instead of the controller of a component if ngRefElement is set' , function ( ) {
118
- $rootScope . $ctrl = 'undamaged' ;
119
-
120
- var el = $compile ( '<my-component ng-ref="myEl" ng-ref-element></my-component>' ) ( $rootScope ) ;
121
- expect ( $rootScope . $ctrl ) . toBe ( 'undamaged' ) ;
122
- expect ( $rootScope . myEl ) . toEqual ( el ) ;
123
- expect ( $rootScope . myEl [ 0 ] . textContent ) . toBe ( 'foo' ) ;
124
- } ) ;
125
-
126
111
} ) ;
127
112
128
113
it ( 'should bind the jqlite wrapped DOM element if there is no component' , inject ( function ( $compile , $rootScope ) {
@@ -158,28 +143,112 @@ describe('ngRef', function() {
158
143
} ) ;
159
144
} ) ;
160
145
146
+ describe ( 'ngRefRead' , function ( ) {
161
147
162
- it ( 'should bind the element instead an element-directive controller if ngRefElement is set' , function ( ) {
163
- var myDirectiveController ;
148
+ it ( 'should bind the element instead of the controller of a component if ngRefRead="$element" is set' , function ( ) {
164
149
165
- module ( function ( $compileProvider ) {
166
- $compileProvider . directive ( 'myDirective' , function ( ) {
167
- return {
168
- restrict : 'E' ,
150
+ module ( function ( $compileProvider ) {
151
+
152
+ $compileProvider . component ( 'myComponent' , {
169
153
template : 'my text' ,
170
- controller : function ( ) {
171
- myDirectiveController = this ;
172
- }
173
- } ;
154
+ controller : function ( ) { }
155
+ } ) ;
156
+ } ) ;
157
+
158
+ inject ( function ( $compile , $rootScope ) {
159
+
160
+ var el = $compile ( '<my-component ng-ref="myEl" ng-ref-read="$element"></my-component>' ) ( $rootScope ) ;
161
+ expect ( $rootScope . myEl ) . toEqual ( el ) ;
162
+ expect ( $rootScope . myEl [ 0 ] . textContent ) . toBe ( 'my text' ) ;
174
163
} ) ;
175
164
} ) ;
176
165
177
- inject ( function ( $compile , $rootScope ) {
178
- var el = $compile ( '<my-directive ng-ref="myEl" ng-ref-element></my-directive>' ) ( $rootScope ) ;
179
166
180
- expect ( $rootScope . myEl ) . toEqual ( el ) ;
181
- expect ( $rootScope . myEl [ 0 ] . textContent ) . toBe ( 'my text' ) ;
167
+ it ( 'should bind the element instead an element-directive controller if ngRefRead="$element" is set' , function ( ) {
168
+
169
+ module ( function ( $compileProvider ) {
170
+ $compileProvider . directive ( 'myDirective' , function ( ) {
171
+ return {
172
+ restrict : 'E' ,
173
+ template : 'my text' ,
174
+ controller : function ( ) { }
175
+ } ;
176
+ } ) ;
177
+ } ) ;
178
+
179
+ inject ( function ( $compile , $rootScope ) {
180
+ var el = $compile ( '<my-directive ng-ref="myEl" ng-ref-read="$element"></my-directive>' ) ( $rootScope ) ;
181
+
182
+ expect ( $rootScope . myEl ) . toEqual ( el ) ;
183
+ expect ( $rootScope . myEl [ 0 ] . textContent ) . toBe ( 'my text' ) ;
184
+ } ) ;
185
+ } ) ;
186
+
187
+
188
+ it ( 'should bind an attribute-directive controller if ngRefRead="controllerName" is set' , function ( ) {
189
+ var attrDirective2Controller ;
190
+
191
+ module ( function ( $compileProvider ) {
192
+ $compileProvider . directive ( 'elementDirective' , function ( ) {
193
+ return {
194
+ restrict : 'E' ,
195
+ template : 'my text' ,
196
+ controller : function ( ) { }
197
+ } ;
198
+ } ) ;
199
+
200
+ $compileProvider . directive ( 'attributeDirective1' , function ( ) {
201
+ return {
202
+ restrict : 'A' ,
203
+ controller : function ( ) {
204
+ attrDirective2Controller = this ;
205
+ }
206
+ } ;
207
+ } ) ;
208
+
209
+ $compileProvider . directive ( 'attributeDirective2' , function ( ) {
210
+ return {
211
+ restrict : 'A' ,
212
+ controller : function ( ) { }
213
+ } ;
214
+ } ) ;
215
+
216
+ } ) ;
217
+
218
+ inject ( function ( $compile , $rootScope ) {
219
+ var el = $compile ( '<element-directive' +
220
+ 'attribute-directive-1' +
221
+ 'attribute-directive-2' +
222
+ 'ng-ref="myController"' +
223
+ 'ng-ref-read="$element"></element-directive>' ) ( $rootScope ) ;
224
+
225
+ expect ( $rootScope . myController ) . toBe ( attrDirective2Controller ) ;
226
+ } ) ;
182
227
} ) ;
228
+
229
+ it ( 'should throw if no controller is found for the ngRefRead value' , function ( ) {
230
+
231
+ module ( function ( $compileProvider ) {
232
+ $compileProvider . directive ( 'elementDirective' , function ( ) {
233
+ return {
234
+ restrict : 'E' ,
235
+ template : 'my text' ,
236
+ controller : function ( ) { }
237
+ } ;
238
+ } ) ;
239
+ } ) ;
240
+
241
+ inject ( function ( $compile , $rootScope ) {
242
+
243
+ expect ( function ( ) {
244
+ $compile ( '<element-directive ' +
245
+ 'ng-ref="myController"' +
246
+ 'ng-ref-read="attribute"></element-directive>' ) ( $rootScope ) ;
247
+ } ) . toThrowMinErr ( 'ngRef' , 'noctrl' , 'The controller for ngRefRead="attribute" could not be found on ngRef="myController"' ) ;
248
+
249
+ } ) ;
250
+ } ) ;
251
+
183
252
} ) ;
184
253
185
254
0 commit comments