简体   繁体   English

Vue 计算的 JEST 单元测试

[英]JEST unit test for Vue computed

How to write a JEST unit test for this computed that checks and filters by type:如何为此计算编写 JEST 单元测试以按类型检查和过滤:

TEMPLATE:模板:

Type email:输入 email:

        <CommunicationPreference
          v-for="(communication, index) in communicationPreferenceTypeEmail"
          :key="index + communication.name"
          :consent="communication.consent"
          :name="communication.name"
          @update="updateConsent"
        />

Not type email:不输入 email:

        <CommunicationPreference
          v-for="(communication, index) in communicationPreferenceTypeNotEmail"
          :key="index + communication.name"
          :consent="communication.consent"
          :name="communication.name"
          @update="updateConsent"
        />

Computed that filers by type so that I can display in two separate lists one that has a type of email and then one that is everything else:按类型计算文件管理器,以便我可以在两个单独的列表中显示一个类型为 email 的列表,然后一个是其他所有类型的列表:

  computed: {
    ...mapState('account', ['communicationPreferences']),
    communicationPreferenceTypeEmail() {
      return this.communicationPreferences.filter((e) => e.type === 'EMAIL')
    },
    communicationPreferenceTypeNotEmail() {
      return this.communicationPreferences.filter((e) => e.type !== 'EMAIL')
    },
  },

My test spec:我的测试规格:

  it('should filter communication preferences by TYPE', () => {})

Your question only includes the part of your template with the CommunicationPreference component, so it's hard to tell how the rest of the template would look like.您的问题仅包括带有CommunicationPreference组件的模板部分,因此很难说出模板的 rest 是什么样子。 But I think this might help you:但我认为这可能对您有所帮助:

import { mount } from '@vue/test-utils'
import Component from './path/to/Component'

const mockedCommunicationPreferencies = [
    {
        //...,
        type: 'EMAIL',
    },
    //...
    {
        //...,
        type: 'NOT-EMAIL',
    }
]

it('should filter communication preferencies by TYPE', () => {
    let component = mount(Component)

    component.setData({ communicationPreferences: mockedCommunicationPreferences })

    const emailCommunicationPreferences = component.vm.communicationPreferenceTypeEmail
    const nonEmailCommunicationPreferences = component.vm.communicationPreferenceTypeNotEmail

    expect(emailCommunicationPreferencies.every(cp => cp.type === 'EMAIL')).toBeTruthy()
    expect(nonEmailCommunicationPreferencies.every(cp => cp.type !== 'EMAIL')).toBeTruthy()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM