Vue Test Utils-数据在两次测试之间仍然存在

问题描述 投票:0回答:1

我试图弄清楚如何使用新数据开始每个测试,但是我似乎无法使其正常工作。

这是我的测试文件之一:

import { createLocalVue, shallowMount } from '@vue/test-utils'
import Vue from 'vue'
import Vuetify from 'vuetify'
import VueFormGenerator from 'vue-form-generator'
import CustomerDetailsTabGroups from '@/modules/customer/view/component/CustomerDetailsTabGroups.vue'
import { customerMock } from './mocks/customerMock'

Vue.use(Vuetify)
Vue.use(VueFormGenerator)

const localVue = createLocalVue()

function createConfig (overrides) {
  let vuetify = new Vuetify()
  const config = {
    localVue,
    vuetify,
    mocks: {
      $translate: (msg) => msg
    },
    data: () => {
      return {
        schema: {},
        customerData: customerMock,
        groups: [
          {
            id: '111',
            name: 'Gruppe1',
            mainGroup: false
          },
          {
            id: '222',
            name: 'Gruppe2',
            mainGroup: false
          }
        ]
      }
    },
    propsData: {
      customer: customerMock,
      tabIndex: 1
    },
    sync: false
  }
  return overrides ? Object.assign(config, overrides) : config
}

describe('CustomerDetailsTabGroups.vue', () => {
  let wrapper

  beforeEach(() => {
    wrapper = shallowMount(CustomerDetailsTabGroups, createConfig(null))
  })

  afterEach(() => {
    wrapper.destroy()
    wrapper = null
  })

  test('renders correctly', () => {
    expect(wrapper.element).toMatchSnapshot()
  })

  test('watcher assign customer to customerData', async () => {
    wrapper = shallowMount(CustomerDetailsTabGroups, createConfig({
      propsData: {
        customer: { id: 'test' },
        tabIndex: 1
      }
    }))
    await wrapper.vm.$nextTick()
    expect(wrapper.vm.$data.customerData.id).toEqual('test')
  })

  test('addGroupToCustomer adds group to customer.customerGroups, sets mainGroup if no other mainGroup present and ' +
    'removes group from groups', () => {
    wrapper.vm.$data.customerData.customerGroups[0].mainGroup = false
    const castedWrapper = wrapper.vm as any
    castedWrapper.addGroupToCustomer(wrapper.vm.$data.groups[0])
    expect(wrapper.vm.$data.customerData.customerGroups.length).toEqual(2)
    expect(wrapper.vm.$data.customerData.customerGroups[1].mainGroup).toBeTruthy()
    expect(wrapper.vm.$data.groups.length).toEqual(1)
  })

  test('removeGroupFromCustomer removes group from customer.customerGroups, sets first customerGroup.mainGroup ' +
    'true if no other mainGroup present and adds group to groups', async () => {
    await localVue.nextTick()
    const castedWrapper = wrapper.vm as any
    castedWrapper.addGroupToCustomer(wrapper.vm.$data.groups[0])
    expect(wrapper.vm.$data.groups.length).toEqual(1)
    expect(wrapper.vm.$data.customerData.customerGroups.length).toEqual(2)
    castedWrapper.removeGroupFromCustomer(wrapper.vm.$data.customerData.customerGroups[0])
    expect(wrapper.vm.$data.customerData.customerGroups.length).toEqual(1)
    expect(wrapper.vm.$data.customerData.customerGroups[0].mainGroup).toBeTruthy()
    expect(wrapper.vm.$data.groups.length).toEqual(2)
  })
})

这就是我尝试测试的组件:

import Vue from 'vue'
import customerMixin from '@/modules/customer/mixins/customerMixin'
import { Customer, CustomerGroup } from '@/modules/customer/type/types'
import {
  QUERY_CUSTOMER_GROUPS,
  QUERY_CUSTOMER_GROUPS_BY_ID
} from '@/modules/customer/graphql/queries'
import OverflowTooltip from '@/components/OverflowTooltip.vue'
import Event from '@/enum/event'

export default Vue.extend({
  name: 'CustomerDetailsTabData',
  mixins: [customerMixin],
  components: {
    OverflowTooltip
  },
  props: {
    customer: {
      type: Object,
      required: true
    },
    tabIndex: {
      type: Number,
      required: true
    }
  },

  data: function () {
    return {
      customerData: { customerGroups: [] } as Customer,
      groups: {}
    }
  },

  watch: {
    customer: {
      immediate: true,
      handler (customer: Customer): void {
        this.customerData = customer
        if (!this.customerData.customerGroups) {
          this.customerData.customerGroups = []
        }
      }
    }
  },

  methods: {
    addGroupToCustomer (groupToAdd: CustomerGroup): void {
      const customerMainGroup = this.customerData.customerGroups.find(group => group.mainGroup)
      groupToAdd.mainGroup = this.customerData.customerGroups.length === 0 || !customerMainGroup
      this.$set(this.customerData.customerGroups, this.customerData.customerGroups.length, groupToAdd)
      this.groups = this.groups.filter(group => group.id !== groupToAdd.id)
    },

    removeGroupFromCustomer (groupToRemove: CustomerGroup): void {
      this.customerData.customerGroups = this.customerData.customerGroups.filter(group => group.id !== groupToRemove.id)
      if (groupToRemove.mainGroup && this.customerData.customerGroups.length > 0) {
        this.changeSelectedMainGroup(this.customerData.customerGroups[0])
        groupToRemove.mainGroup = false
      }
      this.$set(this.groups, this.groups.length, groupToRemove)
      this.groups.sort((group1, group2) => group1.id - group2.id)
    },

    changeSelectedMainGroup (clickedGroup: CustomerGroup): void {
      this.customerData.customerGroups.forEach(group => {
        group.mainGroup = group.id === clickedGroup.id
      })
    }
  }
})

无论我尝试什么,测试4都会失败,因为它具有来自先前测试的组。以为wrapper.destroy()和wrapper = null会擦除它们之间的数据,但是不起作用。将不胜感激。

vue.js jest vue-test-utils
1个回答
0
投票

天哪,对于每个面临相同问题的人,我都发现了我的问题。并不是不是每次都用新数据重新创建包装器。那行得通。我的问题是,我直接将模拟绑定到测试,并且它们更改了模拟本身,因此在每次测试后,模拟都有不同的数据。对我来说JSON.parse(JSON.stringify(mock)))的模拟就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.