美文网首页
Vitest + Vue Test Utils + vue3 单

Vitest + Vue Test Utils + vue3 单

作者: 苏苡 | 来源:发表于2023-05-24 08:20 被阅读0次

    import Table from '@/tableBlock.vue'

    import { describe, test, expect, vi } from 'vitest'

    import { mount } from '@vue/test-utils'

    import sinon from 'sinon' '

    // mock store 配置

    import { createStore } from 'vuex'

    const store = createStore()

    // mock router 配置

    import { createRouter, createWebHistory } from 'vue-router'

    const router = createRouter({

          history: createWebHistory(),

          routes: [{ path: '/', component: '' }]

    })

    // 页面初始化配置  props,store,router,validate,resetFields,$t

    const config = {

          provide: {

                SearchBlock() {

                      return 'SearchBlock'

                }

          },

         props: {

                tableData: [{ checked: true }]

          }

          global: {

                plugins: [[store], [router]],

                stubs: {

                    'ep-form': {

                          render: () => {},

                          methods: {

                                     validate: vi.fn().mockResolvedValue({ isValid: true }).mockReturnValueOnce({ isValid: false }),

                                     resetFields: () => true

                            }

                     }

                 },

                mocks: {

                      state,

                       store: {

                            commit: sinon.stub()

                      }

                }

          },

          methods: {

                $t: sinon.stub()

          }

    }

    test('table组件 Table  ', () => {

          // 接口模拟

          vi.mock('@/api/xx/xx', () => {

              return {

                  getList: vi.fn()

                    .mockResolvedValue({ returnCode: '0000', result: { data: []} })

                    .mockResolvedValueOnce({ returnCode: '1111' })

            }

       })

        const wrapper = mount(TableBlock, config )

        expect(wrapper.classes()).toContain('table-block')

        expect(wrapper.isVisible()).toBe(true)

        wrapper.unmount()

      })

    相关文章

      网友评论

          本文标题:Vitest + Vue Test Utils + vue3 单

          本文链接:https://www.haomeiwen.com/subject/xyuasdtx.html