简体   繁体   English

您如何测试Vue.js mixin的模板?

[英]How do you test a Vue.js mixin's template?

So we have a Vue.js mixin which is inherited in individual components. 因此,我们有一个Vue.js mixin,它在各个组件中继承。 The mixin has a template that is inherited by a handful of components and it works without problems. mixin具有一个模板,该模板由少数组件继承,并且可以正常工作。 However, I can't work out how to test the template with vue-test-utils. 但是,我不知道如何使用vue-test-utils测试模板。

This is a simplified example of what I'm doing: 这是我正在做的简化示例:

describe('MiMixin', () => {
    let wrapper

    wrapper = mount({
        mixins: [MiMixin]
    })

    it('should set the mixin template as the markup', () => {
        wrapper.find('.mi-component').trigger('click')
    })
})

When I run this I get the message: 当我运行它时,我得到消息:

[vue-test-utils]: find did not return .mi-component, cannot call trigger() on empty Wrapper

If I add a render function to the mounted Vue component, it just renders any markup that I return (as expected). 如果我将渲染函数添加到已挂载的Vue组件,它将仅渲染我返回的所有标记(如预期的那样)。 However, when there is no render method (and therefore no template), the html of the component is undefined. 但是,当没有渲染方法(因此也没有模板)时,该组件的html是未定义的。


Questions: 问题:

  1. Why does the 'find' function not find the template for the mixin? 为什么“查找”功能找不到mixin的模板?
  2. Is it correct to test the mixin in isolation, or is it better to test it in a real component? 单独测试mixin是否正确,还是在实际组件中对其进行更好的测试?

NOTE: The template does seem to exist in the wrapper under VueComponent: 注意:该模板似乎确实存在于VueComponent下的包装器中:

VueWrapper {
    vnode: [Getter/Setter],
    element: [Getter/Setter],
    update: [Function: bound update],
    options: { attachedToDocument: false },
    version: 2.5,
    vm: 
    VueComponent {
     _uid: 0,
     _isVue: true,
     '$options': 
      { components: [Object],
        directives: {},
        filters: {},
        _base: [Object],
        _Ctor: [Object],
        beforeCreate: [Object],
        template: '<div class="mi-component"> // Template is here </div>'

You could pass in a mocked component with whatever template you need: 您可以使用需要的任何模板来传递模拟组件:

const Component = {
  template: '<div class="mi-component"></div>'
};

describe('MiMixin', () => {
    let wrapper

    wrapper = mount(Component, {
        mixins: [MiMixin]
    })

    it('should set the mixin template as the markup', () => {
        wrapper.find('.mi-component').trigger('click')
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM