【发布时间】:2021-09-29 01:25:12
【问题描述】:
在 ControleValueAccessor 内部使用时如何测试 ngFor 元素。
@Component({
selector: 'checkbox-group',
templateUrl: `
<h3 class="tt">s</h3>
<ng-container *ngFor="let item of _model;let i = index" > <h3>s</h3>
<div>
<input type="checkbox" id="c{{i}}"/>
</div>
</ng-container>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxGroupComponent),
multi: true
}
]
})
export class CheckboxGroupComponent implements ControlValueAccessor {
constructor(private ref: ChangeDetectorRef) {
ref.detach();
}
writeValue(value: any): void {
this._model = value;
if(value != null) { this.ref.detectChanges();
}
}
并从父 settings.html 导入此控件值访问器:
<div>
<checkbox-group [(ngModel)]="selectedItems"></checkbox-group>
</div>
现在当我想测试它时,它只有一个子元素。并且只知道标签是第一个和最后一个子元素
setting.spec.ts:
TestBed.configureTestingModule({
declarations: [ SettingsPage, CheckboxGroupComponent ],
imports: [IonicModule.forRoot()],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxGroupComponent),
multi: true
}
// other providers
],
schemas: [ NO_ERRORS_SCHEMA ]
}).compileComponents();
it('should get checkBoxGroup Childs', fakeAsync(() => {
let customComponent = fixture.nativeElement.querySelector('checkbox-group');
component.selectedItems = [true,true,true];
fixture.detectChanges();
fixture.whenStable().then(() => {
customComponent.NgModule=[true,false,true];
fixture.detectChanges();
debugger;
});
}))
在 customComponent 上调试:
【问题讨论】:
标签: angular jestjs jasmine angular-test angular2-changedetection