Skip to content

Commit

Permalink
fix(cdk/tree): fix tests for zoneless
Browse files Browse the repository at this point in the history
  • Loading branch information
BobobUnicorn committed Jun 13, 2024
1 parent f22830c commit f407a87
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/cdk/tree/tree-with-tree-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ViewChild,
TrackByFunction,
Type,
provideZoneChangeDetection,
EventEmitter,
ViewChildren,
QueryList,
Expand All @@ -30,7 +29,7 @@ import {NestedTreeControl} from './control/nested-tree-control';
import {CdkTreeModule, CdkTreeNodePadding} from './index';
import {CdkTree, CdkTreeNode} from './tree';

describe('CdkTree', () => {
describe('CdkTree with TreeControl', () => {
/** Represents an indent for expectNestedTreeToMatch */
const _ = {};
let dataSource: FakeDataSource;
Expand All @@ -42,7 +41,6 @@ describe('CdkTree', () => {
TestBed.configureTestingModule({
imports: [CdkTreeModule],
providers: [
provideZoneChangeDetection(),
{
provide: Directionality,
useFactory: () => (dir = {value: 'ltr', change: new EventEmitter<Direction>()}),
Expand Down Expand Up @@ -150,6 +148,7 @@ describe('CdkTree', () => {

it('should be able to use units different from px for the indentation', () => {
component.indent = '15rem';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

const data = dataSource.data;
Expand All @@ -166,6 +165,7 @@ describe('CdkTree', () => {

it('should default to px if no unit is set for string value indentation', () => {
component.indent = '17';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

const data = dataSource.data;
Expand All @@ -182,6 +182,7 @@ describe('CdkTree', () => {

it('should be able to set zero as the indent level', () => {
component.paddingNodes.forEach(node => (node.level = 0));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

const data = dataSource.data;
Expand All @@ -200,6 +201,7 @@ describe('CdkTree', () => {
const node = getNodes(treeElement)[0];

component.indent = 10;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

expect(node.style.paddingLeft).toBe('10px');
Expand Down Expand Up @@ -391,6 +393,7 @@ describe('CdkTree', () => {
);

dataSource.addChild(data[1]);
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

treeElement = fixture.nativeElement.querySelector('cdk-tree');
Expand Down Expand Up @@ -771,6 +774,7 @@ describe('CdkTree', () => {
let data = dataSource.data;
const child = dataSource.addChild(data[1], false);
dataSource.addChild(child, false);
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

(getNodes(treeElement)[1] as HTMLElement).click();
Expand All @@ -793,6 +797,7 @@ describe('CdkTree', () => {
const child = dataSource.addChild(data[1], false);
dataSource.addChild(child, false);

fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

expectNestedTreeToMatch(
Expand Down Expand Up @@ -1192,6 +1197,7 @@ describe('CdkTree', () => {

it('ignores clicks on disabled items', () => {
dataSource.data[1].isDisabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

nodes[1].click();
Expand Down
1 change: 1 addition & 0 deletions src/cdk/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ describe('CdkTree', () => {

it('ignores clicks on disabled items', () => {
dataSource.data[1].isDisabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(nodes.map(x => x.getAttribute('tabindex')).join(', ')).toEqual('0, -1, -1');

Expand Down
12 changes: 6 additions & 6 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ import {
isObservable,
of as observableOf,
} from 'rxjs';
import {distinctUntilChanged,
import {
distinctUntilChanged,
concatMap,
map,
reduce,
Expand Down Expand Up @@ -351,7 +352,7 @@ export class CdkTree<T, K = T>
}
}

private _getExpansionModel() {
_getExpansionModel() {
if (!this.treeControl) {
this._expansionModel ??= new SelectionModel<K>(true);
return this._expansionModel;
Expand Down Expand Up @@ -1137,8 +1138,6 @@ export class CdkTree<T, K = T>
export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerItem {
protected _tabindex: number | null = -1;

_changeDetectorRef = inject(ChangeDetectorRef);

/**
* The role of the tree node.
*
Expand Down Expand Up @@ -1300,8 +1299,9 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI

ngOnInit(): void {
this._parentNodeAriaLevel = getParentNodeAriaLevel(this._elementRef.nativeElement);
this._tree._getExpansionModel().changed
.pipe(
this._tree
._getExpansionModel()
.changed.pipe(
map(() => this.isExpanded),
distinctUntilChanged(),
)
Expand Down

0 comments on commit f407a87

Please sign in to comment.