如何在使用Cypress和Vue进行组件测试时在插槽中使用组件?

huangapple go评论68阅读模式
英文:

How to use component in slot for component testing with Cypress and Vue?

问题

我需要在使用Vue的Cypress组件测试时在插槽中导入一个组件。

文档中说,对于插槽:

import DefaultSlot from './DefaultSlot.vue';

describe('<DefaultSlot />', () => {
  it('renders', () => {
    cy.mount(DefaultSlot, {
      slots: {
        default: 'Hello there!',
      },
    })
    cy.get('div.content').should('have.text', 'Hello there!')
  })
})

我想要像这样做:

<DefaultSlot>
  <AnotherSlot />
</DefaultSlot>
英文:

I need to import a component in slot with Cypress component Testing using Vue.

Documentation say that for slots :

import DefaultSlot from &#39;./DefaultSlot.vue&#39;

describe(&#39;&lt;DefaultSlot /&gt;&#39;, () =&gt; {
  it(&#39;renders&#39;, () =&gt; {
    cy.mount(DefaultSlot, {
      slots: {
        default: &#39;Hello there!&#39;,
      },
    })
    cy.get(&#39;div.content&#39;).should(&#39;have.text&#39;, &#39;Hello there!&#39;)
  })
})

I want to do like this :

&lt;DefaultSlot&gt;
 &lt;AnotherSlot /&gt;
&lt;/DefaultSlot&gt;

答案1

得分: 1

为了解决我的问题,我已经使用组件选项导入了组件,如下所示:

import DefaultSlot from './DefaultSlot.vue'
import AnotherSlot from './AnotherSlot.vue';

describe('', () => {
it('renders', () => {
cy.mount(DefaultSlot, {
components: {
AnotherSlot
},
slots: {
default: '',
},
});
cy.get('div.content').should('have.text', 'Hello there!');
});
});


<details>
<summary>英文:</summary>

To solve my question, I&#39;ve imported component using components options like :

import DefaultSlot from './DefaultSlot.vue'
import AnotherSlot from './AnotherSlot.vue'

describe('<DefaultSlot />', () => {
it('renders', () => {
cy.mount(DefaultSlot, {
components: {
AnotherSlot
}
slots: {
default: '<AnotherSlot label="my label" />',
},
})
cy.get('div.content').should('have.text', 'Hello there!')
})
})

huangapple
  • 本文由 发表于 2023年2月6日 17:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定