如何使用 props 传递 vuelidate 对象进行玩笑测试?

发布于 2025-01-16 17:02:24 字数 3463 浏览 2 评论 0原文

我有几个组件,每个组件都有用 vuelidate 验证的字段。所有字段验证规则都在子组件的父组件包装器中描述,我传递对象:v =“$ v”,一切正常。但是在测试中,安装时,我收到错误,据我了解,因为安装时,我没有传递这个对象,请告诉我该怎么做?

父包装

   <template>
      <VModal>
        <template v-slot:content>
          <div class="layer">
            <institutes-info
                :v="$v"
                ref="info-component"
            />
    
    
          </div>
        </template>
      </VModal>
    </template>

export default {
  name: "WrapModalInstitutes",
  validations: {
    si: {
      name: {
        required,
        maxLength: maxLength(256),
      },
    },
  },
}

子组件

        <template>
      <form action="" id="form-info" @submit.prevent="uploadInfo($event, si.id)">
        <p class="sub-text">{{ $t("vnz.info") }}</p>
    
        <div class="institut-info">
    
          <div class="input-group" :class="{ 'form-group--error': v.si.name.$error, 'form-group--success': !v.si.name.$invalid }">
            <label for="name-institut">{{ $t("vnz.nameVNZ") }}</label>
            <div class="input-container">
              <input id="name-institut" name="title" type="text" :placeholder="$t('vnz.nameVNZ')" v-model.trim="name" @keydown="trimSpecialCharacters($event)" @input="trimSpecialCharacters($event, 'name')">
              <div class="error" v-if="!v.si.name.required && v.si.name.$dirty">{{ $t("vnz.notEmpty") }}</div>
            </div>
          </div>
    
        </div>
    
        <button v-if="edit" type="submit" class="btn-primary edit" :disabled="submitStatus === 'PENDING'">{{ $t("vnz.save") }}</button>
      </form>
    </template>

export default {
  name: "InstitutesInfoModal",
  props: ['v'],
}

测试文件

    import Vuex from "vuex"
import {mount, createLocalVue, config} from "@vue/test-utils"
import InstitutesInfo from '../../assets/src/components/InstitutesInfo'
import Vuelidate from 'vuelidate'
import VueTheMask from 'vue-the-mask'
import translations from "../../assets/src/common-resources/translations/translations"
import fetchMock from 'jest-fetch-mock'

const locale = "uk";
config.mocks["$t"] = msg => translations[locale][msg]
fetchMock.enableMocks()

const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(Vuelidate)
localVue.use(VueTheMask)

describe('testing institute-info component', () => {
    let store;
    let mutations;
    let actions;
    beforeEach(() => {
        mutations = {
            'institutes/EDIT_SI_OBJ': jest.fn(),
            'institutes/SUBMIT_STATUS': jest.fn()
        }
        actions = {
            'institutes/UPLOAD_INFO': jest.fn()
        }
        store = new Vuex.Store({
            modules: {
                institutes: {
                    state: {
                        si: {
                            name: null,
                        },
                        submitStatus: null
                    },
                    mutations,
                    actions
                }
            }
        })
    })
    test('some test"',() => {
        const wrapper = mount(InstitutesInfo, {
            store, localVue
        })
    })
})

[Vue warn]:渲染错误:“TypeError:无法读取未定义的属性'si' ”

I have several components inside each of them have fields that are validated with vuelidate. All field validation rules are described in the parent component wrapper in the child components, I pass the object :v="$v", everything works. But in the test, when mounting, I get an error, and as I understand it, because when mounting, I do not pass this object, tell me how to do it right?

parent wrapper

   <template>
      <VModal>
        <template v-slot:content>
          <div class="layer">
            <institutes-info
                :v="$v"
                ref="info-component"
            />
    
    
          </div>
        </template>
      </VModal>
    </template>

export default {
  name: "WrapModalInstitutes",
  validations: {
    si: {
      name: {
        required,
        maxLength: maxLength(256),
      },
    },
  },
}

child component

        <template>
      <form action="" id="form-info" @submit.prevent="uploadInfo($event, si.id)">
        <p class="sub-text">{{ $t("vnz.info") }}</p>
    
        <div class="institut-info">
    
          <div class="input-group" :class="{ 'form-group--error': v.si.name.$error, 'form-group--success': !v.si.name.$invalid }">
            <label for="name-institut">{{ $t("vnz.nameVNZ") }}</label>
            <div class="input-container">
              <input id="name-institut" name="title" type="text" :placeholder="$t('vnz.nameVNZ')" v-model.trim="name" @keydown="trimSpecialCharacters($event)" @input="trimSpecialCharacters($event, 'name')">
              <div class="error" v-if="!v.si.name.required && v.si.name.$dirty">{{ $t("vnz.notEmpty") }}</div>
            </div>
          </div>
    
        </div>
    
        <button v-if="edit" type="submit" class="btn-primary edit" :disabled="submitStatus === 'PENDING'">{{ $t("vnz.save") }}</button>
      </form>
    </template>

export default {
  name: "InstitutesInfoModal",
  props: ['v'],
}

test file

    import Vuex from "vuex"
import {mount, createLocalVue, config} from "@vue/test-utils"
import InstitutesInfo from '../../assets/src/components/InstitutesInfo'
import Vuelidate from 'vuelidate'
import VueTheMask from 'vue-the-mask'
import translations from "../../assets/src/common-resources/translations/translations"
import fetchMock from 'jest-fetch-mock'

const locale = "uk";
config.mocks["$t"] = msg => translations[locale][msg]
fetchMock.enableMocks()

const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(Vuelidate)
localVue.use(VueTheMask)

describe('testing institute-info component', () => {
    let store;
    let mutations;
    let actions;
    beforeEach(() => {
        mutations = {
            'institutes/EDIT_SI_OBJ': jest.fn(),
            'institutes/SUBMIT_STATUS': jest.fn()
        }
        actions = {
            'institutes/UPLOAD_INFO': jest.fn()
        }
        store = new Vuex.Store({
            modules: {
                institutes: {
                    state: {
                        si: {
                            name: null,
                        },
                        submitStatus: null
                    },
                    mutations,
                    actions
                }
            }
        })
    })
    test('some test"',() => {
        const wrapper = mount(InstitutesInfo, {
            store, localVue
        })
    })
})

[Vue warn]: Error in render: "TypeError: Cannot read property 'si' of undefined"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文