如何触发点击外部事件?

问题描述 投票:0回答:3

我正在为 React 中的弹出窗口组件编写单元测试和 e2e 测试。 当我单击组件外部时,我应该检查弹出窗口是否隐藏。 我使用 Jest + Enzyme 进行单元测试,使用 Cypress 进行 e2e 测试。 有人知道该怎么做吗?

我在赛普拉斯中尝试如下。

cy.get('[data-test-id="popover-container"]').click(-20, -20, {force: true});

但是点击的点实际上在弹出窗口之外,但是不起作用。

react-tiny-popover
库用于显示弹出窗口,如下所示:

<Popover
      content={({ position, targetRect, popoverRect }) => (
        <ArrowContainer
          position={position}
          targetRect={targetRect}
          popoverRect={popoverRect}
          arrowColor={'#ccc'}
          arrowSize={10}
        >
          <div data-test-id="popover-container">
            <Content/>
          </div>
        </ArrowContainer>
      )}
      isOpen={visible}
      onClickOutside={() => hideOnOutsideClick && setVisible(false)}
      position={position}
    >
      <div onClick={() => setVisible(!visible)}>{children}</div>
    </Popover>
javascript click mouseevent cypress
3个回答
31
投票

您只需单击身体上的某个位置即可:

Cypress.Commands.add('clickOutside', function(): Chainable<any> {
  return cy.get('body').click(0,0); //0,0 here are the x and y coordinates
});

测试中:

cy.get('[data-test-id="popover-container"]').clickOutside();

点击参考


9
投票

取自其他答案,您可以:

cy.get('body').click(0,0);

无需添加命令,除非您要多次使用它


0
投票

谢谢,这真的对我很有帮助,很棒的命令

© www.soinside.com 2019 - 2024. All rights reserved.