如何正确测试 Vuex?
How to test Vuex correctly?
我需要你的帮助来测试 Vuex 存储(以前从未做过,不幸的是很难理解)。
我有两个按钮,我需要测试它们是否调用了两个不同的函数 return。
按钮:
...
<input class="main-form__login-submit" v-if="this.loginBtn" type="submit" @click="handleLogin" value="Войти" />
<input class="main-form__login-submit main-form__login-session" v-if="this.showLogoutEverywhereBtn" type="button" @click="websocketAuth" value="Выйти из других окон" />
...
方法:
...
methods: {
handleLogin(e) {
e.preventDefault()
this.$store.dispatch('login', this.loginForm)
.then((response) => {
console.log('login page response: ', response)
if (response.id_user !== undefined) {
this.$router.push({ path: '/' })
}
})
.catch((e) => {
console.log('ты внутри ошибки: ', e);
});
},
websocketAuth(e) {
e.preventDefault()
console.log('login: ', this.loginForm.username, this.loginForm.password)
this.$store.dispatch("logoutEverywhere", {
user_login: this.loginForm.username,
user_password: this.loginForm.password
})
.then((resp) => {
let data = { userId: resp, page: 'login' }
socketUrl.emit('logoutEverywhere', data, (flag) => {
if(flag) {
this.$store.dispatch('duplicateLoginClear')
}
console.log(flag)
})
console.log('1 ', resp)
})
}
}
...
提前致谢!
您需要使用 jest 模拟(如果使用 jest)。
这里有一个快速教程。
https://codeburst.io/a-pattern-for-mocking-and-unit-testing-vuex-actions-8f6672bdb255
我需要你的帮助来测试 Vuex 存储(以前从未做过,不幸的是很难理解)。
我有两个按钮,我需要测试它们是否调用了两个不同的函数 return。
按钮:
...
<input class="main-form__login-submit" v-if="this.loginBtn" type="submit" @click="handleLogin" value="Войти" />
<input class="main-form__login-submit main-form__login-session" v-if="this.showLogoutEverywhereBtn" type="button" @click="websocketAuth" value="Выйти из других окон" />
...
方法:
...
methods: {
handleLogin(e) {
e.preventDefault()
this.$store.dispatch('login', this.loginForm)
.then((response) => {
console.log('login page response: ', response)
if (response.id_user !== undefined) {
this.$router.push({ path: '/' })
}
})
.catch((e) => {
console.log('ты внутри ошибки: ', e);
});
},
websocketAuth(e) {
e.preventDefault()
console.log('login: ', this.loginForm.username, this.loginForm.password)
this.$store.dispatch("logoutEverywhere", {
user_login: this.loginForm.username,
user_password: this.loginForm.password
})
.then((resp) => {
let data = { userId: resp, page: 'login' }
socketUrl.emit('logoutEverywhere', data, (flag) => {
if(flag) {
this.$store.dispatch('duplicateLoginClear')
}
console.log(flag)
})
console.log('1 ', resp)
})
}
}
...
提前致谢!
您需要使用 jest 模拟(如果使用 jest)。
这里有一个快速教程。
https://codeburst.io/a-pattern-for-mocking-and-unit-testing-vuex-actions-8f6672bdb255