@@ -6,22 +6,24 @@ describe('ui.grid.utilService', function() {
6
6
$q ,
7
7
$interpolateProvider ;
8
8
9
- beforeEach ( module ( 'ui.grid' , function ( _$interpolateProvider_ ) {
10
- $interpolateProvider = _$interpolateProvider_ ;
11
- } ) ) ;
12
-
13
- beforeEach ( inject ( function ( _$rootScope_ , _$q_ , _gridUtil_ , _$window_ , _Grid_ ) {
14
- gridUtil = _gridUtil_ ;
15
- $window = _$window_ ;
16
- Grid = _Grid_ ;
17
- $rootScope = _$rootScope_ ;
18
- $q = _$q_ ;
19
- } ) ) ;
9
+ beforeEach ( function ( ) {
10
+ module ( 'ui.grid' , function ( _$interpolateProvider_ ) {
11
+ $interpolateProvider = _$interpolateProvider_ ;
12
+ } ) ;
13
+
14
+ inject ( function ( _$rootScope_ , _$q_ , _gridUtil_ , _$window_ , _Grid_ ) {
15
+ gridUtil = _gridUtil_ ;
16
+ $window = _$window_ ;
17
+ Grid = _Grid_ ;
18
+ $rootScope = _$rootScope_ ;
19
+ $q = _$q_ ;
20
+ } ) ;
21
+ } ) ;
20
22
21
23
describe ( 'newId()' , function ( ) {
22
24
it ( 'creates a unique id each time it is called' , function ( ) {
23
- var id1 = gridUtil . newId ( ) ;
24
- var id2 = gridUtil . newId ( ) ;
25
+ var id1 = gridUtil . newId ( ) ,
26
+ id2 = gridUtil . newId ( ) ;
25
27
26
28
expect ( id1 ) . not . toEqual ( id2 ) ;
27
29
} ) ;
@@ -532,12 +534,14 @@ describe('ui.grid.utilService', function() {
532
534
/* Create Button1 */
533
535
button1 = document . createElement ( 'button' ) ;
534
536
aButton1 = angular . element ( button1 ) ;
537
+ aButton1 . attr ( 'id' , 'aButton1' ) ;
535
538
aButton1 . attr ( 'type' , 'button' ) ;
536
539
// The class is not set here because it is set inside of tests if needed
537
540
538
541
/* Create Button2 */
539
542
button2 = document . createElement ( 'button' ) ;
540
543
aButton2 = angular . element ( button1 ) ;
544
+ aButton2 . attr ( 'id' , 'aButton2' ) ;
541
545
aButton2 . attr ( 'type' , 'button' ) ;
542
546
aButton2 . addClass ( button2class ) ;
543
547
@@ -557,14 +561,61 @@ describe('ui.grid.utilService', function() {
557
561
expect ( element . innerHTML ) . toEqual ( document . activeElement . innerHTML ) ;
558
562
}
559
563
564
+ describe ( 'byId' , function ( ) {
565
+ describe ( 'when the grid is not defined' , function ( ) {
566
+ it ( 'should focus on the element with the id passed' , function ( ) {
567
+ gridUtil . focus . byId ( 'aButton2' ) ;
568
+ $timeout . flush ( ) ;
569
+
570
+ expectFocused ( button2 ) ;
571
+ } ) ;
572
+ } ) ;
573
+ describe ( 'when the grid is defined' , function ( ) {
574
+ it ( 'should focus on the element with the grid id and the id passed' , function ( ) {
575
+ var gridId = 'gridId' ;
576
+
577
+ aButton2 . attr ( 'id' , 'gridId-aButton2' ) ;
578
+ gridUtil . focus . byId ( 'aButton2' , { id : gridId } ) ;
579
+ $timeout . flush ( ) ;
580
+
581
+ expectFocused ( button2 ) ;
582
+ } ) ;
583
+ } ) ;
584
+ describe ( 'when the id passed in is not in the dom' , function ( ) {
585
+ it ( 'should keep focus on the body' , function ( ) {
586
+ gridUtil . focus . byId ( 'notAnElement' ) ;
587
+ $timeout . flush ( ) ;
588
+
589
+ expectFocused ( document . body ) ;
590
+ } ) ;
591
+ } ) ;
592
+ } ) ;
560
593
describe ( 'byElement' , function ( ) {
561
- it ( 'should focus on the element passed' , function ( ) {
562
- gridUtil . focus . byElement ( button1 ) ;
563
- $timeout . flush ( ) ;
564
- expectFocused ( button1 ) ;
594
+ describe ( 'when argument passed is an element' , function ( ) {
595
+ it ( 'should focus on the element passed' , function ( ) {
596
+ gridUtil . focus . byElement ( button1 ) ;
597
+ $timeout . flush ( ) ;
598
+ expectFocused ( button1 ) ;
599
+ } ) ;
600
+ } ) ;
601
+ describe ( 'when argument passed is not an element' , function ( ) {
602
+ it ( 'should keep focus on the body' , function ( ) {
603
+ gridUtil . focus . byElement ( '' ) ;
604
+ expectFocused ( document . body ) ;
605
+ } ) ;
565
606
} ) ;
566
607
} ) ;
567
608
describe ( 'bySelector' , function ( ) {
609
+ describe ( 'when argument passed is not an element' , function ( ) {
610
+ it ( 'should throw an error' , function ( done ) {
611
+ try {
612
+ gridUtil . focus . bySelector ( '' ) ;
613
+ } catch ( error ) {
614
+ expect ( error . message ) . toEqual ( 'The parent element is not an element.' ) ;
615
+ done ( ) ;
616
+ }
617
+ } ) ;
618
+ } ) ;
568
619
it ( 'should focus on an elment using a selector' , function ( ) {
569
620
gridUtil . focus . bySelector ( elm , '.' + button2class ) ;
570
621
$timeout . flush ( ) ;
0 commit comments