如何覆盖放置在数组中的方法

发布于 2025-02-09 08:10:39 字数 1518 浏览 2 评论 0原文

在我的一个组件之一中,我使用了动态形式,为了使该动态形式工作,我需要将表单元素作为组件文件的数组发送。另外,如果表单元素有一个事件要执行,我还需要通过数组发送事件,以使其动态呈现。 数组

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文