@@ -27,24 +27,34 @@ describe('rating directive', function () {
27
27
return state ;
28
28
}
29
29
30
+ function triggerKeyDown ( keyCode ) {
31
+ var e = $ . Event ( 'keydown' ) ;
32
+ e . which = keyCode ;
33
+ element . trigger ( e ) ;
34
+ }
35
+
30
36
it ( 'contains the default number of icons' , function ( ) {
31
37
expect ( getStars ( ) . length ) . toBe ( 5 ) ;
38
+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '5' ) ;
32
39
} ) ;
33
40
34
41
it ( 'initializes the default star icons as selected' , function ( ) {
35
42
expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
43
+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '3' ) ;
36
44
} ) ;
37
45
38
46
it ( 'handles correctly the click event' , function ( ) {
39
47
getStar ( 2 ) . click ( ) ;
40
48
$rootScope . $digest ( ) ;
41
49
expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
42
50
expect ( $rootScope . rate ) . toBe ( 2 ) ;
51
+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
43
52
44
53
getStar ( 5 ) . click ( ) ;
45
54
$rootScope . $digest ( ) ;
46
55
expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
47
56
expect ( $rootScope . rate ) . toBe ( 5 ) ;
57
+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
48
58
} ) ;
49
59
50
60
it ( 'handles correctly the hover event' , function ( ) {
@@ -68,20 +78,23 @@ describe('rating directive', function () {
68
78
$rootScope . $digest ( ) ;
69
79
70
80
expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
81
+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
71
82
} ) ;
72
83
73
84
it ( 'shows different number of icons when `max` attribute is set' , function ( ) {
74
85
element = $compile ( '<rating ng-model="rate" max="7"></rating>' ) ( $rootScope ) ;
75
86
$rootScope . $digest ( ) ;
76
87
77
88
expect ( getStars ( ) . length ) . toBe ( 7 ) ;
89
+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '7' ) ;
78
90
} ) ;
79
91
80
92
it ( 'shows different number of icons when `max` attribute is from scope variable' , function ( ) {
81
93
$rootScope . max = 15 ;
82
94
element = $compile ( '<rating ng-model="rate" max="max"></rating>' ) ( $rootScope ) ;
83
95
$rootScope . $digest ( ) ;
84
96
expect ( getStars ( ) . length ) . toBe ( 15 ) ;
97
+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '15' ) ;
85
98
} ) ;
86
99
87
100
it ( 'handles readonly attribute' , function ( ) {
@@ -124,6 +137,43 @@ describe('rating directive', function () {
124
137
expect ( $rootScope . leaving ) . toHaveBeenCalled ( ) ;
125
138
} ) ;
126
139
140
+ describe ( 'keyboard navigation' , function ( ) {
141
+ it ( 'supports arrow keys' , function ( ) {
142
+ triggerKeyDown ( 38 ) ;
143
+ expect ( $rootScope . rate ) . toBe ( 4 ) ;
144
+
145
+ triggerKeyDown ( 37 ) ;
146
+ expect ( $rootScope . rate ) . toBe ( 3 ) ;
147
+ triggerKeyDown ( 40 ) ;
148
+ expect ( $rootScope . rate ) . toBe ( 2 ) ;
149
+
150
+ triggerKeyDown ( 39 ) ;
151
+ expect ( $rootScope . rate ) . toBe ( 3 ) ;
152
+ } ) ;
153
+
154
+ it ( 'can get zero value but not negative' , function ( ) {
155
+ $rootScope . rate = 1 ;
156
+ $rootScope . $digest ( ) ;
157
+
158
+ triggerKeyDown ( 37 ) ;
159
+ expect ( $rootScope . rate ) . toBe ( 0 ) ;
160
+
161
+ triggerKeyDown ( 37 ) ;
162
+ expect ( $rootScope . rate ) . toBe ( 0 ) ;
163
+ } ) ;
164
+
165
+ it ( 'cannot get value above max' , function ( ) {
166
+ $rootScope . rate = 4 ;
167
+ $rootScope . $digest ( ) ;
168
+
169
+ triggerKeyDown ( 38 ) ;
170
+ expect ( $rootScope . rate ) . toBe ( 5 ) ;
171
+
172
+ triggerKeyDown ( 38 ) ;
173
+ expect ( $rootScope . rate ) . toBe ( 5 ) ;
174
+ } ) ;
175
+ } ) ;
176
+
127
177
describe ( 'custom states' , function ( ) {
128
178
beforeEach ( inject ( function ( ) {
129
179
$rootScope . classOn = 'icon-ok-sign' ;
@@ -150,7 +200,8 @@ describe('rating directive', function () {
150
200
} ) ) ;
151
201
152
202
it ( 'should define number of icon elements' , function ( ) {
153
- expect ( getStars ( ) . length ) . toBe ( $rootScope . states . length ) ;
203
+ expect ( getStars ( ) . length ) . toBe ( 4 ) ;
204
+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '4' ) ;
154
205
} ) ;
155
206
156
207
it ( 'handles each icon' , function ( ) {
0 commit comments