我最近开始学习单元测试,但是我仍然对测试的某些方面感到困惑。
我有一个父组件,其中包含呈现表的 Child 组件。
<section class="header" >
<h3>Parent Component</h3>
</section>
<app-mat-table
[data]="faculties"
[columns]="columns" <----- Child component
[countRecords]="length"
(pageEvent)='pageUpdate($event)'>
</app-mat-table>
</section>
@ViewChild
并从 Child 组件中调用方法。@Input
和@Output
参数?Observable
,我需要监视这项服务吗? 例如:
getRange(callback) {
setTimeout(() => this.isLoading = true);
this.apiService.getRecordsRange(this.entity, this.pageSize, this.pageIndex * this.pageSize)
.subscribe(data => {
callback(data);
this.isLoading = false;
});
}
ParentComponent.ts
export class ParentComponent {
@ViewChild(ChildComponent, {static: false}) table: ChildComponent;
faculties: Faculty[] = [];
columns: Column [] = [*some Data*];
length: number;
pageUpdate() {
this.table.getRange((data: Faculty[]) => this.faculties = data);
this.table.getCountRecords(response => this.length = response.numberOfRecords);
}
}
ChildComponent.html
<div class="table-container">
<table>
<tr *ngFor="let item of data;></tr>
</table>
</div>
ChildComponent.ts
export class ChildComponent implements AfterViewInit {
@Input() data: any[] = [];
@Input() columns: Column[];
@Input() countRecords: number;
@Output() pageEvent = new EventEmitter<PageEvent>();
constructor(private apiService: ApiService) {}
ngAfterViewInit(): void {
this.onPaginationChange({ *emit some stuff* });
}
getRange(callback) {
setTimeout(() => this.isLoading = true);
this.apiService.getRecordsRange(this.entity, this.pageSize, this.pageIndex * this.pageSize)
.subscribe(data => {
callback(data);
this.isLoading = false;
});
}