@@ -28,7 +28,7 @@ import {createKeyboardEvent} from '@angular/cdk/testing/testbed/fake-events';
28
28
* This is a cloned version of `tree.spec.ts` that contains all the same tests,
29
29
* but modifies them to use the newer API.
30
30
*/
31
- describe ( 'CdkTree redesign ' , ( ) => {
31
+ describe ( 'CdkTree' , ( ) => {
32
32
/** Represents an indent for expectNestedTreeToMatch */
33
33
const _ = { } ;
34
34
let dataSource : FakeDataSource ;
@@ -61,34 +61,36 @@ describe('CdkTree redesign', () => {
61
61
} ) . compileComponents ( ) ;
62
62
}
63
63
64
- it ( 'should clear out the `mostRecentTreeNode` on destroy' , ( ) => {
65
- configureCdkTreeTestingModule ( [ SimpleCdkTreeApp ] ) ;
66
- const fixture = TestBed . createComponent ( SimpleCdkTreeApp ) ;
67
- fixture . detectChanges ( ) ;
64
+ describe ( 'onDestroy' , ( ) => {
65
+ it ( 'should clear out the `mostRecentTreeNode` on destroy' , ( ) => {
66
+ configureCdkTreeTestingModule ( [ SimpleCdkTreeApp ] ) ;
67
+ const fixture = TestBed . createComponent ( SimpleCdkTreeApp ) ;
68
+ fixture . detectChanges ( ) ;
68
69
69
- // Cast the assertions to a boolean to avoid Jasmine going into an
70
- // infinite loop when stringifying the object, if the test starts failing.
71
- expect ( ! ! CdkTreeNode . mostRecentTreeNode ) . toBe ( true ) ;
70
+ // Cast the assertions to a boolean to avoid Jasmine going into an
71
+ // infinite loop when stringifying the object, if the test starts failing.
72
+ expect ( ! ! CdkTreeNode . mostRecentTreeNode ) . toBe ( true ) ;
72
73
73
- fixture . destroy ( ) ;
74
+ fixture . destroy ( ) ;
74
75
75
- expect ( ! ! CdkTreeNode . mostRecentTreeNode ) . toBe ( false ) ;
76
- } ) ;
76
+ expect ( ! ! CdkTreeNode . mostRecentTreeNode ) . toBe ( false ) ;
77
+ } ) ;
77
78
78
- it ( 'should complete the viewChange stream on destroy' , ( ) => {
79
- configureCdkTreeTestingModule ( [ SimpleCdkTreeApp ] ) ;
80
- const fixture = TestBed . createComponent ( SimpleCdkTreeApp ) ;
81
- fixture . detectChanges ( ) ;
82
- const spy = jasmine . createSpy ( 'completeSpy' ) ;
83
- const subscription = fixture . componentInstance . tree . viewChange . subscribe ( { complete : spy } ) ;
79
+ it ( 'should complete the viewChange stream on destroy' , ( ) => {
80
+ configureCdkTreeTestingModule ( [ SimpleCdkTreeApp ] ) ;
81
+ const fixture = TestBed . createComponent ( SimpleCdkTreeApp ) ;
82
+ fixture . detectChanges ( ) ;
83
+ const spy = jasmine . createSpy ( 'completeSpy' ) ;
84
+ const subscription = fixture . componentInstance . tree . viewChange . subscribe ( { complete : spy } ) ;
84
85
85
- fixture . destroy ( ) ;
86
- expect ( spy ) . toHaveBeenCalled ( ) ;
87
- subscription . unsubscribe ( ) ;
86
+ fixture . destroy ( ) ;
87
+ expect ( spy ) . toHaveBeenCalled ( ) ;
88
+ subscription . unsubscribe ( ) ;
89
+ } ) ;
88
90
} ) ;
89
91
90
92
describe ( 'flat tree' , ( ) => {
91
- describe ( 'should initialize ' , ( ) => {
93
+ describe ( 'displaying a flat tree ' , ( ) => {
92
94
let fixture : ComponentFixture < SimpleCdkTreeApp > ;
93
95
let component : SimpleCdkTreeApp ;
94
96
@@ -104,19 +106,19 @@ describe('CdkTree redesign', () => {
104
106
treeElement = fixture . nativeElement . querySelector ( 'cdk-tree' ) ;
105
107
} ) ;
106
108
107
- it ( 'with a connected data source ' , ( ) => {
109
+ it ( 'connects the datasource ' , ( ) => {
108
110
expect ( tree . dataSource ) . toBe ( dataSource ) ;
109
111
expect ( dataSource . isConnected ) . toBe ( true ) ;
110
112
} ) ;
111
113
112
- it ( 'with rendered dataNodes ' , ( ) => {
114
+ it ( 'renders at least one node ' , ( ) => {
113
115
const nodes = getNodes ( treeElement ) ;
114
116
115
117
expect ( nodes ) . withContext ( 'Expect nodes to be defined' ) . toBeDefined ( ) ;
116
118
expect ( nodes [ 0 ] . classList ) . toContain ( 'customNodeClass' ) ;
117
119
} ) ;
118
120
119
- it ( 'with the right data ' , ( ) => {
121
+ it ( 'renders nodes that match the datasource ' , ( ) => {
120
122
expect ( dataSource . data . length ) . toBe ( 3 ) ;
121
123
122
124
let data = dataSource . data ;
@@ -145,7 +147,7 @@ describe('CdkTree redesign', () => {
145
147
) ;
146
148
} ) ;
147
149
148
- it ( 'should be able to use units different from px for the indentation ' , ( ) => {
150
+ it ( 'indents when given an indentation of 15rem ' , ( ) => {
149
151
component . indent = '15rem' ;
150
152
fixture . detectChanges ( ) ;
151
153
@@ -161,7 +163,7 @@ describe('CdkTree redesign', () => {
161
163
) ;
162
164
} ) ;
163
165
164
- it ( 'should default to px if no unit is set for string value indentation ' , ( ) => {
166
+ it ( 'indents in units of pixel when no unit is given ' , ( ) => {
165
167
component . indent = '17' ;
166
168
fixture . detectChanges ( ) ;
167
169
@@ -193,7 +195,7 @@ describe('CdkTree redesign', () => {
193
195
) ;
194
196
} ) ;
195
197
196
- it ( 'should reset the opposite direction padding if the direction changes' , ( ) => {
198
+ it ( 'should reset element.styel to the opposite direction padding if the direction changes' , ( ) => {
197
199
const node = getNodes ( treeElement ) [ 0 ] ;
198
200
199
201
component . indent = 10 ;
@@ -770,7 +772,7 @@ describe('CdkTree redesign', () => {
770
772
} ) ;
771
773
772
774
it ( 'with the right aria-expanded attrs' , ( ) => {
773
- expect ( getNodeAttributes ( getNodes ( treeElement ) , 'aria-expanded' ) )
775
+ expect ( getNodes ( treeElement ) . map ( x => x . getAttribute ( 'aria-expanded' ) ) )
774
776
. withContext ( 'aria-expanded attributes' )
775
777
. toEqual ( [ null , null , null ] ) ;
776
778
@@ -785,7 +787,7 @@ describe('CdkTree redesign', () => {
785
787
786
788
// NB: only four elements are present here; children are not present
787
789
// in DOM unless the parent node is expanded.
788
- expect ( getNodeAttributes ( getNodes ( treeElement ) , 'aria-expanded' ) )
790
+ expect ( getNodes ( treeElement ) . map ( x => x . getAttribute ( 'aria-expanded' ) ) )
789
791
. withContext ( 'aria-expanded attributes' )
790
792
. toEqual ( [ null , 'true' , 'false' , null ] ) ;
791
793
} ) ;
@@ -1150,39 +1152,58 @@ describe('CdkTree redesign', () => {
1150
1152
} ) ;
1151
1153
1152
1154
describe ( 'focus management' , ( ) => {
1153
- it ( 'the tree is tabbable when no element is active' , ( ) => {
1154
- expect ( treeElement . getAttribute ( 'tabindex' ) ) . toBe ( '0' ) ;
1155
+ beforeEach ( ( ) => {
1156
+ dataSource . clear ( ) ;
1157
+ dataSource . data = [
1158
+ new TestData ( 'cheese' ) ,
1159
+ new TestData ( 'pepperoni' ) ,
1160
+ new TestData ( 'anchovie' ) ,
1161
+ ] ;
1162
+ fixture . detectChanges ( ) ;
1163
+ nodes = getNodes ( treeElement ) ;
1164
+ } ) ;
1165
+
1166
+ it ( 'the tree does not have tabindex attribute' , ( ) => {
1167
+ expect ( treeElement . hasAttribute ( 'tabindex' ) ) . toBeFalse ( ) ;
1155
1168
} ) ;
1156
1169
1157
- it ( 'the tree is not tabbable when an element is active' , ( ) => {
1170
+ it ( 'the tree does not have a tabindex when an element is active' , ( ) => {
1158
1171
// activate the second child by clicking on it
1159
1172
nodes [ 1 ] . click ( ) ;
1173
+ fixture . detectChanges ( ) ;
1160
1174
1161
- expect ( treeElement . getAttribute ( 'tabindex' ) ) . toBe ( null ) ;
1175
+ expect ( treeElement . hasAttribute ( 'tabindex' ) ) . toBeFalse ( ) ;
1176
+ } ) ;
1177
+
1178
+ it ( 'sets the tabindex to the first item by default' , ( ) => {
1179
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '0,-1,-1' ) ;
1180
+ } ) ;
1181
+
1182
+ it ( 'set the tabindex to the first item that is not disabled' , ( ) => {
1183
+ dataSource . data [ 0 ] . isDisabled = true ;
1184
+ fixture . detectChanges ( ) ;
1185
+
1186
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '-1,0,-1' ) ;
1162
1187
} ) ;
1163
1188
1164
1189
it ( 'sets tabindex on the latest activated item, with all others "-1"' , ( ) => {
1165
1190
// activate the second child by clicking on it
1166
1191
nodes [ 1 ] . click ( ) ;
1192
+ fixture . detectChanges ( ) ;
1167
1193
1168
- expect ( getNodeAttributes ( nodes , 'tabindex' ) ) . toEqual ( [ '-1' , '0' , '-1' , '-1' , '-1' , '-1' ] ) ;
1169
-
1170
- // activate the first child by clicking on it
1171
- nodes [ 0 ] . click ( ) ;
1172
-
1173
- expect ( getNodeAttributes ( nodes , 'tabindex' ) ) . toEqual ( [ '0' , '-1' , '-1' , '-1' , '-1' , '-1' ] ) ;
1194
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '-1,0,-1' ) ;
1174
1195
} ) ;
1175
1196
1176
1197
it ( 'maintains tabindex when a node is programatically focused' , ( ) => {
1177
1198
// activate the second child by programatically focusing it
1178
1199
nodes [ 1 ] . focus ( ) ;
1179
1200
1180
- expect ( getNodeAttributes ( nodes , 'tabindex' ) ) . toEqual ( [ '-1' , '0' , '-1' , '-1' , ' -1', '-1' ] ) ;
1201
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '-1,0, -1') ;
1181
1202
1182
1203
// activate the first child by programatically focusing it
1183
1204
nodes [ 0 ] . focus ( ) ;
1184
1205
1185
- expect ( getNodeAttributes ( nodes , 'tabindex' ) ) . toEqual ( [ '0' , '-1' , '-1' , ' -1', '-1' , '-1' ] ) ;
1206
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '0,-1, -1') ;
1186
1207
} ) ;
1187
1208
1188
1209
it ( 'maintains tabindex when component is blurred' , ( ) => {
@@ -1194,7 +1215,7 @@ describe('CdkTree redesign', () => {
1194
1215
nodes [ 1 ] . blur ( ) ;
1195
1216
1196
1217
expect ( treeElement . getAttribute ( 'tabindex' ) ) . toBe ( null ) ;
1197
- expect ( getNodeAttributes ( nodes , 'tabindex' ) ) . toEqual ( [ '-1' , '0' , '-1' , '-1' , ' -1', '-1' ] ) ;
1218
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '-1,0, -1') ;
1198
1219
} ) ;
1199
1220
1200
1221
it ( 'ignores clicks on disabled items' , ( ) => {
@@ -1205,7 +1226,8 @@ describe('CdkTree redesign', () => {
1205
1226
nodes [ 0 ] . click ( ) ;
1206
1227
1207
1228
expect ( treeElement . getAttribute ( 'tabindex' ) ) . toBe ( '0' ) ;
1208
- expect ( getNodeAttributes ( nodes , 'tabindex' ) ) . toEqual ( [ '-1' , '-1' , '-1' , '-1' , '-1' , '-1' ] ) ;
1229
+
1230
+ expect ( nodes . map ( x => x . getAttribute ( 'tabindex' ) ) . join ( ',' ) ) . toEqual ( '-1,0,-1' ) ;
1209
1231
} ) ;
1210
1232
1211
1233
describe ( 'when no item is currently active' , ( ) => {
@@ -1232,7 +1254,7 @@ describe('CdkTree redesign', () => {
1232
1254
} ) ;
1233
1255
1234
1256
it ( 'sets the treeitem role on all nodes' , ( ) => {
1235
- expect ( getNodeAttributes ( nodes , 'role' ) ) . toEqual ( [
1257
+ expect ( nodes . map ( x => x . getAttribute ( 'role' ) ) ) . toEqual ( [
1236
1258
'treeitem' ,
1237
1259
'treeitem' ,
1238
1260
'treeitem' ,
@@ -1243,30 +1265,30 @@ describe('CdkTree redesign', () => {
1243
1265
} ) ;
1244
1266
1245
1267
it ( 'sets aria attributes for tree nodes' , ( ) => {
1246
- expect ( getNodeAttributes ( nodes , 'aria-expanded' ) )
1268
+ expect ( nodes . map ( x => x . getAttribute ( 'aria-expanded' ) ) )
1247
1269
. withContext ( 'aria-expanded attributes' )
1248
1270
. toEqual ( [ null , 'false' , 'false' , null , null , null ] ) ;
1249
- expect ( getNodeAttributes ( nodes , 'aria-level' ) )
1271
+ expect ( nodes . map ( x => x . getAttribute ( 'aria-level' ) ) )
1250
1272
. withContext ( 'aria-level attributes' )
1251
1273
. toEqual ( [ '1' , '1' , '2' , '3' , '3' , '1' ] ) ;
1252
- expect ( getNodeAttributes ( nodes , 'aria-posinset' ) )
1274
+ expect ( nodes . map ( x => x . getAttribute ( 'aria-posinset' ) ) )
1253
1275
. withContext ( 'aria-posinset attributes' )
1254
1276
. toEqual ( [ '1' , '2' , '1' , '1' , '2' , '3' ] ) ;
1255
- expect ( getNodeAttributes ( nodes , 'aria-setsize' ) )
1277
+ expect ( nodes . map ( x => x . getAttribute ( 'aria-setsize' ) ) )
1256
1278
. withContext ( 'aria-setsize attributes' )
1257
1279
. toEqual ( [ '3' , '3' , '1' , '2' , '2' , '3' ] ) ;
1258
1280
} ) ;
1259
1281
1260
1282
it ( 'changes aria-expanded status when expanded or collapsed' , ( ) => {
1261
1283
tree . expand ( dataSource . data [ 1 ] ) ;
1262
1284
fixture . detectChanges ( ) ;
1263
- expect ( getNodeAttributes ( nodes , 'aria-expanded' ) )
1285
+ expect ( nodes . map ( x => x . getAttribute ( 'aria-expanded' ) ) )
1264
1286
. withContext ( 'aria-expanded attributes' )
1265
1287
. toEqual ( [ null , 'true' , 'false' , null , null , null ] ) ;
1266
1288
1267
1289
tree . collapse ( dataSource . data [ 1 ] ) ;
1268
1290
fixture . detectChanges ( ) ;
1269
- expect ( getNodeAttributes ( nodes , 'aria-expanded' ) )
1291
+ expect ( nodes . map ( x => x . getAttribute ( 'aria-expanded' ) ) )
1270
1292
. withContext ( 'aria-expanded attributes' )
1271
1293
. toEqual ( [ null , 'false' , 'false' , null , null , null ] ) ;
1272
1294
} ) ;
@@ -1283,7 +1305,7 @@ export class TestData {
1283
1305
isDisabled ?: boolean ;
1284
1306
readonly observableChildren : BehaviorSubject < TestData [ ] > ;
1285
1307
1286
- constructor ( pizzaTopping : string , pizzaCheese : string , pizzaBase : string , level : number = 1 ) {
1308
+ constructor ( pizzaTopping : string , pizzaCheese = '' , pizzaBase = '' , level : number = 1 ) {
1287
1309
this . pizzaTopping = pizzaTopping ;
1288
1310
this . pizzaCheese = pizzaCheese ;
1289
1311
this . pizzaBase = pizzaBase ;
@@ -1466,10 +1488,6 @@ function expectNestedTreeToMatch(treeElement: Element, ...expectedTree: any[]) {
1466
1488
}
1467
1489
}
1468
1490
1469
- function getNodeAttributes ( nodes : HTMLElement [ ] , attribute : string ) {
1470
- return nodes . map ( node => node . getAttribute ( attribute ) ) ;
1471
- }
1472
-
1473
1491
@Component ( {
1474
1492
template : `
1475
1493
<cdk-tree [dataSource]="dataSource" [levelAccessor]="getLevel"
0 commit comments