Vue रचना एपीआई घटक में जेस्ट के साथ यूनिट परीक्षण कैसे करें?


9

मैं jue के साथ एक यूनिट टेस्ट लिख रहा हूँ, मेरी संरचना API घटक के लिए vue.js.

लेकिन मैं कंपोज़िशन एपीआई के सेटअप () में फंक्शन्स तक नहीं पहुँच सकता।

Indicator.vue

<template>
  <div class="d-flex flex-column justify-content-center align-content-center">
    <ul class="indicator-menu d-flex justify-content-center">
      <li v-for="step in steps" :key="step">
        <a href="#" @click="updateValue(step)" :class="activeClass(step, current)"> </a>
      </li>
    </ul>
    <div class="indicator-caption d-flex justify-content-center">
      step
      <span> {{ current }}</span>
      from
      <span> {{ steps }}</span>
    </div>
  </div>
</template>

<script lang="ts">
import {createComponent} from '@vue/composition-api';

export default createComponent({
  name: 'Indicator',
  props: {
    steps: {
      type: Number,
      required: true
    },
    current: {
      type: Number,
      required: true
    }
  },
  setup(props, context) {
    const updateValue = (step: number) => {
      context.emit('clicked', step);
    };
    const activeClass = (step: number, current: number) =>
      step < current ? 'passed' : step === current ? 'current' : '';
    return {
      updateValue,
      activeClass
    };
  }
});
</script>

<style></style>

Indicator.test.ts

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

describe('@/views/components/Indicator.vue', () => {  
  let wrapper: any;
  beforeEach(() => {
    wrapper = shallowMount(Indicator, {
      propsData: {
        steps: 4,
        current: 2
      }
    });
  });
  it('should return "current" for values (2,2)', () => {
    expect(wrapper.vm.activeClass(2, 2)).toBe('current');
  });
});

और मुझे यह त्रुटि आई, टेस्ट कमांड चलाने में:

TypeError: अपरिभाषित की संपत्ति 'vm' नहीं पढ़ सकता है

जवाबों:


1

मुझे लगता है कि बस आयात CompositionApiकरने से आपकी समस्या हल हो जानी चाहिए।

import CompositionApi from '@vue/composition-api'

Vue.use(CompositionApi)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.