第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何正確觸發(fā)更改事件以觸發(fā)單元測(cè)試中的功能?

如何正確觸發(fā)更改事件以觸發(fā)單元測(cè)試中的功能?

慕勒3428872 2021-04-26 16:46:01
我有一個(gè)復(fù)選框組件,由于Safari的錯(cuò)誤,我更改了@input=input() to @change='input'因?yàn)镾afari沒有輸入事件。復(fù)選框<template>  <div    class="checkbox">    <input      ....      @change="input">  </div></template><script>export default {  ....  methods: {    input () {      /**       * Input event on change       *       * @event input       * @type {Boolean}       */      this.$emit('input', this.$refs.checkbox.checked)    }  }}</script>單元測(cè)試  describe('...', () => {    beforeEach(async () => {      const input = wrapper.find('input')      jest.spyOn(wrapper.vm, 'input')      input.trigger('change') // told this is incorrect      jest.runAllTimers()    })    it('[positive] should emit an input event with the input\'s value', () => {      expect(wrapper.emitted().input).toBeTruthy()      expect(wrapper.emitted().input).toHaveLength(1)      expect(wrapper.emitted().input[0]).toEqual([false])    })    it('[positive] should call the input() method with the target value', () => {     // this is wrong also, because the expectation will always be true     wrapper.vm.input()      expect(wrapper.vm.input).toHaveBeenCalled()    })  })我應(yīng)該如何正確設(shè)置第二項(xiàng)測(cè)試?為什么在單元測(cè)試中input.trigger('change')是錯(cuò)誤的?
查看完整描述

1 回答

?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

我的第二項(xiàng)測(cè)試設(shè)置為始終為真。我稱這個(gè)功能


wrapper.vm.input()

然后斷言它將被調(diào)用,這將是正確的,因此它是一個(gè)不好的測(cè)試。


input.trigger('change')

...是正確的,那正是我擁有的地方。這是我的重構(gòu)測(cè)試:


  describe('when the checkbox state is changed', () => {

    let input

    beforeEach(() => {

      input = wrapper.find('input')

      jest.spyOn(wrapper.vm, 'input')

      jest.runAllTimers()

    })


    it('[positive] should emit an input event with the input\'s value', () => {

      input.trigger('change')

      expect(wrapper.emitted().input).toBeTruthy()

      expect(wrapper.emitted().input).toHaveLength(1)

      expect(wrapper.emitted().input[0]).toEqual([false])

    })

    it('[negative] should not emit an input event with the input\'s value', () => {

      input.trigger('input')

      expect(wrapper.emitted().input).toBeFalsy()

    })

  })


查看完整回答
反對(duì) 回復(fù) 2021-05-20
  • 1 回答
  • 0 關(guān)注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)