【问题标题】:How to test Vuex correctly?如何正确测试 Vuex?
【发布时间】:2019-12-09 12:07:10
【问题描述】:

我需要您的帮助来测试 Vuex 商店(以前从未这样做过,很遗憾很难理解)。

我有两个按钮,我需要测试它们是否调用了两个不同的函数来返回一些东西。

按钮:

...
<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)
                })
        }
    }
...

提前谢谢你!

【问题讨论】:

    标签: javascript vue.js jestjs vuex vue-test-utils


    【解决方案1】:

    你需要使用 jest mocks(如果使用 jest)。

    这里有一个关于它的快速教程。

    https://codeburst.io/a-pattern-for-mocking-and-unit-testing-vuex-actions-8f6672bdb255

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多