1
1
describe ( 'rating directive' , function ( ) {
2
- var $rootScope , $compile , element ;
2
+ var $rootScope , $compile , element , innerElem ;
3
3
beforeEach ( module ( 'ui.bootstrap.rating' ) ) ;
4
4
beforeEach ( module ( 'uib/template/rating/rating.html' ) ) ;
5
5
beforeEach ( inject ( function ( _$compile_ , _$rootScope_ ) {
6
6
$compile = _$compile_ ;
7
7
$rootScope = _$rootScope_ ;
8
8
$rootScope . rate = 3 ;
9
- element = $compile ( '<uib-rating ng-model="rate"></uib-rating >' ) ( $rootScope ) ;
9
+ element = $compile ( '<span uib-rating ng-model="rate"></span >' ) ( $rootScope ) ;
10
10
$rootScope . $digest ( ) ;
11
+ innerElem = element . children ( ) . eq ( 0 ) ;
11
12
} ) ) ;
12
13
13
14
function getStars ( ) {
14
- return element . find ( 'i' ) ;
15
+ return innerElem . find ( 'i' ) ;
15
16
}
16
17
17
18
function getStar ( number ) {
@@ -38,37 +39,37 @@ describe('rating directive', function() {
38
39
function triggerKeyDown ( keyCode ) {
39
40
var e = $ . Event ( 'keydown' ) ;
40
41
e . which = keyCode ;
41
- element . trigger ( e ) ;
42
+ innerElem . trigger ( e ) ;
42
43
}
43
44
44
45
it ( 'contains the default number of icons' , function ( ) {
45
46
expect ( getStars ( ) . length ) . toBe ( 5 ) ;
46
- expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '5' ) ;
47
+ expect ( innerElem . attr ( 'aria-valuemax' ) ) . toBe ( '5' ) ;
47
48
} ) ;
48
49
49
50
it ( 'initializes the default star icons as selected' , function ( ) {
50
51
expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
51
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '3' ) ;
52
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '3' ) ;
52
53
} ) ;
53
54
54
55
it ( 'handles correctly the click event' , function ( ) {
55
56
getStar ( 2 ) . click ( ) ;
56
57
$rootScope . $digest ( ) ;
57
58
expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
58
59
expect ( $rootScope . rate ) . toBe ( 2 ) ;
59
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
60
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
60
61
61
62
getStar ( 5 ) . click ( ) ;
62
63
$rootScope . $digest ( ) ;
63
64
expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
64
65
expect ( $rootScope . rate ) . toBe ( 5 ) ;
65
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
66
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
66
67
67
68
getStar ( 5 ) . click ( ) ;
68
69
$rootScope . $digest ( ) ;
69
70
expect ( getState ( ) ) . toEqual ( [ false , false , false , false , false ] ) ;
70
71
expect ( $rootScope . rate ) . toBe ( 0 ) ;
71
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '0' ) ;
72
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '0' ) ;
72
73
} ) ;
73
74
74
75
it ( 'handles correctly the hover event' , function ( ) {
@@ -82,7 +83,7 @@ describe('rating directive', function() {
82
83
expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
83
84
expect ( $rootScope . rate ) . toBe ( 3 ) ;
84
85
85
- element . trigger ( 'mouseout' ) ;
86
+ innerElem . trigger ( 'mouseout' ) ;
86
87
expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
87
88
expect ( $rootScope . rate ) . toBe ( 3 ) ;
88
89
} ) ;
@@ -92,44 +93,47 @@ describe('rating directive', function() {
92
93
$rootScope . $digest ( ) ;
93
94
94
95
expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
95
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
96
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
96
97
97
98
$rootScope . rate = 2.5 ;
98
99
$rootScope . $digest ( ) ;
99
100
100
101
expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
101
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '3' ) ;
102
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '3' ) ;
102
103
} ) ;
103
104
104
105
it ( 'changes the number of selected icons when value changes' , function ( ) {
105
106
$rootScope . rate = 2 ;
106
107
$rootScope . $digest ( ) ;
107
108
108
109
expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
109
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
110
- expect ( element . attr ( 'aria-valuetext' ) ) . toBe ( 'two' ) ;
110
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
111
+ expect ( innerElem . attr ( 'aria-valuetext' ) ) . toBe ( 'two' ) ;
111
112
} ) ;
112
113
113
114
it ( 'shows different number of icons when `max` attribute is set' , function ( ) {
114
- element = $compile ( '<uib-rating ng-model="rate" max="7"></uib-rating >' ) ( $rootScope ) ;
115
+ element = $compile ( '<span uib-rating ng-model="rate" max="7"></span >' ) ( $rootScope ) ;
115
116
$rootScope . $digest ( ) ;
117
+ innerElem = element . children ( ) . eq ( 0 ) ;
116
118
117
119
expect ( getStars ( ) . length ) . toBe ( 7 ) ;
118
- expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '7' ) ;
120
+ expect ( innerElem . attr ( 'aria-valuemax' ) ) . toBe ( '7' ) ;
119
121
} ) ;
120
122
121
123
it ( 'shows different number of icons when `max` attribute is from scope variable' , function ( ) {
122
124
$rootScope . max = 15 ;
123
- element = $compile ( '<uib-rating ng-model="rate" max="max"></uib-rating >' ) ( $rootScope ) ;
125
+ element = $compile ( '<span uib-rating ng-model="rate" max="max"></span >' ) ( $rootScope ) ;
124
126
$rootScope . $digest ( ) ;
127
+ innerElem = element . children ( ) . eq ( 0 ) ;
125
128
expect ( getStars ( ) . length ) . toBe ( 15 ) ;
126
- expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '15' ) ;
129
+ expect ( innerElem . attr ( 'aria-valuemax' ) ) . toBe ( '15' ) ;
127
130
} ) ;
128
131
129
132
it ( 'handles read-only attribute' , function ( ) {
130
133
$rootScope . isReadonly = true ;
131
- element = $compile ( '<uib-rating ng-model="rate" read-only="isReadonly"></uib-rating >' ) ( $rootScope ) ;
134
+ element = $compile ( '<span uib-rating ng-model="rate" read-only="isReadonly"></span >' ) ( $rootScope ) ;
132
135
$rootScope . $digest ( ) ;
136
+ innerElem = element . children ( ) . eq ( 0 ) ;
133
137
134
138
expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
135
139
@@ -148,8 +152,9 @@ describe('rating directive', function() {
148
152
149
153
it ( 'handles enable-reset attribute' , function ( ) {
150
154
$rootScope . canReset = false ;
151
- element = $compile ( '<uib-rating ng-model="rate" enable-reset="canReset"></uib-rating >' ) ( $rootScope ) ;
155
+ element = $compile ( '<span uib-rating ng-model="rate" enable-reset="canReset"></span >' ) ( $rootScope ) ;
152
156
$rootScope . $digest ( ) ;
157
+ innerElem = element . children ( ) . eq ( 0 ) ;
153
158
154
159
var star = {
155
160
states : [ true , true , true , true , true ] ,
@@ -162,19 +167,20 @@ describe('rating directive', function() {
162
167
$rootScope . $digest ( ) ;
163
168
expect ( getState ( ) ) . toEqual ( star . states ) ;
164
169
expect ( $rootScope . rate ) . toBe ( 5 ) ;
165
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
170
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
166
171
167
172
selectStar . click ( ) ;
168
173
$rootScope . $digest ( ) ;
169
174
expect ( getState ( ) ) . toEqual ( star . states ) ;
170
175
expect ( $rootScope . rate ) . toBe ( 5 ) ;
171
- expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
176
+ expect ( innerElem . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
172
177
} ) ;
173
178
174
179
it ( 'should fire onHover' , function ( ) {
175
180
$rootScope . hoveringOver = jasmine . createSpy ( 'hoveringOver' ) ;
176
- element = $compile ( '<uib-rating ng-model="rate" on-hover="hoveringOver(value)"></uib-rating >' ) ( $rootScope ) ;
181
+ element = $compile ( '<span uib-rating ng-model="rate" on-hover="hoveringOver(value)"></span >' ) ( $rootScope ) ;
177
182
$rootScope . $digest ( ) ;
183
+ innerElem = element . children ( ) . eq ( 0 ) ;
178
184
179
185
getStar ( 3 ) . trigger ( 'mouseover' ) ;
180
186
$rootScope . $digest ( ) ;
@@ -183,10 +189,11 @@ describe('rating directive', function() {
183
189
184
190
it ( 'should fire onLeave' , function ( ) {
185
191
$rootScope . leaving = jasmine . createSpy ( 'leaving' ) ;
186
- element = $compile ( '<uib-rating ng-model="rate" on-leave="leaving()"></uib-rating >' ) ( $rootScope ) ;
192
+ element = $compile ( '<span uib-rating ng-model="rate" on-leave="leaving()"></span >' ) ( $rootScope ) ;
187
193
$rootScope . $digest ( ) ;
194
+ innerElem = element . children ( ) . eq ( 0 ) ;
188
195
189
- element . trigger ( 'mouseleave' ) ;
196
+ innerElem . trigger ( 'mouseleave' ) ;
190
197
$rootScope . $digest ( ) ;
191
198
expect ( $rootScope . leaving ) . toHaveBeenCalled ( ) ;
192
199
} ) ;
@@ -243,8 +250,9 @@ describe('rating directive', function() {
243
250
beforeEach ( inject ( function ( ) {
244
251
$rootScope . classOn = 'icon-ok-sign' ;
245
252
$rootScope . classOff = 'icon-ok-circle' ;
246
- element = $compile ( '<uib-rating ng-model="rate" state-on="classOn" state-off="classOff"></uib-rating >' ) ( $rootScope ) ;
253
+ element = $compile ( '<span uib-rating ng-model="rate" state-on="classOn" state-off="classOff"></span >' ) ( $rootScope ) ;
247
254
$rootScope . $digest ( ) ;
255
+ innerElem = element . children ( ) . eq ( 0 ) ;
248
256
} ) ) ;
249
257
250
258
it ( 'changes the default icons' , function ( ) {
@@ -260,13 +268,14 @@ describe('rating directive', function() {
260
268
{ stateOn : 'heart' } ,
261
269
{ stateOff : 'off' }
262
270
] ;
263
- element = $compile ( '<uib-rating ng-model="rate" rating-states="states"></uib-rating >' ) ( $rootScope ) ;
271
+ element = $compile ( '<span uib-rating ng-model="rate" rating-states="states"></span >' ) ( $rootScope ) ;
264
272
$rootScope . $digest ( ) ;
273
+ innerElem = element . children ( ) . eq ( 0 ) ;
265
274
} ) ) ;
266
275
267
276
it ( 'should define number of icon elements' , function ( ) {
268
277
expect ( getStars ( ) . length ) . toBe ( 4 ) ;
269
- expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '4' ) ;
278
+ expect ( innerElem . attr ( 'aria-valuemax' ) ) . toBe ( '4' ) ;
270
279
} ) ;
271
280
272
281
it ( 'handles each icon' , function ( ) {
@@ -291,8 +300,9 @@ describe('rating directive', function() {
291
300
uibRatingConfig . max = 10 ;
292
301
uibRatingConfig . stateOn = 'on' ;
293
302
uibRatingConfig . stateOff = 'off' ;
294
- element = $compile ( '<uib-rating ng-model="rate"></uib-rating >' ) ( $rootScope ) ;
303
+ element = $compile ( '<span uib-rating ng-model="rate"></span >' ) ( $rootScope ) ;
295
304
$rootScope . $digest ( ) ;
305
+ innerElem = element . children ( ) . eq ( 0 ) ;
296
306
} ) ) ;
297
307
afterEach ( inject ( function ( uibRatingConfig ) {
298
308
// return it to the original state
@@ -320,8 +330,9 @@ describe('rating directive', function() {
320
330
$rootScope . rate = 5 ;
321
331
angular . extend ( originalConfig , uibRatingConfig ) ;
322
332
uibRatingConfig . max = 10 ;
323
- element = $compile ( '<uib-rating ng-model="rate"></uib-rating >' ) ( $rootScope ) ;
333
+ element = $compile ( '<span uib-rating ng-model="rate"></span >' ) ( $rootScope ) ;
324
334
$rootScope . $digest ( ) ;
335
+ innerElem = element . children ( ) . eq ( 0 ) ;
325
336
} ) ) ;
326
337
afterEach ( inject ( function ( uibRatingConfig ) {
327
338
// return it to the original state
@@ -336,18 +347,20 @@ describe('rating directive', function() {
336
347
describe ( 'shows custom titles ' , function ( ) {
337
348
it ( 'should return the custom title for each star' , function ( ) {
338
349
$rootScope . titles = [ 44 , 45 , 46 ] ;
339
- element = $compile ( '<uib-rating ng-model="rate" titles="titles"></uib-rating >' ) ( $rootScope ) ;
350
+ element = $compile ( '<span uib-rating ng-model="rate" titles="titles"></span >' ) ( $rootScope ) ;
340
351
$rootScope . $digest ( ) ;
352
+ innerElem = element . children ( ) . eq ( 0 ) ;
341
353
expect ( getTitles ( ) ) . toEqual ( [ '44' , '45' , '46' , '4' , '5' ] ) ;
342
354
} ) ;
343
355
it ( 'should return the default title if the custom title is empty' , function ( ) {
344
356
$rootScope . titles = [ ] ;
345
- element = $compile ( '<uib-rating ng-model="rate" titles="titles"></uib-rating >' ) ( $rootScope ) ;
357
+ element = $compile ( '<span uib-rating ng-model="rate" titles="titles"></span >' ) ( $rootScope ) ;
346
358
$rootScope . $digest ( ) ;
359
+ innerElem = element . children ( ) . eq ( 0 ) ;
347
360
expect ( getTitles ( ) ) . toEqual ( [ 'one' , 'two' , 'three' , 'four' , 'five' ] ) ;
348
361
} ) ;
349
362
it ( 'should return the default title if the custom title is not an array' , function ( ) {
350
- element = $compile ( '<uib-rating ng-model="rate" titles="test"></uib-rating >' ) ( $rootScope ) ;
363
+ element = $compile ( '<span uib-rating ng-model="rate" titles="test"></span >' ) ( $rootScope ) ;
351
364
$rootScope . $digest ( ) ;
352
365
expect ( getTitles ( ) ) . toEqual ( [ 'one' , 'two' , 'three' , 'four' , 'five' ] ) ;
353
366
} ) ;
0 commit comments