@@ -27,24 +27,34 @@ describe('rating directive', function () {
2727 return state ;
2828 }
2929
30+ function triggerKeyDown ( keyCode ) {
31+ var e = $ . Event ( 'keydown' ) ;
32+ e . which = keyCode ;
33+ element . trigger ( e ) ;
34+ }
35+
3036 it ( 'contains the default number of icons' , function ( ) {
3137 expect ( getStars ( ) . length ) . toBe ( 5 ) ;
38+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '5' ) ;
3239 } ) ;
3340
3441 it ( 'initializes the default star icons as selected' , function ( ) {
3542 expect ( getState ( ) ) . toEqual ( [ true , true , true , false , false ] ) ;
43+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '3' ) ;
3644 } ) ;
3745
3846 it ( 'handles correctly the click event' , function ( ) {
3947 getStar ( 2 ) . click ( ) ;
4048 $rootScope . $digest ( ) ;
4149 expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
4250 expect ( $rootScope . rate ) . toBe ( 2 ) ;
51+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
4352
4453 getStar ( 5 ) . click ( ) ;
4554 $rootScope . $digest ( ) ;
4655 expect ( getState ( ) ) . toEqual ( [ true , true , true , true , true ] ) ;
4756 expect ( $rootScope . rate ) . toBe ( 5 ) ;
57+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '5' ) ;
4858 } ) ;
4959
5060 it ( 'handles correctly the hover event' , function ( ) {
@@ -68,20 +78,23 @@ describe('rating directive', function () {
6878 $rootScope . $digest ( ) ;
6979
7080 expect ( getState ( ) ) . toEqual ( [ true , true , false , false , false ] ) ;
81+ expect ( element . attr ( 'aria-valuenow' ) ) . toBe ( '2' ) ;
7182 } ) ;
7283
7384 it ( 'shows different number of icons when `max` attribute is set' , function ( ) {
7485 element = $compile ( '<rating ng-model="rate" max="7"></rating>' ) ( $rootScope ) ;
7586 $rootScope . $digest ( ) ;
7687
7788 expect ( getStars ( ) . length ) . toBe ( 7 ) ;
89+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '7' ) ;
7890 } ) ;
7991
8092 it ( 'shows different number of icons when `max` attribute is from scope variable' , function ( ) {
8193 $rootScope . max = 15 ;
8294 element = $compile ( '<rating ng-model="rate" max="max"></rating>' ) ( $rootScope ) ;
8395 $rootScope . $digest ( ) ;
8496 expect ( getStars ( ) . length ) . toBe ( 15 ) ;
97+ expect ( element . attr ( 'aria-valuemax' ) ) . toBe ( '15' ) ;
8598 } ) ;
8699
87100 it ( 'handles readonly attribute' , function ( ) {
@@ -124,6 +137,43 @@ describe('rating directive', function () {
124137 expect ( $rootScope . leaving ) . toHaveBeenCalled ( ) ;
125138 } ) ;
126139
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+
127177 describe ( 'custom states' , function ( ) {
128178 beforeEach ( inject ( function ( ) {
129179 $rootScope . classOn = 'icon-ok-sign' ;
@@ -150,7 +200,8 @@ describe('rating directive', function () {
150200 } ) ) ;
151201
152202 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' ) ;
154205 } ) ;
155206
156207 it ( 'handles each icon' , function ( ) {
0 commit comments