1
1
import { Component , ElementRef , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
2
2
import { of } from 'rxjs' ;
3
- import { MatTree } from './tree' ;
4
3
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
5
4
import { MatTreeModule } from './tree-module' ;
6
5
import { NOOP_TREE_KEY_MANAGER_FACTORY_PROVIDER } from '@angular/cdk/a11y' ;
6
+ import { DOWN_ARROW } from '@angular/cdk/keycodes' ;
7
+ import { createKeyboardEvent } from '@angular/cdk/testing/private' ;
7
8
8
9
describe ( 'MatTree when provided LegacyTreeKeyManager' , ( ) => {
9
10
let fixture : ComponentFixture < SimpleMatTreeApp > ;
@@ -19,7 +20,7 @@ describe('MatTree when provided LegacyTreeKeyManager', () => {
19
20
fixture . detectChanges ( ) ;
20
21
} ) ;
21
22
22
- describe ( 'when nodes do not have a tabindex set ' , ( ) => {
23
+ describe ( 'when nodes have default options ' , ( ) => {
23
24
it ( 'Should render tabindex attribute of 0' , ( ) => {
24
25
const treeItems = fixture . componentInstance . treeNodes ;
25
26
@@ -30,10 +31,12 @@ describe('MatTree when provided LegacyTreeKeyManager', () => {
30
31
} ) ;
31
32
32
33
describe ( 'when nodes have TabIndex Input binding of 42' , ( ) => {
33
- it ( 'Should render tabindex attribute of 42.' , ( ) => {
34
+ beforeEach ( ( ) => {
34
35
fixture . componentInstance . tabIndexInputBinding = 42 ;
35
36
fixture . detectChanges ( ) ;
37
+ } ) ;
36
38
39
+ it ( 'Should render tabindex attribute of 42.' , ( ) => {
37
40
const treeItems = fixture . componentInstance . treeNodes ;
38
41
39
42
expect ( treeItems . map ( x => `${ x . nativeElement . getAttribute ( 'tabindex' ) } ` ) . join ( ', ' ) )
@@ -43,17 +46,40 @@ describe('MatTree when provided LegacyTreeKeyManager', () => {
43
46
} ) ;
44
47
45
48
describe ( 'when nodes have tabindex attribute binding of 2' , ( ) => {
46
- it ( 'should render tabindex attribute of 2' , ( ) => {
49
+ beforeEach ( ( ) => {
47
50
fixture . componentInstance . tabindexAttributeBinding = '2' ;
48
51
fixture . detectChanges ( ) ;
52
+ } ) ;
49
53
54
+ it ( 'should render tabindex attribute of 2' , ( ) => {
50
55
const treeItems = fixture . componentInstance . treeNodes ;
51
56
52
57
expect ( treeItems . map ( x => `${ x . nativeElement . getAttribute ( 'tabindex' ) } ` ) . join ( ', ' ) )
53
58
. withContext ( 'tabindex of tree nodes' )
54
59
. toEqual ( '2, 2, 2' ) ;
55
60
} ) ;
56
61
} ) ;
62
+
63
+ describe ( 'when pressing down arrow key' , ( ) => {
64
+ beforeEach ( ( ) => {
65
+ const treeElement = fixture . componentInstance . tree . nativeElement ;
66
+
67
+ treeElement . dispatchEvent ( createKeyboardEvent ( 'keydown' , DOWN_ARROW , 'down' ) ) ;
68
+ fixture . detectChanges ( ) ;
69
+ } ) ;
70
+
71
+ it ( 'should not change the active element' , ( ) => {
72
+ expect ( document . activeElement ) . toEqual ( document . body ) ;
73
+ } ) ;
74
+
75
+ it ( 'should not change the tabindex of tree nodes' , ( ) => {
76
+ const treeItems = fixture . componentInstance . treeNodes ;
77
+
78
+ expect ( treeItems . map ( x => `${ x . nativeElement . getAttribute ( 'tabindex' ) } ` ) . join ( ', ' ) )
79
+ . withContext ( 'tabindex of tree nodes' )
80
+ . toEqual ( '0, 0, 0' ) ;
81
+ } ) ;
82
+ } ) ;
57
83
} ) ;
58
84
59
85
class MinimalTestData {
@@ -63,7 +89,7 @@ class MinimalTestData {
63
89
64
90
@Component ( {
65
91
template : `
66
- <mat-tree [dataSource]="dataSource" [childrenAccessor]="getChildren">
92
+ <mat-tree #tree [dataSource]="dataSource" [childrenAccessor]="getChildren">
67
93
<mat-tree-node #node *matTreeNodeDef="let node"
68
94
[tabIndex]="tabIndexInputBinding" tabindex="{{tabindexAttributeBinding}}">
69
95
{{node.name}}
@@ -86,6 +112,6 @@ class SimpleMatTreeApp {
86
112
/** Value passed to tabindex attribute binding of each tree node. Null by default. */
87
113
tabindexAttributeBinding : string | null = null ;
88
114
89
- @ViewChild ( MatTree ) tree : MatTree < MinimalTestData > ;
115
+ @ViewChild ( 'tree' , { read : ElementRef } ) tree : ElementRef < HTMLElement > ;
90
116
@ViewChildren ( 'node' ) treeNodes : QueryList < ElementRef < HTMLElement > > ;
91
117
}
0 commit comments