0

I am trying to build a test that will target an element with the data-testid attribute. I have a BaseTile component that looks like this:

<template>
      <div
        data-testid="base-tile-icon"
        v-if="!!this.$slots.icon"
      >
        <slot name="icon"></slot>
      </div>
</template>

<script>
export default {};
</script>

<style></style>

And my test looks like this:

import { mount } from '@vue/test-utils';
import BaseTile from '@/components/BaseTile';

const factory = (slot = 'default') => {
  return mount(BaseTile, {
    slots: {
      [slot]: '<div class="test-msg"></div>'
    }
  });
};

it('has an icon slot if an icon is provided', () => {
  let wrapper = factory({ slot: 'icon' });
  const input = wrapper.find('[data-testid="base-tile-icon"]');
  expect(input.findAll('.test-msg').length).toBe(1);
});

How do I appropriately target the data-testid attribute with this test?

Spike
  • 121
  • 2
  • 12

1 Answers1

2

The named parameter of the factory is implemented incorrectly. The correct method is described in this post: Is there a way to provide named parameters in a function call in JavaScript?

The correct way to implement this is as follows:

import { mount } from '@vue/test-utils';
import BaseTile from '@/components/BaseTile';

const factory = ({ slot = 'default' } = {}) => {
  return mount(BaseTile, {
    slots: {
      [slot]: '<div class="test-msg"></div>'
    }
  });
};

it('has an icon slot if an icon is provided', () => {
  let wrapper = factory({ slot: 'icon' });
  const input = wrapper.find('[data-testid="base-tile-icon"]');
  expect(input.findAll('.test-msg').length).toBe(1);
});
Spike
  • 121
  • 2
  • 12