@@ -10,7 +10,16 @@ describe('rating directive', function () {
10
10
$rootScope . $digest ( ) ;
11
11
} ) ) ;
12
12
13
- function getState ( stars ) {
13
+ function getStars ( ) {
14
+ return element . find ( 'i' ) ;
15
+ }
16
+
17
+ function getStar ( number ) {
18
+ return getStars ( ) . eq ( number - 1 ) ;
19
+ }
20
+
21
+ function getState ( ) {
22
+ var stars = getStars ( ) ;
14
23
var state = [ ] ;
15
24
for ( var i = 0 , n = stars . length ; i < n ; i ++ ) {
16
25
state . push ( ( stars . eq ( i ) . hasClass ( 'icon-star' ) && ! stars . eq ( i ) . hasClass ( 'icon-star-empty' ) ) ) ;
@@ -19,130 +28,111 @@ describe('rating directive', function () {
19
28
}
20
29
21
30
it ( 'contains the default number of icons' , function ( ) {
22
- expect ( element . find ( 'i' ) . length ) . toBe ( 5 ) ;
31
+ expect ( getStars ( ) . length ) . toBe ( 5 ) ;
23
32
} ) ;
24
33
25
34
it ( 'initializes the default star icons as selected' , function ( ) {
26
- var stars = element . find ( 'i' ) ;
27
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , false , false ] ) ;
35
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
28
36
} ) ;
29
37
30
38
it ( 'handles correctly the click event' , function ( ) {
31
- var stars = element . find ( 'i' ) ;
32
-
33
- var star2 = stars . eq ( 1 ) ;
34
- star2 . click ( ) ;
39
+ getStar ( 2 ) . click ( ) ;
35
40
$rootScope . $digest ( ) ;
36
- expect ( getState ( stars ) ) . toEqual ( [ true , true , false , false , false ] ) ;
41
+ expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
37
42
expect ( $rootScope . rate ) . toBe ( 2 ) ;
38
43
39
- var star5 = stars . eq ( 4 ) ;
40
- star5 . click ( ) ;
44
+ getStar ( 5 ) . click ( ) ;
41
45
$rootScope . $digest ( ) ;
42
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , true , true ] ) ;
46
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
43
47
expect ( $rootScope . rate ) . toBe ( 5 ) ;
44
48
} ) ;
45
49
46
50
it ( 'handles correctly the hover event' , function ( ) {
47
- var stars = element . find ( 'i' ) ;
48
-
49
- var star2 = stars . eq ( 1 ) ;
50
- star2 . trigger ( 'mouseover' ) ;
51
+ getStar ( 2 ) . trigger ( 'mouseover' ) ;
51
52
$rootScope . $digest ( ) ;
52
- expect ( getState ( stars ) ) . toEqual ( [ true , true , false , false , false ] ) ;
53
+ expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
53
54
expect ( $rootScope . rate ) . toBe ( 3 ) ;
54
55
55
- var star5 = stars . eq ( 4 ) ;
56
- star5 . trigger ( 'mouseover' ) ;
56
+ getStar ( 5 ) . trigger ( 'mouseover' ) ;
57
57
$rootScope . $digest ( ) ;
58
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , true , true ] ) ;
58
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
59
59
expect ( $rootScope . rate ) . toBe ( 3 ) ;
60
60
61
61
element . trigger ( 'mouseout' ) ;
62
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , false , false ] ) ;
62
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
63
63
expect ( $rootScope . rate ) . toBe ( 3 ) ;
64
64
} ) ;
65
65
66
66
it ( 'changes the number of selected icons when value changes' , function ( ) {
67
67
$rootScope . rate = 2 ;
68
68
$rootScope . $digest ( ) ;
69
69
70
- var stars = element . find ( 'i' ) ;
71
- expect ( getState ( stars ) ) . toEqual ( [ true , true , false , false , false ] ) ;
70
+ expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
72
71
} ) ;
73
72
74
73
it ( 'shows different number of icons when `max` attribute is set' , function ( ) {
75
74
element = $compile ( '<rating value="rate" max="7"></rating>' ) ( $rootScope ) ;
76
75
$rootScope . $digest ( ) ;
77
76
78
- expect ( element . find ( 'i' ) . length ) . toBe ( 7 ) ;
77
+ expect ( getStars ( ) . length ) . toBe ( 7 ) ;
79
78
} ) ;
80
79
81
80
it ( 'handles readonly attribute' , function ( ) {
82
81
$rootScope . isReadonly = true ;
83
82
element = $compile ( '<rating value="rate" readonly="isReadonly"></rating>' ) ( $rootScope ) ;
84
83
$rootScope . $digest ( ) ;
85
84
86
- var stars = element . find ( 'i' ) ;
87
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , false , false ] ) ;
85
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
88
86
89
- var star5 = stars . eq ( 4 ) ;
87
+ var star5 = getStar ( 5 ) ;
90
88
star5 . trigger ( 'mouseover' ) ;
91
89
$rootScope . $digest ( ) ;
92
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , false , false ] ) ;
90
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
93
91
94
92
$rootScope . isReadonly = false ;
95
93
$rootScope . $digest ( ) ;
96
94
97
95
star5 . trigger ( 'mouseover' ) ;
98
96
$rootScope . $digest ( ) ;
99
- expect ( getState ( stars ) ) . toEqual ( [ true , true , true , true , true ] ) ;
97
+ expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
100
98
} ) ;
101
99
102
100
it ( 'should fire onHover' , function ( ) {
103
101
$rootScope . hoveringOver = jasmine . createSpy ( 'hoveringOver' ) ;
104
-
105
102
element = $compile ( '<rating value="rate" on-hover="hoveringOver(value)"></rating>' ) ( $rootScope ) ;
106
103
$rootScope . $digest ( ) ;
107
104
108
- var star3 = element . find ( 'i' ) . eq ( 2 ) ;
109
- star3 . trigger ( 'mouseover' ) ;
105
+ getStar ( 3 ) . trigger ( 'mouseover' ) ;
110
106
$rootScope . $digest ( ) ;
111
107
expect ( $rootScope . hoveringOver ) . toHaveBeenCalledWith ( 3 ) ;
112
108
} ) ;
113
109
114
110
it ( 'should fire onLeave' , function ( ) {
115
111
$rootScope . leaving = jasmine . createSpy ( 'leaving' ) ;
116
-
117
112
element = $compile ( '<rating value="rate" on-leave="leaving()"></rating>' ) ( $rootScope ) ;
118
113
$rootScope . $digest ( ) ;
119
114
120
115
element . trigger ( 'mouseleave' ) ;
121
116
$rootScope . $digest ( ) ;
122
117
expect ( $rootScope . leaving ) . toHaveBeenCalled ( ) ;
123
118
} ) ;
124
- } ) ;
125
119
126
- describe ( 'setting ratingConfig' , function ( ) {
127
- var $rootScope , element ;
128
- var originalConfig = { } ;
129
- beforeEach ( module ( 'ui.bootstrap.rating' ) ) ;
130
- beforeEach ( module ( 'template/rating/rating.html' ) ) ;
131
- beforeEach ( inject ( function ( _$compile_ , _$rootScope_ , ratingConfig ) {
132
- $compile = _$compile_ ;
133
- $rootScope = _$rootScope_ ;
134
- $rootScope . rate = 5 ;
135
- angular . extend ( originalConfig , ratingConfig ) ;
136
- ratingConfig . max = 10 ;
137
- element = $compile ( '<rating value="rate"></rating>' ) ( $rootScope ) ;
138
- $rootScope . $digest ( ) ;
139
- } ) ) ;
140
- afterEach ( inject ( function ( ratingConfig ) {
141
- // return it to the original state
142
- angular . extend ( ratingConfig , originalConfig ) ;
143
- } ) ) ;
144
-
145
- it ( 'should change number of icon elements' , function ( ) {
146
- expect ( element . find ( 'i' ) . length ) . toBe ( 10 ) ;
120
+ describe ( 'setting ratingConfig' , function ( ) {
121
+ var originalConfig = { } ;
122
+ beforeEach ( inject ( function ( ratingConfig ) {
123
+ $rootScope . rate = 5 ;
124
+ angular . extend ( originalConfig , ratingConfig ) ;
125
+ ratingConfig . max = 10 ;
126
+ element = $compile ( '<rating value="rate"></rating>' ) ( $rootScope ) ;
127
+ $rootScope . $digest ( ) ;
128
+ } ) ) ;
129
+ afterEach ( inject ( function ( ratingConfig ) {
130
+ // return it to the original state
131
+ angular . extend ( ratingConfig , originalConfig ) ;
132
+ } ) ) ;
133
+
134
+ it ( 'should change number of icon elements' , function ( ) {
135
+ expect ( getStars ( ) . length ) . toBe ( 10 ) ;
136
+ } ) ;
147
137
} ) ;
148
- } ) ;
138
+ } ) ;
0 commit comments