Skip to content

Commit 32e1c74

Browse files
committed
* update tests
* delete getNodeAttributes
1 parent 66a8ad3 commit 32e1c74

File tree

1 file changed

+73
-55
lines changed

1 file changed

+73
-55
lines changed

src/cdk/tree/tree.spec.ts

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {createKeyboardEvent} from '@angular/cdk/testing/testbed/fake-events';
2828
* This is a cloned version of `tree.spec.ts` that contains all the same tests,
2929
* but modifies them to use the newer API.
3030
*/
31-
describe('CdkTree redesign', () => {
31+
describe('CdkTree', () => {
3232
/** Represents an indent for expectNestedTreeToMatch */
3333
const _ = {};
3434
let dataSource: FakeDataSource;
@@ -61,34 +61,36 @@ describe('CdkTree redesign', () => {
6161
}).compileComponents();
6262
}
6363

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();
6869

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);
7273

73-
fixture.destroy();
74+
fixture.destroy();
7475

75-
expect(!!CdkTreeNode.mostRecentTreeNode).toBe(false);
76-
});
76+
expect(!!CdkTreeNode.mostRecentTreeNode).toBe(false);
77+
});
7778

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});
8485

85-
fixture.destroy();
86-
expect(spy).toHaveBeenCalled();
87-
subscription.unsubscribe();
86+
fixture.destroy();
87+
expect(spy).toHaveBeenCalled();
88+
subscription.unsubscribe();
89+
});
8890
});
8991

9092
describe('flat tree', () => {
91-
describe('should initialize', () => {
93+
describe('displaying a flat tree', () => {
9294
let fixture: ComponentFixture<SimpleCdkTreeApp>;
9395
let component: SimpleCdkTreeApp;
9496

@@ -104,19 +106,19 @@ describe('CdkTree redesign', () => {
104106
treeElement = fixture.nativeElement.querySelector('cdk-tree');
105107
});
106108

107-
it('with a connected data source', () => {
109+
it('connects the datasource', () => {
108110
expect(tree.dataSource).toBe(dataSource);
109111
expect(dataSource.isConnected).toBe(true);
110112
});
111113

112-
it('with rendered dataNodes', () => {
114+
it('renders at least one node', () => {
113115
const nodes = getNodes(treeElement);
114116

115117
expect(nodes).withContext('Expect nodes to be defined').toBeDefined();
116118
expect(nodes[0].classList).toContain('customNodeClass');
117119
});
118120

119-
it('with the right data', () => {
121+
it('renders nodes that match the datasource', () => {
120122
expect(dataSource.data.length).toBe(3);
121123

122124
let data = dataSource.data;
@@ -145,7 +147,7 @@ describe('CdkTree redesign', () => {
145147
);
146148
});
147149

148-
it('should be able to use units different from px for the indentation', () => {
150+
it('indents when given an indentation of 15rem', () => {
149151
component.indent = '15rem';
150152
fixture.detectChanges();
151153

@@ -161,7 +163,7 @@ describe('CdkTree redesign', () => {
161163
);
162164
});
163165

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', () => {
165167
component.indent = '17';
166168
fixture.detectChanges();
167169

@@ -193,7 +195,7 @@ describe('CdkTree redesign', () => {
193195
);
194196
});
195197

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', () => {
197199
const node = getNodes(treeElement)[0];
198200

199201
component.indent = 10;
@@ -770,7 +772,7 @@ describe('CdkTree redesign', () => {
770772
});
771773

772774
it('with the right aria-expanded attrs', () => {
773-
expect(getNodeAttributes(getNodes(treeElement), 'aria-expanded'))
775+
expect(getNodes(treeElement).map(x => x.getAttribute('aria-expanded')))
774776
.withContext('aria-expanded attributes')
775777
.toEqual([null, null, null]);
776778

@@ -785,7 +787,7 @@ describe('CdkTree redesign', () => {
785787

786788
// NB: only four elements are present here; children are not present
787789
// 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')))
789791
.withContext('aria-expanded attributes')
790792
.toEqual([null, 'true', 'false', null]);
791793
});
@@ -1150,39 +1152,58 @@ describe('CdkTree redesign', () => {
11501152
});
11511153

11521154
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();
11551168
});
11561169

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', () => {
11581171
// activate the second child by clicking on it
11591172
nodes[1].click();
1173+
fixture.detectChanges();
11601174

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');
11621187
});
11631188

11641189
it('sets tabindex on the latest activated item, with all others "-1"', () => {
11651190
// activate the second child by clicking on it
11661191
nodes[1].click();
1192+
fixture.detectChanges();
11671193

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');
11741195
});
11751196

11761197
it('maintains tabindex when a node is programatically focused', () => {
11771198
// activate the second child by programatically focusing it
11781199
nodes[1].focus();
11791200

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');
11811202

11821203
// activate the first child by programatically focusing it
11831204
nodes[0].focus();
11841205

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');
11861207
});
11871208

11881209
it('maintains tabindex when component is blurred', () => {
@@ -1194,7 +1215,7 @@ describe('CdkTree redesign', () => {
11941215
nodes[1].blur();
11951216

11961217
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');
11981219
});
11991220

12001221
it('ignores clicks on disabled items', () => {
@@ -1205,7 +1226,8 @@ describe('CdkTree redesign', () => {
12051226
nodes[0].click();
12061227

12071228
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');
12091231
});
12101232

12111233
describe('when no item is currently active', () => {
@@ -1232,7 +1254,7 @@ describe('CdkTree redesign', () => {
12321254
});
12331255

12341256
it('sets the treeitem role on all nodes', () => {
1235-
expect(getNodeAttributes(nodes, 'role')).toEqual([
1257+
expect(nodes.map(x => x.getAttribute('role'))).toEqual([
12361258
'treeitem',
12371259
'treeitem',
12381260
'treeitem',
@@ -1243,30 +1265,30 @@ describe('CdkTree redesign', () => {
12431265
});
12441266

12451267
it('sets aria attributes for tree nodes', () => {
1246-
expect(getNodeAttributes(nodes, 'aria-expanded'))
1268+
expect(nodes.map(x => x.getAttribute('aria-expanded')))
12471269
.withContext('aria-expanded attributes')
12481270
.toEqual([null, 'false', 'false', null, null, null]);
1249-
expect(getNodeAttributes(nodes, 'aria-level'))
1271+
expect(nodes.map(x => x.getAttribute('aria-level')))
12501272
.withContext('aria-level attributes')
12511273
.toEqual(['1', '1', '2', '3', '3', '1']);
1252-
expect(getNodeAttributes(nodes, 'aria-posinset'))
1274+
expect(nodes.map(x => x.getAttribute('aria-posinset')))
12531275
.withContext('aria-posinset attributes')
12541276
.toEqual(['1', '2', '1', '1', '2', '3']);
1255-
expect(getNodeAttributes(nodes, 'aria-setsize'))
1277+
expect(nodes.map(x => x.getAttribute('aria-setsize')))
12561278
.withContext('aria-setsize attributes')
12571279
.toEqual(['3', '3', '1', '2', '2', '3']);
12581280
});
12591281

12601282
it('changes aria-expanded status when expanded or collapsed', () => {
12611283
tree.expand(dataSource.data[1]);
12621284
fixture.detectChanges();
1263-
expect(getNodeAttributes(nodes, 'aria-expanded'))
1285+
expect(nodes.map(x => x.getAttribute('aria-expanded')))
12641286
.withContext('aria-expanded attributes')
12651287
.toEqual([null, 'true', 'false', null, null, null]);
12661288

12671289
tree.collapse(dataSource.data[1]);
12681290
fixture.detectChanges();
1269-
expect(getNodeAttributes(nodes, 'aria-expanded'))
1291+
expect(nodes.map(x => x.getAttribute('aria-expanded')))
12701292
.withContext('aria-expanded attributes')
12711293
.toEqual([null, 'false', 'false', null, null, null]);
12721294
});
@@ -1283,7 +1305,7 @@ export class TestData {
12831305
isDisabled?: boolean;
12841306
readonly observableChildren: BehaviorSubject<TestData[]>;
12851307

1286-
constructor(pizzaTopping: string, pizzaCheese: string, pizzaBase: string, level: number = 1) {
1308+
constructor(pizzaTopping: string, pizzaCheese = '', pizzaBase = '', level: number = 1) {
12871309
this.pizzaTopping = pizzaTopping;
12881310
this.pizzaCheese = pizzaCheese;
12891311
this.pizzaBase = pizzaBase;
@@ -1466,10 +1488,6 @@ function expectNestedTreeToMatch(treeElement: Element, ...expectedTree: any[]) {
14661488
}
14671489
}
14681490

1469-
function getNodeAttributes(nodes: HTMLElement[], attribute: string) {
1470-
return nodes.map(node => node.getAttribute(attribute));
1471-
}
1472-
14731491
@Component({
14741492
template: `
14751493
<cdk-tree [dataSource]="dataSource" [levelAccessor]="getLevel"

0 commit comments

Comments
 (0)