forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(material/tree): add unit tests for opt-out (angular#28237)
Add unit tests to cover the use of LegacyTreeKeyManager, which opts out of updated focus management behavior.
- Loading branch information
Showing
4 changed files
with
127 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {Component, ElementRef, QueryList, ViewChild, ViewChildren} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {of} from 'rxjs'; | ||
import {CdkTreeModule} from './tree-module'; | ||
import {NOOP_TREE_KEY_MANAGER_FACTORY_PROVIDER} from '@angular/cdk/a11y'; | ||
|
||
describe('CdkTree when provided LegacyTreeKeyManager', () => { | ||
let fixture: ComponentFixture<SimpleCdkTreeApp>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [CdkTreeModule], | ||
declarations: [SimpleCdkTreeApp], | ||
providers: [NOOP_TREE_KEY_MANAGER_FACTORY_PROVIDER], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(SimpleCdkTreeApp); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
describe('with default node options', () => { | ||
it('renders nodes with tabindex attribute of -1', () => { | ||
const treeItems = fixture.componentInstance.treeNodes; | ||
|
||
expect(treeItems.map(x => `${x.nativeElement.getAttribute('tabindex')}`).join(', ')) | ||
.withContext('tabindex of tree nodes') | ||
.toEqual('-1, -1'); | ||
}); | ||
}); | ||
|
||
describe('when focusing the second node', () => { | ||
beforeEach(() => { | ||
const treeItems = fixture.componentInstance.treeNodes; | ||
|
||
treeItems.get(1)!.nativeElement.focus(); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('does not change tabindex of nodes', () => { | ||
const treeItems = fixture.componentInstance.treeNodes; | ||
|
||
expect(treeItems.map(x => `${x.nativeElement.getAttribute('tabindex')}`).join(', ')) | ||
.withContext('tabindex of tree nodes') | ||
.toEqual('-1, -1'); | ||
}); | ||
}); | ||
|
||
describe('when clicking the second node', () => { | ||
beforeEach(() => { | ||
const treeItems = fixture.componentInstance.treeNodes; | ||
|
||
treeItems.get(1)!.nativeElement.click(); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('does not change active element', () => { | ||
expect(document.activeElement).toEqual(document.body); | ||
}); | ||
|
||
it('does not change tabindex of nodes', () => { | ||
const treeItems = fixture.componentInstance.treeNodes; | ||
|
||
expect(treeItems.map(x => `${x.nativeElement.getAttribute('tabindex')}`).join(', ')) | ||
.withContext('tabindex of tree nodes') | ||
.toEqual('-1, -1'); | ||
}); | ||
}); | ||
}); | ||
|
||
class MinimalTestData { | ||
constructor(public name: string) {} | ||
children: MinimalTestData[] = []; | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<cdk-tree #tree [dataSource]="dataSource" [childrenAccessor]="getChildren"> | ||
<cdk-tree-node #node *cdkTreeNodeDef="let node"> | ||
{{node.name}} | ||
</cdk-tree-node> | ||
</cdk-tree> | ||
`, | ||
}) | ||
class SimpleCdkTreeApp { | ||
isExpandable = (node: MinimalTestData) => node.children.length > 0; | ||
getChildren = (node: MinimalTestData) => node.children; | ||
|
||
dataSource = of([new MinimalTestData('apple'), new MinimalTestData('banana')]); | ||
|
||
@ViewChild('tree', {read: ElementRef}) tree: ElementRef<HTMLElement>; | ||
@ViewChildren('node') treeNodes: QueryList<ElementRef<HTMLElement>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters