【发布时间】:2019-07-23 04:09:30
【问题描述】:
我正在开发一个 Angular 应用程序,我正在使用 Jasmine 对应用程序进行单元测试。
应用程序将ngx-modal-dialog(enter link description here) 插件用于弹出对话框,例如作为动态组件的确认框。
我想要的是触发确认或取消的点击事件,无论用户选择什么。
弹出对话框的代码如下:
export class SomeComponent {
constructor(private modalService: ModalDialogService) {}
cancleEditConfirmDialog() {
this.modalService.openDialog(this.viewRef, {
title: 'Discard Changes ',
childComponent: SimpleModalComponent,
data: {
text: 'Changes will not be saved. Do you want to proceed?'
},
settings: {
closeButtonClass: 'close theme-icon-close'
},
actionButtons: [
{
text: 'Discard',
buttonClass: 'btn btn-success',
onAction: () => new Promise((resolve: any) => {
// invoke delete
// do something such as discard changes
resolve()
})
},
{
text: 'Cancel',
buttonClass: 'btn btn-danger',
onAction: () => new Promise((resolve: any) => {
// cancel and close popup
setTimeout(() => {
resolve();
}, 20);
})
}
]
});
}
}
我如何在点击事件中触发
onAction: => ()丢弃按钮 和取消按钮。
【问题讨论】:
-
如果您使用原始 ngx-modal-dialog 设置您的 TestBed,您只需使用打开对话框的元素上的 debugElem.triggerEventHandler(‚click') 打开模式,然后您就可以访问再次作为 debugElem 取消和丢弃按钮并使用 triggerEventHandler。如果 ngx-modal-dialog 有太多依赖项,你不想添加到测试中,因为你想对组件进行浅层测试,你可以模拟 ngx-modal-dialog
-
@Erbsenkoenig 感谢您的回复。我试过了,但它是一个引导组件,所以它仍然显示为
cannot find nativeElement of null。 -
你能告诉我如何模拟它
-
我会尝试做一个stackblitz。
-
你能帮我一个忙并编辑这个 stackblitz (stackblitz.com/edit/angular-xawnbl) 以便对话框正常工作。我以前没有使用过这个特定的模态对话框。之后我会做一些测试。
标签: javascript angular unit-testing jasmine jasmine-jquery