Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Vue的methods属性中测试嵌套函数?_Javascript_Vue.js_Jestjs_Vue Component_Vue Test Utils - Fatal编程技术网

Javascript 如何在Vue的methods属性中测试嵌套函数?

Javascript 如何在Vue的methods属性中测试嵌套函数?,javascript,vue.js,jestjs,vue-component,vue-test-utils,Javascript,Vue.js,Jestjs,Vue Component,Vue Test Utils,我有一个Vue组件和一个方法 methods: { launchOpinionLab () { initOpinionLab({ clientId: this.clientId, flow: this.flow, srcCorrelationId: this.srcCorrelationId }) window.OOo.inlineFeedbackShow() } initOpinionLa

我有一个Vue组件和一个方法

  methods: {
    launchOpinionLab () {
      initOpinionLab({
        clientId: this.clientId,
        flow: this.flow,
        srcCorrelationId: this.srcCorrelationId
      })
      window.OOo.inlineFeedbackShow()
    }
initOpinionLab()本身就是一个导出函数

initOpinionLab.js

/**
 * Initializes the OpinionLab library with custom data.
 *
 * @param {Object} data - any data we want to attach to user feedback submissions
 */
export default function initOpinionLab (data) {
  const opinionLab = window.OOo

  /**
   * Callback to launch the feedback comment card.
   * This assigns our custom data to the OpinionLab instance.
   *
   * @param {Event} event - window event
   */
  opinionLab.inlineFeedbackShow = (event) => {
    let replacePattern = '://' + window.location.host

    if (window.location.host !== 'checkout.mastercard.com') {
      replacePattern = '://test.checkout.mastercard.com'
    }

    opinionLab.oo_feedback = new opinionLab.Ocode({
      referrerRewrite: {
        searchPattern: /:\/\/[^/]*/,
        replacePattern: replacePattern
      },
      customVariables: data
    })

    // Now that oo_feedback has been re-initialized with the custom
    // var and context of the current page, launch the comment card
    opinionLab.oo_launch(event, 'oo_feedback')
  }

  /**
   * Launches opinionLab.
   *
   * @param {Event} event
   * @param {Object} feedback - opinionLab-structured feedback object
   */
  opinionLab.oo_launch = (event, feedback) => {
    opinionLab[feedback].show(event || window.event)
  }
}
it('[positive] initOpinionLab should be initialized with props' , () => {
    const initOpinionLab = jest.fn()
    jest.spyOn(wrapper.vm, 'initOpinionLab')
    wrapper.vm.launchOpinionLab()
    expect(initOpinionLab).toHaveBeenCalled()
  })
在我的单元测试的顶部是该模块的mock,
jest.mock('./initOpinionLab',()=>jest.fn())

我的目标是断言initOpinionLab({})是用正确的props调用的。当我试图监视它时,我得到“无法监视initOpinionLab属性,因为它不是一个函数;而是一个未定义的函数”。如何为此编写健壮的测试

或者,如果我将initOpinionLab()移动到mounted会怎么样?那我能更好地测试一下吗

组件规范js

/**
 * Initializes the OpinionLab library with custom data.
 *
 * @param {Object} data - any data we want to attach to user feedback submissions
 */
export default function initOpinionLab (data) {
  const opinionLab = window.OOo

  /**
   * Callback to launch the feedback comment card.
   * This assigns our custom data to the OpinionLab instance.
   *
   * @param {Event} event - window event
   */
  opinionLab.inlineFeedbackShow = (event) => {
    let replacePattern = '://' + window.location.host

    if (window.location.host !== 'checkout.mastercard.com') {
      replacePattern = '://test.checkout.mastercard.com'
    }

    opinionLab.oo_feedback = new opinionLab.Ocode({
      referrerRewrite: {
        searchPattern: /:\/\/[^/]*/,
        replacePattern: replacePattern
      },
      customVariables: data
    })

    // Now that oo_feedback has been re-initialized with the custom
    // var and context of the current page, launch the comment card
    opinionLab.oo_launch(event, 'oo_feedback')
  }

  /**
   * Launches opinionLab.
   *
   * @param {Event} event
   * @param {Object} feedback - opinionLab-structured feedback object
   */
  opinionLab.oo_launch = (event, feedback) => {
    opinionLab[feedback].show(event || window.event)
  }
}
it('[positive] initOpinionLab should be initialized with props' , () => {
    const initOpinionLab = jest.fn()
    jest.spyOn(wrapper.vm, 'initOpinionLab')
    wrapper.vm.launchOpinionLab()
    expect(initOpinionLab).toHaveBeenCalled()
  })