最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vue.js - How to simulate the behavior of closing v-dialog in Vitest? - Stack Overflow

programmeradmin0浏览0评论

I try the fireEvent to click outside or emitted 'update:modelValue' but the v-dailog element still visible. So how can I correctly simulate the closing behavior of v-dialog ? Thanks in advance for your help!

// ImportFile.test.js
it('opens and closes dialog when button is clicked', async () => {
    // render the component
    const wrapper = render(ImportFile, {
      global: {
        plugins: [createTestingPinia({createSpy: vi.fn}), vuetify],
      },
      attachTo: document.body
    })
    
    // check if the button is in the document
    const button = wrapper.getByText('Import')
    expect(button).toBeInTheDocument()
  
    // ensure the dialog is not initially visible
    const dialog = screen.queryByRole('dialog')
    expect(dialog).not.toBeInTheDocument()
    console.log(`before click: ${dialog}`)
  
    // simulate a click event
    await fireEvent.click(button);
  
    // check if the dialog is now visible
    const dialogAfterClick = screen.queryByRole('dialog')
    expect(dialogAfterClick).toBeInTheDocument()
    console.log(`after click: ${dialogAfterClick === null ? 'hidden' : 'visible'}`)

    // triggering dialog close
    wrapper.emitted('update:modelValue', false)
    await waitFor(() => {
      const dialogAfterClose = screen.queryByRole('dialog')
      console.log(`after close: ${dialogAfterClose === null ? 'hidden' : 'visible'}`)
    })
  })
发布评论

评论列表(0)

  1. 暂无评论