模板无法使用 Jest 测试 mouseenter\mouseleave 事件

发布于 01-09 13:17 字数 1336 浏览 2 评论 0原文

我使用 Stencil 构建了一个简单的按钮组件,并分配了 4 个事件(onMouseDownonMouseUponMouseEnteronMouseLeave)按钮。该组件如下所示:

   .
   .
   .
   @State() buttonState: string ='disabled';
   .
   .
   .
   someInternalLogic(eventName: Events) {
     ...//just sets a state variable of this.buttonState
   }
   render() {
     return (
       <button
         onMouseDown={() => this.someInternalLogic(Events.MOUSEDOWN)}
         onMouseUp={() => this.someInternalLogic(Events.MOUSEUP)}
         onMouseEnter={() => this.someInternalLogic(Events.MOUSEENTER)}
         onMouseLeave={() => this.someInternalLogic(Events.MOUSELEAVE)}
       >
       </button>
     );
   }

我对一般测试尤其是 Jest 不太熟悉。我很难理解如何综合测试这些事件。我想出了一个可行的解决方法,但显然不是可行的方法。
解决方法:

  it('should mouseleave', async () => {
    const button = await page.root.shadowRoot.querySelector('button');
    
    const mouseleave = new window.Event("mouseleave", {
      bubbles: false,
      cancelable: false
    });

    let mouseleaveBool = false;
    button.addEventListener("mouseleave", e=>{
      mouseleaveBool = true;
    });
    await button.dispatchEvent(mouseleave);

    await page.waitForChanges();
    expect(mouseleaveBool ).toBeTruthy();
  });

I built a simple button component using Stencil and assigned 4 events (onMouseDown, onMouseUp onMouseEnter, onMouseLeave), to the button. The component looks like this:

   .
   .
   .
   @State() buttonState: string ='disabled';
   .
   .
   .
   someInternalLogic(eventName: Events) {
     ...//just sets a state variable of this.buttonState
   }
   render() {
     return (
       <button
         onMouseDown={() => this.someInternalLogic(Events.MOUSEDOWN)}
         onMouseUp={() => this.someInternalLogic(Events.MOUSEUP)}
         onMouseEnter={() => this.someInternalLogic(Events.MOUSEENTER)}
         onMouseLeave={() => this.someInternalLogic(Events.MOUSELEAVE)}
       >
       </button>
     );
   }

I'm new to testing in general and Jest in particular. I'm having troubles understanding how to test these events synthetically. I've come up with a workaround which works, but is obviously not the way to go.

The workaround:

  it('should mouseleave', async () => {
    const button = await page.root.shadowRoot.querySelector('button');
    
    const mouseleave = new window.Event("mouseleave", {
      bubbles: false,
      cancelable: false
    });

    let mouseleaveBool = false;
    button.addEventListener("mouseleave", e=>{
      mouseleaveBool = true;
    });
    await button.dispatchEvent(mouseleave);

    await page.waitForChanges();
    expect(mouseleaveBool ).toBeTruthy();
  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷2025-01-16 13:17:03

您可以直接在组件实例上调用事件处理程序,而不是调度事件

因此对于此组件

export class TestBtn {
  onMouseLeave() {
    // do something
  }

  render() {
    return (
      <Host>
        <button onMouseLeave={() => this.onMouseLeave()}>Test</button>
      </Host>
    );
  }
}

测试可以如下所示

describe('test-btn', () => {
  it('does something on mouse leave', async () => {
    // arrange
    const page = await newSpecPage({
      components: [TestBtn],
      html: `<test-btn></test-btn>`,
    });
    let component = page.rootInstance as TestBtn;

    // act
    component.onMouseLeave();

    // assert
    // check if did something
  });
});

Instead of dispatching events you can directly call event handlers on your component instance

So for this component

export class TestBtn {
  onMouseLeave() {
    // do something
  }

  render() {
    return (
      <Host>
        <button onMouseLeave={() => this.onMouseLeave()}>Test</button>
      </Host>
    );
  }
}

Test can look like this

describe('test-btn', () => {
  it('does something on mouse leave', async () => {
    // arrange
    const page = await newSpecPage({
      components: [TestBtn],
      html: `<test-btn></test-btn>`,
    });
    let component = page.rootInstance as TestBtn;

    // act
    component.onMouseLeave();

    // assert
    // check if did something
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文