angular - Angular如何使用嵌套的ng-template测试嵌套组件?
问题描述
我想在 Angular 中制作可重用和可扩展的组件,所以我关注了这篇文章:https ://medium.com/javascript-everyday/reusable-components-with-configurable-templates-in-angular-3c55741c97f3 。
它工作得很好,但是在编写测试时我遇到了麻烦。我无法从ng-template
with内部访问任何 DOM 元素querySelector(...)
- 我得到null
.
<app-edit [dialogTitle]="'Edit'">
<ng-template editTopLayout>
<div fxLayout="row">
<div fxFlex fxLayoutAlign="end center">
<button id="btn-release" (click)="onRelease()" mat-stroked-button color="primary" [disabled]="buttonsDisabled">Release</button>
<button id="btn-reject" (click)="onReject()" mat-stroked-button color="warn" [disabled]="buttonsDisabled">Reject</button>
<button id="btn-cancel" (click)="onCancel()" mat-stroked-button [disabled]="buttonsDisabled">Cancel</button>
</div>
<div fxFlex="2"></div>
</div>
</ng-template>
</app-edit>
在上面的示例中,被测组件app-edit
通过EditTopLayoutDirective
.
我的目的是测试,单击某个按钮后是否执行了正确的操作,ng-template
但正如我所说,fixture.querySelector(...)
在测试中使用时,我得到null
.
我可以访问里面的按钮ng-template
以务实地单击它们吗?
编辑:这是测试文件
describe('MyTestedComponent', () => {
let component: MyTestedComponent;
let fixture: ComponentFixture<MyTestedComponent>;
let fb = new FormBuilder();
let configService: ConfigurationService;
let eventEmitterSpy;
let paymentServiceStub = {
rejectPayments: jasmine.createSpy('rejectPayments'),
releasePayments: jasmine.createSpy('releasePayments')
};
let dialogServiceStub = { confirmContinuation: jasmine.createSpy('confirmContinuation') };
let configSpy;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterTestingModule,
DemoMaterialModule,
FormsModule,
ReactiveFormsModule,
BrowserAnimationsModule
],
declarations: [
MyTestedComponent,
EditComponent,
MoneyComponent,
EditTopLayoutDirective
],
providers: [
TemplateService,
AccountService,
AlertService,
ConfigurationService,
{
provide: DialogService,
useValue: dialogServiceStub
},
{
provide: PaymentService,
useValue: paymentServiceStub
},
{
provide: FormBuilder,
useValue: fb
}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyTestedComponent);
component = fixture.componentInstance;
eventEmitterSpy = spyOn(component.action, 'emit');
component.payment = fakePayment;
configService = TestBed.get(ConfigurationService);
fixture.detectChanges();
});
afterEach(() => {
eventEmitterSpy.calls.reset();
dialogServiceStub.confirmContinuation.calls.reset();
paymentServiceStub.rejectPayments.calls.reset();
paymentServiceStub.releasePayments.calls.reset();
if (configSpy != null && configSpy.configuration != null)
configSpy.configuration.calls.reset();
})
});
在测试文件中,我想做这样的事情(但得到null
):
fixture.debugElement.nativeElement.querySelector('#btn-release')
解决方案
推荐阅读
- c# - 长 id 未正确转换
- objective-c - 如果我调用它的getter,为什么关联对象的弱属性不会被取消?
- three.js - Threejs:禁用某些对象的截锥体剔除
- java - Spring Bean 给出 NullPointerException 但不为空
- python - 在字符串之后生成随机数
- python-3.x - python sqlite3记录未使用占位符函数插入数据库
- tensorflow - 如何在我自己的 GCP VM 或 Jupterlab 实例中使用 TensorFlow Embeddings Projector?
- mysql - 在将数据添加到mysql数据库中的表之前检查
- reactjs - Gatsby 条件渲染组件
- android - 如何从android中的谷歌地图中删除所有标记和折线?