【问题标题】:StencilJS E2E testing: How do you dispatch a custom event?StencilJS E2E 测试:如何调度自定义事件?
【发布时间】:2019-08-08 23:47:22
【问题描述】:

如何在 StencilJS E2E 测试中将 CustomEvent 分派给元素:

const page = await newE2EPage({html: `<my-component></my-component>`});
let component = await page.find('my-component');

?? component.dispatch(new Event('my-event'));

在上面的代码中,我尝试将 customEvent 分派给我的组件,以便测试它是否正确响应。但我在 newE2EPage 中看不到任何这样做的方法。有什么建议吗?

【问题讨论】:

    标签: typescript web-component e2e-testing stenciljs


    【解决方案1】:

    Stenciljs 在E2EElement class 上提供了一个函数 triggerEvent。

    你可以这样做:

    const page = await newE2EPage();
    await page.setContent('some-markup-with-your-component');
    const el = page.find('#element-that-would-emit');
    el.triggerEvent('some-custom-event');
    await page.waitForChanges();
    //Your expect assertion here.
    

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2020-05-07
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多