Skip to content

Issues during testing #202

Closed
Closed
@parker-codes

Description

@parker-codes

I've noticed some things that do not work with the composition API in testing.

  1. setValue(xxx) does nothing
    resource here

  2. wrapper.vm does not include the component state for reference
    resource here

Component

<template>
  <div class="mt-4 flex px-3 py-2 text-gray-800 rounded-full bg-gray-300">
    <input
      v-model="newTitle"
      @keydown.enter="submit"
      class="mx-2 bg-gray-300"
      aria-label="New Item Title"
    />

    <button @click="submit" class aria-label="Create New Item">
      <PlusSvg classes="h-6 w-6" />
    </button>
  </div>
</template>

<script lang="ts">
import { createComponent, ref } from '@vue/composition-api';
import PlusSvg from '@/components/svg/PlusSvg.vue';

export default createComponent({
  setup(_, { emit }) {
    const newTitle = ref('');

    function submit() {
      if (!newTitle.value.trim()) return;
      emit('submit', newTitle.value.trim());
      newTitle.value = '';
    }

    return { newTitle, submit };
  },

  components: { PlusSvg }
});
</script>

Tests:

import { shallowMount } from '@vue/test-utils';
import NewItem from '@/components/NewItem.vue';

import Vue from 'vue';
import CompositionApi from '@vue/composition-api';
Vue.use(CompositionApi);

describe('NewItem', () => {
  test('is a Vue instance', () => {
    const wrapper = shallowMount(NewItem);
    expect(wrapper.isVueInstance()).toBeTruthy();
  });

  test('initializes with empty input', () => {
    const wrapper = shallowMount(NewItem);
    expect(wrapper.find('input').text()).toBe('');
  });

  test('cannot submit with empty input', () => {
    const wrapper = shallowMount(NewItem);
    wrapper.find('button').trigger('click');
    expect(wrapper.emitted().submit).toBeFalsy();
  });

  test('submit bubbles event with input', async () => {
    const wrapper = shallowMount(NewItem);
    const newTodoTitle = 'Go to the store';
    wrapper.find('input').setValue(newTodoTitle);
    wrapper.find('button').trigger('click');
    await wrapper.vm.$nextTick();
    expect(wrapper.emitted().submit[0]).toBe(newTodoTitle);
  });

  test('input is cleared after submit', async () => {
    const wrapper = shallowMount(NewItem);
    const newTodoTitle = 'Take out the trash';
    expect(wrapper.find('input').is('input')).toBeTruthy();
    wrapper.find('input').setValue(newTodoTitle);
    await wrapper.vm.$nextTick();
    expect(wrapper.find('input').text()).toBe(newTodoTitle);
    wrapper.find('button').trigger('click');
    await wrapper.vm.$nextTick();
    expect(wrapper.find('input').text()).toBe('');
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions