【问题标题】:vue unit test trigger('click') not firingvue 单元测试触发器('click')没有触发
【发布时间】:2021-09-23 08:45:43
【问题描述】:

我在我的 vue 项目中有一个看起来像这样的测试,

it('should close on dialog close click', async () => {
    wrapper = mountWith({open :true});
    wrapper.find('.c-dialog__close').trigger('click');
    await Vue.nextTick();
    console.log(wrapper.vm.open);
    expect(wrapper.vm.open).toBe(false);
});

我期望wrapper.vm.open 在触发点击时从true 变为false,但它保持为假。在实际的 UI 中,点击有效并且显示设置为 false,所以测试中一定有什么地方出错了?组件代码如下,

    <template>
    <!-- eslint-disable vue/no-v-html -->
    <div class="c-dialog" :class="{'u-block': show}" @triggerOpen="handleOpen">
        <div class="c-dialog__background" @click="handleClose">
            <div class="c-dialog__wrapper u-mt-4 u-mb-1">
                <div class="c-dialog__action u-flex">
                    <button class="c-dialog__close u-bg-denim-blue u-border-0 u-text-white" @click="handleClose()">Close</button>
                </div>
                <div class="c-dialog__main u-px-2 u-py-4 u-border u-bg-white">
                    <h4>{{ content.title }}</h4>
                    <div class="c-dialog__content u-mt-2" v-html="content.content" />
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts">

import Vue from 'vue';

export default Vue.extend({
    props: {
        content: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            show: false
        }
    },
    methods: {
        handleOpen() {
            this.show = true;
        },
        handleClose() {
            this.show = false;
        }
    }

});

</script>

【问题讨论】:

    标签: javascript vue.js unit-testing jestjs


    【解决方案1】:

    您可能已经弄清楚了,在您的组件中您有“显示”,但在您的测试中您期望“打开”为真。

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2018-04-30
      • 2021-09-03
      • 2016-12-30
      • 2021-07-06
      相关资源
      最近更新 更多