javascript - Using vue official scaffolding for unit testing, how to cover variables in watch?
typecho
typecho 2017-06-27 09:19:20
0
1
1107

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

typecho
typecho

Following the voice in heart.

reply all(1)
大家讲道理

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()
  })
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template