You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue+Vite环境下FormKit基础表单测试问题排查

Vue + Vite 环境下 FormKit 表单测试问题求助

在Vue和Vite环境中测试一个基础FormKit表单,表单包含文本框和按钮,预期输入文本并点击按钮后,输入内容会显示在页面指定区域。但测试时,虽能输入文本并触发按钮点击,却无法获取输入内容,页面也未显示对应文本,尝试多种方法仍未解决,特此求助。

基础表单代码

<template>
<div class="ml-6 mt-6">
  <h2>Basic Form</h2>
  <FormKit type="form" id="myform" v-model="myform" :actions="false">
    <FormKit
      type="text"
      name="name"
      id="name"
      label="Name:"
      help="Your full name"
      placeholder="“Jon Doe”"
    />
    <div class="mt-6 mb-6">
      <FormKit
        type="button"
        name="clickme"
        data-testid="clickme"
        label="Click Me"
        help="This will show what you entered."
        @click="displayName"
      />
    </div>
  </FormKit>
  <div class="mt-4">
    <p>Entered Name: {{ enteredName }}</p>
  </div>
</div>
</template>

<script setup>
import { ref } from 'vue';

const myform = ref();
let enteredName = ref('');

function displayName()
{
  console.log('button clicked');
  enteredName.value = myform.value.name;
  console.log('form value: ' + JSON.stringify(myform.value));
  console.log('Entered Name: ' + enteredName.value);
}
</script>

测试代码

import { mount} from '@vue/test-utils';
import { beforeEach, describe, it, expect } from 'vitest';
import BasicForm from '../components/BasicForm.vue';
import { plugin, defaultConfig } from "@formkit/vue";

describe('BasicForm.vue', () => {
  let wrapper = null;
  beforeEach(() => {
        // render the component
        wrapper = mount(BasicForm, {
      attachTo: document.getElementById('app'),
            props: {
            },
            global: {
                plugins: [
                    [plugin, defaultConfig],
                ],
            },
        });
  });

  it('displays the entered name when the button is clicked', async () => {
    // Find the input and set its value
    const input = wrapper.find('input[name="name"]');
    console.log('input= ' + JSON.stringify(input));
    await input.setValue('Joe Doe');
    console.log('input= ' + JSON.stringify(input));

    // Find the button and trigger a click event
    const button = await wrapper.find('[data-testid="clickme"]');
    console.log('button= ' + JSON.stringify(button));
    await button.trigger('click');
    await wrapper.vm.$nextTick();

    // Assert that the entered name is displayed
    // expect(wrapper.vm.enteredName).toBe('Joe Doe');
    expect(wrapper.text()).toContain('Entered Name: Joe Doe');
  });
});

测试结果

测试断言失败,页面未显示预期的Entered Name: Joe Doe内容,无法获取输入框中的文本值。

内容的提问来源于stack exchange,提问作者techLiberty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:25:00