gpt4 book ai didi

vue.js - 在 bootstrap-vue modal (b-modal) int 测试中找不到按钮

转载 作者:行者123 更新时间:2023-12-02 02:24:11 24 4
gpt4 key购买 nike

我想在我的测试用例中测试模态行为。

  1. 通过页面上的按钮打开模态框 (isVisible false=> true)
  2. 通过模态上的按钮关闭模态(isVisible trye=> false)

第一步没问题,但第二步失败。我可以通过ref找到modal,但是找不到modal上的按钮。

首页.vue

<template>
<div class="home">
<b-button id='button-open' @click="open">open</b-button>
<b-modal
ref="modal-ref"
hide-footer
>
<b-button id='button-close' @click="close">close</b-button>
</b-modal>
</div>
</template>


<script>
export default {
methods: {
open() {
console.log('open');
this.$refs['modal-ref'].show();
},
close() {
console.log('ok');
this.$refs['modal-ref'].hide();
},
},
};
</script>

home.spec.js

import { expect } from 'chai';
import { createLocalVue, mount } from '@vue/test-utils';
import BootstrapVue from 'bootstrap-vue';
import Home from '@/views/Home.vue';

describe('Boostrap Modal', () => {
it('open and close modal', async () => {
const localVue = createLocalVue();
localVue.use(BootstrapVue);
const wrapper = mount(Home, {
localVue,
attachToDocument: true,
});

const open = wrapper.find('#button-open');
const modal = wrapper.find({ ref: 'modal-ref' });
expect(modal.vm.isVisible).to.equal(false);
open.trigger('click');
await wrapper.vm.$nextTick();
console.log(modal.exists());
expect(modal.vm.isVisible).to.equal(true);

// find it from modal
const close = modal.find('#button-close');
expect(close.exists()).to.equal(true);
close.trigger('click');

await wrapper.vm.$nextTick();
expect(modal.vm.isVisible).to.equal(false);
wrapper.destroy();
});
});

我不明白为什么找不到关闭按钮。(close.exists() => false)

最佳答案

默认情况下,模态窗口会附加到<body>元素。显示时不会出现在 wrapper 内。

您需要查询document找到您的按钮:

import { expect } from 'chai';
import { createLocalVue, mount, createWrapper } from '@vue/test-utils';
import BootstrapVue from 'bootstrap-vue';
import Home from '@/views/Home.vue';

describe('Boostrap Modal', () => {
it('open and close modal', async () => {
const localVue = createLocalVue();
localVue.use(BootstrapVue);
const wrapper = mount(Home, {
localVue,
attachToDocument: true,
});

const open = wrapper.find('#button-open');
const modal = wrapper.find({ ref: 'modal-ref' });
expect(modal.vm.isVisible).to.equal(false);
open.trigger('click');
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
console.log(modal.exists());
expect(modal.vm.isVisible).to.equal(true);

// find it from modal close button in the document
const closeElement = document.getElementById('button-close');
expect(closeElement).toBeDefined();
const closeWrapper = createWrapper(closeElement);
expect(closeWrapper.exists()).to.equal(true);
expect(closeWrapper.is('button')).toBe(true);
closeWrapper.trigger('click');

await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
expect(modal.vm.isVisible).to.equal(false);
wrapper.destroy();
});
});

关于vue.js - 在 bootstrap-vue modal (b-modal) int 测试中找不到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58320101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com