这是仅描述相关段落的片断/幻想代码

Test/my-Component.spec.js

import { shallowMount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';

import stateMyPluginStore from '../my-component/state';
import stateProductDetail from '...' // the original state from Shopware
...

const product = {
    purchasePrices: [
        {
            net: 15
        }
    ]
};

const productComponent = function(id, purchasePriceNet = 0, purchaseUnitCount = 0, unitId = null) {
    let component = {
        id: 'component_product_ident_' + id,
        name: 'component_name_' + id,
        purchasePrices: [
            {
                net: purchasePriceNet
            }
        ],
        extensions: {
            productPriceConfigurator: {
                purchaseUnitCount: purchaseUnitCount
            }
        }
    }

    if(unitId) {
        component.unitId = unitId
    }

    return component;
}

describe('PriceConfiguratorPurchasePricesView', () => {
    const localVue = createLocalVue();
    localVue.use(Vuex);
    let wrapper = null;

    beforeEach(() => {
        Shopware.State.registerModule('swProductDetail', stateProductDetail)
        Shopware.State.registerModule('MyPluginStore', stateMyPluginStore)
    });

    afterEach(() => {
        Shopware.State.unregisterModule('MyPluginStore')
        Shopware.State.unregisterModule('swProductDetail')

        if(wrapper) {
            wrapper.destroy();
            wrapper = null;
        }
    });

    it('should update list', async () => {
        Shopware.State.commit(
            'swProductDetail/setProduct', {
                purchasePrices: [
                    {
                        net: 10
                    }
                ]
            }
        )
        Shopware.State.commit(
            'MyPluginStore/setProductComponents', [
                productComponent(2, 1.10, 1, 'unitId_2'),
            ]
        )
        wrapper = shallowMount(await Shopware.Component.build('my-plugin'), {
            localVue,
            mocks: {
                ...
            },
            stubs: {
                ...
            },
            provide: {
                validationService: {},
                repositoryFactory: {
                    create: (entity) => ({
                        search: () => {
                            if(entity === 'unit') {
                                const response = {
                                    total: 2
                                };
                                return Promise.resolve(response)
                            }
                        },
                        get: () => {
                            if(entity === 'unit') {
                                const response = {
                                    "translated": {
                                        "shortCode": "kg",
                                        "name": "Kilogramm",
                                        "customFields": {}
                                    }
                                };
                                return Promise.resolve(response)
                            }
                        }
                    })
                }
            },
        });
        await wrapper.setData({}).then(() => {})

        console.log(wrapper.html())
        expect(wrapper.findAll('fieldset').length).toEqual(1);
    });
})

测试配置的第一步是执行默认状态提交,以使一些数据可用.结果:

  • console.log(wrapper.html())2.
<div class="sw-container" style="justify-items: stretch; align-items: stretch;" id="purchaseCalculatorTemplate"><br>
  <sw-skeleton-stub></sw-skeleton-stub>
</div>
  • console.log(wrapper.html())2.
<div class="sw-container" style="justify-items: stretch; align-items: stretch;" id="purchaseCalculatorTemplate">
      <fieldset class="">
        <div class="sw-container container-input-fields" style="grid-template-columns: 3fr 1fr 3fr 1fr 3fr; justify-items: stretch; align-items: stretch;" data-product="component_product_ident_2">
          <div class="sw-container sw-field sw-block-field sw-contextual-field sw-field--number sw-field--default" style="justify-items: stretch; align-items: stretch;">
            <div class="sw-field sw-block-field sw-contextual-field sw-field--number is--disabled sw-field--default">
              <div class="sw-field__label">
                <!----> <label for="sumProductComponentUnitPurchase_0" class="">
                  ∑ ["terms.purchasePrice.name"] (["terms.net.name"])
                </label>
                <!---->
              </div>
              <div class="sw-block-field__block">
                <!----> <input id="sumProductComponentUnitPurchase_0" name="sumProductComponentUnitPurchase_0" type="text" placeholder="" disabled="disabled" autocomplete="off">
                <div class="sw-field__addition">
                  <!---->
                </div>
              </div>
              <!---->
              <!---->
            </div>
          </div>
        </div>
      </fieldset>
    </div>

Question如果设置为await wrapper.setData({}).then(() => {}),为什么包装器按预期呈现(使用可用的comit数据)only以及如何修复它?

推荐答案

通过等待setData,您也在等待呈现内容所需的另一个tick,例如,当初始状态显示加载指示符时.

您可以使用nextTick函数来等待即将到来的滴答,但可能需要等待多个滴答,直到达到所需的状态...

wrapper = shallowMount(await Shopware.Component.build('my-plugin'), {});
await wrapper.vm.$nextTick();
console.log(wrapper.html());

...或者您可以使用全局flushPromises功能来等待所有后续的promise /勾选.

wrapper = shallowMount(await Shopware.Component.build('my-plugin'), {});
await flushPromises();
console.log(wrapper.html());

Vue.js相关问答推荐

通过元素加中的正则表达式输入电话号码格式

Vue3如何将自定义事件绑定到路由视图中的特定组件?

如何 for each 用户分组聊天消息?

如何渲染数组以 Select 选项 vue.js

Vue3在'vue-router'中找不到导出'createWebHistory,createRouter'

全局方法和实例方法有什么区别?

在 SPA VueJS 中全局声明 mapState 和 mapMutations

VueJS 禁用特定属性的react性

如何在 Vue.js 中传递 $router.push 中的数据?

Vue router路由,Root Vue 没有数据?

Nuxt 应用程序在刷新动态路由时返回 404(Tomcat 服务器)

VueJS错误编译模板

将 VueJS 数据属性重置为初始值

如何避免在 Vue 中一直写 this.$store.state.donkey 的需要?

Vue CLI 3 sass-resources-loader - Options.loaders 未定义

来自 Vue.js 的 ESLint 插件的消息中的LHS是什么意思?

如何使用 Vue Test utils 测试输入是否聚焦?

Vue.js react性如何在幕后工作?

如何使用 Vuetify 验证复选框组

Mapbox 事件监听器