1
mutation.jsファイルをテストしようとしています。テストは正しく実行されますが、カバレッジを見ると、仕様から呼び出されないため、カバーされていない1つの関数(tick())があります...Vue.js突然変異ユニットは、私のsetInterval()コールバック関数が実行されない理由をテストします
注:私はsinonjs/lolexを使用して通過時間をシミュレートします
mutation.spec.js
import Vue from 'vue'
import mutations, { tick } from '@/vuex/mutations'
import * as types from '@/vuex/mutation_types'
import { WORKING_TIME, RESTING_TIME } from '@/config'
const lolex = require('lolex') //JS implementation of timer APIs
describe('mutations',() => {
let state
let clock
beforeEach(() => {
clock = lolex.createClock()
state = {}
Vue.noise = {
start:() => {},
stop:() => {},
pause:() => {}
}
sinon.spy(Vue.noise, 'start')
sinon.spy(Vue.noise, 'pause')
sinon.spy(Vue.noise, 'stop')
})
afterEach(() => {
Vue.noise.start.restore()
Vue.noise.pause.restore()
Vue.noise.stop.restore()
})
describe('TOGGLE WORKING->REST->WORKING',() => {
it('should switch to REST period after WORKING then back WORKING after RESTING',() => {
state.isWorking = true
state.soundEnabled = true
// IT SHOULD CALL THE TICK() imported method to cover it !
state.interval = clock.setInterval(() => tick(state), 1000)
console.log('STATE INTERVAL: ', state.setInterval)
// console LOG: 'STATE INTERVAL: ', undefined
state.counter = 0
mutations[types.START](state)
expect(Vue.noise.start).to.have.been.called
clock.setTimeout(() => {
// should be resting now
expect(state.isWorking).to.equal(false)
clock.setTimeout(() => {
// should be back working now
expect(state.isWorking).to.equal(true)
}, WORKING_TIME + RESTING_TIME)
}, WORKING_TIME)
})
})
})
vuex/mutation.js
import * as types from './mutation_types'
import _ from 'underscore'
import { WORKING_TIME, RESTING_TIME, KITTEN_TIME } from '../config'
import Vue from 'vue'
function togglePomodoro (state, toggle) {
if (_.isBoolean(toggle) === false) {
toggle = !state.isWorking
}
state.isWorking = toggle
if (state.isWorking) {
Vue.noise.start()
} else {
Vue.noise.pause()
}
state.counter = state.isWorking ? WORKING_TIME : RESTING_TIME
}
export function tick (state) {
console.log('tick called')
if (state.counter === 0) {
togglePomodoro(state)
}
state.counter--
if (state.counter % KITTEN_TIME === 0) {
state.timestamp = new Date().getTime()
}
}
export default {
[types.START] (state) {
state.started = true
state.paused = false
state.stopped = false
state.interval = setInterval(() => tick(state), 1000)
if (state.isWorking && state.soundEnabled) {
Vue.noise.start()
}
},
}