如何覆盖放置在数组中的方法
在我的一个组件之一中,我使用了动态形式,为了使该动态形式工作,我需要将表单元素作为组件文件的数组发送。另外,如果表单元素有一个事件要执行,我还需要通过数组发送事件,以使其动态呈现。 数组
defineTestForm(): void {
this.dynamicFormParams = {
mainFilters: [
{
filters: [
{
name: 'testDropdown',
inputType: 'dropdown',
width: '3',
label: 'test label',
placeholder: 'test placeholder',
required: true,
validators: [],
showClear: true,
showFilter: true,
options: this.someoptions,
change: (event, form) => {
this.somemethod(event.value);
}
},
{
name: 'testArea',
inputType: 'textarea',
width: '4',
label: 'Test',
placeholder: '',
required: false,
validators: [],
},
]
}
]
}
}
这是代码正常工作时的 ,代码覆盖范围不是。 更改
方法和this.somemethod
没有被覆盖。如何覆盖它?
这是我尝试的代码。但没有起作用
describe('#defineTestForm', () => {
it('should cover the method', () => {
spyOn(component, 'somemethod')
component.defineTestForm();
component?.dynamicFormParams?.mainFilters[0]?.filters[0]?.change({ value : 111 } as any, null) // Cannot invoke an object which is possibly 'undefined'
expect(component.somemethod).toHaveBeenCalled()
})
})
In one of my component, I used a dynamic form and for that dynamic form to work, I need to send the form elements as an array from component file. Also, if the form element has a event to be performed, I also need to send the event via array, so that it renders dynamically. Here is the array
defineTestForm(): void {
this.dynamicFormParams = {
mainFilters: [
{
filters: [
{
name: 'testDropdown',
inputType: 'dropdown',
width: '3',
label: 'test label',
placeholder: 'test placeholder',
required: true,
validators: [],
showClear: true,
showFilter: true,
options: this.someoptions,
change: (event, form) => {
this.somemethod(event.value);
}
},
{
name: 'testArea',
inputType: 'textarea',
width: '4',
label: 'Test',
placeholder: '',
required: false,
validators: [],
},
]
}
]
}
}
While the code is working fine, the code coverage is not. change
method and this.somemethod
is not getting covered. How to cover it?
here is the code that I tried. but didn't worked
describe('#defineTestForm', () => {
it('should cover the method', () => {
spyOn(component, 'somemethod')
component.defineTestForm();
component?.dynamicFormParams?.mainFilters[0]?.filters[0]?.change({ value : 111 } as any, null) // Cannot invoke an object which is possibly 'undefined'
expect(component.somemethod).toHaveBeenCalled()
})
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论