profile

Filip Hric

Component testing in Cypress #5: Slots

Published over 1 year ago • 1 min read

Hey! 👋

Welcome to another part of component testing in Cypress. Today we will take a look into components with slots.

Slots are a powerful way of how we can pass content, template or even a whole another component within your component. In my component called Dropdown.vue, I have a <slot /> element, that will render our dropdown items. Simplified code would look something like this:

<template>
  <div>
    <div>Dropdown items</div>
    <hr>
    <slot />
  </div>
</template>

Passing slots into Cypress is very easy. We can add a property that will render the slot we need. We can confirm everything works as intended by creating the following component test:

import Dropdown from '@/components/common/Dropdown.vue';

it('shows dropdown', () => {
  cy.mount(Dropdown, {
    slots: {
      default: 'Add a new list'
    }
  });

  cy.contains('Add a new list')
      .should('be.visible')
});

See the "Add a new list"? This is what will get rendered as an item in the dropdown component. We confirm that everything works as expected with the Cypress assertion.

But what if instead a text, our component takes another component? In this case, we need to make sure to render the slot by using Vue’s h() function. We can even pass props to this child component as a second argument of this function.It looks something like this:

const dropdownActionItem = () => h(DropdownItem, { itemText: 'Add new list' })

We can then pass this function as a property into our slots attribute, the same way we did with text in the last example. The full test that tests different components rendered within the slots will look something like this:

import Dropdown from '@/components/common/Dropdown.vue';
import DropdownItem from '@/components/common/DropdownItem.vue';
import { h } from 'vue';

const dropdownActionItem = () => h(DropdownItem, { itemText: 'Add new list' })
const dropdownCloseItem = () => h(DropdownItem, { itemText: 'Delete list', warning: true })

it('shows dropdown', () => {
  cy.mount(Dropdown, {
    slots: {
      default: [dropdownActionItem, dropdownCloseItem],
    }
  });

  cy.contains('Add new list')
    .should('be.visible')

  cy.contains('Delete list')
    .should('be.visible')
    .and('have.class', 'warning')

  cy.get('[data-cy=dropdown-item]')
    .should('have.length', 2)
    
});

This test will render two subcomponents, one with a "warning" styling passed as a prop. Then we make an assertion on component styling and rendering.

This is a great way of testing components that are usually coupled together and see that they work properly.

That’s it, see you next time! 👋

​

​

​

​

​

​

​

​

Filip Hric

Lead SDET at @slidoapp, psychology graduate, @Cypress_io ambassador. Instructor at @eggheadio, @TestAutomationU and @Learn2Code_sk. I blog at http://filiphric.com

Read more from Filip Hric

Hey! Nice to be back in your inbox 😃 Welcome to another issue of component testing newsletter. Last time, we have successfully mounted a simple component. But maybe that component was way too simple. Let’s try something more fun. The component I want to test in today’s issue takes properties, that will change how the button is rendered. <template> <button class="button"> </button> </template> <script setup lang="ts"> defineProps({ buttonText: { default: 'Save', type: String, } }); </script>...

over 1 year ago • 1 min read

🎉 My new Vue component testing course is out! Link is at the bottom of this email 🎉 Let’s continue from where we last finished. Once you set up your component testing configuration and your cy.mount() command, you are ready to go start testing! So let’s take a look at how this can be done. Where to place component tests With e2e testing, we usually have a dedicated folder where all of our tests are. But with component testing it is better to place your tests right next to your components....

over 1 year ago • 1 min read

Hey! Welcome to second issue of component testing in Cypress. Let‘s talk about how to set it all up and what’s going on with component testin in Cypress. With Cypress v10 set up of component testing is very simple. Actually, Cypress authors have made a great job making it as simple as possible. Once you open Cypress GUI using npx cypress open command, you will be welcomed by this screen: Framework and bundler Once you choose component testing, Cypress asks you to choose a framework and a...

over 1 year ago • 2 min read
Share this post