我的组件:
@Component({
selector: 'p-dropdown',
template: `
<mat-form-field *ngIf="settings">
<mat-select [value]="settings.selected" [compareWith]="compareFn" [placeholder]="placeholder">
<mat-option *ngFor="let option of settings.options" [value]="option"> {{option.name}} </mat-option>
</mat-select>
</mat-form-field>
我的测试:
fdescribe('Drodown Component', () => {
let component: TestHostComponent;
let fixture: ComponentFixture<TestHostComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestHostComponent, DropdownComponent],
imports:[materialModule,BrowserAnimationsModule]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestHostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should show placeholder', () => {
const el: HTMLElement = fixture.nativeElement;
fixture.detectChanges();
const p = el.querySelector('.mat-form-field-label');
expect(p.textContent).toEqual('name2');
});
});
@Component({
selector: `host-component`,
template: `<p-dropdown [settings]="settings"></p-dropdown>`
})
class TestHostComponent {
settings = {options:[{_id:1,name:'name1'}, {_id:2,name:'name2'}], selected:{_id:2,name:'name2'}}
}
“ p-dropdown”在投放时效果很好,但由于某些原因,在我的测试中不起作用。 在调试中,我也可以看到p-dropdown设置,所以我不明白问题出在哪里。