Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: 使用 wrapper.trigger('click') 无法正常触发 el-radio #2508

Closed
JZLeung opened this issue Sep 6, 2024 · 1 comment
Closed

Bug: 使用 wrapper.trigger('click') 无法正常触发 el-radio #2508

JZLeung opened this issue Sep 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@JZLeung
Copy link

JZLeung commented Sep 6, 2024

Describe the bug

在项目的组件中使用了 ElRadio 组件,需要进行测试。

在测试中:

const [radio1, radio2] = wrapper.findAll('.el-radio')
await radio2.trigger('click')
expect(radio2.classes()).toContain('is-checked') // AssertionError: expected [ 'el-radio' ] to include 'is-checked'

无法【点击】到 radio2,使测试出错

To Reproduce

Element Plus Playground

Expected behavior

测试正常通过

Related information:

"vue": "3.4.21",
"element-plus": "2.6.0",

"vitest": "^1.5.2",
"@vue/test-utils": "^2.4.5",

Additional context

在控制台中,可以通过 document.querySelector('.el-radio').click() 正常触发,但无法通过 dom.dispatchEvent() 触发,恰巧这是 @vue/test-utilswrapper.trigger 的实现原理。

@JZLeung JZLeung added the bug Something isn't working label Sep 6, 2024
@cexbrayat
Copy link
Member

Hi @JZLeung

I don't speak chinese, so I can only guess what the issue is, and answer in english, sorry.

You probably need to use attachTo in that case:

describe('Radio group', () => {
  it('create', async () => {
    const wrapper = mount(RadioComponent, { attachTo: document.body })
    await nextTick()

    const [radio1, radio2] = wrapper.findAll('.el-radio')
    expect(radio1.classes()).toContain('is-checked')

    await radio2.trigger('click')
    expect(radio2.classes()).toContain('is-checked')
  })
})

I think this should solve your issue. If not, open a repro on Stackblitz, and I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants