Acu*_*ute 5 angular-material2 angular angular-material-6
我在我的组件中使用cdk-virtual-scroll-viewport+cdkVirtualFor并且它似乎工作正常。
然而,在该组件的单元测试中,项目不会被渲染。
我基于此示例制作了一个示例应用程序,虽然该示例在您为该应用程序提供服务时有效,但我编写的测试失败了。
import { ScrollingModule } from '@angular/cdk/scrolling';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
ScrollingModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
import { Component, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
items = Array.from({length: 100000}).map((_, i) => `Item #${i}`);
}
Run Code Online (Sandbox Code Playgroud)
<cdk-virtual-scroll-viewport itemSize="50" class="example-viewport">
<div *cdkVirtualFor="let item of items" class="example-item">{{ item }}</div>
</cdk-virtual-scroll-viewport>
Run Code Online (Sandbox Code Playgroud)
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { ScrollingModule } from '@angular/cdk/scrolling';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
ScrollingModule,
],
}).compileComponents();
}));
it('should render at least 4 items', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement as HTMLElement;
expect(compiled.querySelectorAll('.example-item').length).toBeGreaterThanOrEqual(4); // <-- Error: Expected 0 to be greater than or equal 4.
});
});
Run Code Online (Sandbox Code Playgroud)
将单元测试(见问题)更改为以下解决了它:
it('should render at least 4 items', fakeAsync(() => { // <---
const fixture = TestBed.createComponent(AppComponent);
fixture.autoDetectChanges(); // <---
tick(500); // <---
const compiled = fixture.debugElement.nativeElement as HTMLElement;
expect(compiled.querySelectorAll('.example-item').length).toBeGreaterThanOrEqual(4);
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3037 次 |
| 最近记录: |