search

Home  >  Q&A  >  body text

javascript - Using vue official scaffolding for unit testing, how to cover variables in watch?

The watch in the component is as follows:

player(isPlay) {
  if (isPlay) {
    this.playState = 'play'
  } else {
    this.playState = 'pause'
  }
}

The relevant use cases in the test file Home.spec.js are as follows

it('播放状态切换', () => {
  const Constructor = Vue.extend(Home)
  const vm = new Constructor().$mount()
  vm.playerShow = true
  Vue.nextTick( () => {
    expect(vm.playState).to.equal("play")
  })
  vm.playerShow = false
  Vue.nextTick( () => {
    expect(vm.playState).to.equal("pause")
    done()
  })
})

After writing this, check the coverage report and find that the watch part of the code in the component is not covered (all are red)
Please tell me how to write a use case to cover the watch code

typechotypecho2711 days ago1089

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-06-27 09:21:20

    Although I don’t know if this is the right approach, after writing it this way, the watch code will be covered...

    it('播放状态切换', () => {
      const Constructor = Vue.extend(Home)
      const vm = new Constructor().$mount()
      vm._watchers[0].cb(true)
      Vue.nextTick( () => {
        expect(vm.playState).to.equal("play")
      })
      vm._watchers[0].cb(false)
      Vue.nextTick( () => {
        expect(vm.playState).to.equal("pause")
        done()
      })
    })

    reply
    0
  • Cancelreply