Как проверить вложенную функцию в свойстве методов в Vue? - PullRequest
0 голосов
/ 19 сентября 2019

У меня есть компонент 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)
  }
}

В верхней части моего модульного теста находится макет этого модуля, jest.mock('./initOpinionLab', () => jest.fn()).

Моя цель - утверждать, что initOpinionLab ({}) вызывается с правильными реквизитами.Когда я пытаюсь шпионить за ним, я получаю «Невозможно шпионить за свойством initOpinionLab, потому что оно не является функцией; вместо него задано неопределенное значение».Как мне написать надежный тест для этого?

Или, что, если я переместил initOpinionLab () в mount?Могу ли я лучше проверить это тогда?

component.spec.js

it('[positive] initOpinionLab should be initialized with props' , () => {
    const initOpinionLab = jest.fn()
    jest.spyOn(wrapper.vm, 'initOpinionLab')
    wrapper.vm.launchOpinionLab()
    expect(initOpinionLab).toHaveBeenCalled()
  })
...