代码之家  ›  专栏  ›  技术社区  ›  kossmoboleat

如何使用react测试库为react ui菜单触发onClose?

  •  0
  • kossmoboleat  · 技术社区  · 7 年前

    我正在使用带有 onClose 当菜单失去焦点时触发的道具。我无法触发此状态,即使我在菜单外添加了一个组件的点击,或者在菜单外添加了一个输入元素的焦点。

    const UserMenu: React.FunctionComponent<UserMenuProps> = ({ className }) => {
      const signedIn = useAuthState(selectors => selectors.SignedIn);
      const username = useAuthState(selectors => selectors.Username);
      const messages = useMapState((state: AppRootState) => state.app.messages);
      const signOut = useSignOut();
    
      const [open, updateOpenStatus] = useState(false);
      const anchorRef = useRef(null);
    
      if (!signedIn) {
        return <div data-testid="user-menu" className={className}>
          <LinkButton to={ROUTES.SignIn.link()}>{messages.SignIn.Title}</LinkButton>
          <LinkButton to={ROUTES.SignUp.link()}>{messages.SignUp.Title}</LinkButton>
          <LinkButton to={ROUTES.ConfirmSignUp.link()}>{messages.ConfirmSignUp.Title}</LinkButton>
        </div>;
      }
    
      return <div data-testid="user-menu" className={className}>
        <Grid container direction="row" alignItems="center">
          <Typography noWrap variant="subtitle2">
            <span id="username" className="bold">{username}</span>
          </Typography>
          <IconButton id="menu-toggle" buttonRef={anchorRef} onClick={() => updateOpenStatus(true)}>
            <AccountCircle/>
          </IconButton>
          <Menu
            anchorEl={anchorRef.current}
            anchorOrigin={{
              vertical: 'top',
              horizontal: 'right'
            }}
            transformOrigin={{
              vertical: 'top',
              horizontal: 'right'
            }}
            open={open}
            onClose={() => updateOpenStatus(false)}
          >
            <MenuItem id="sign-out" onClick={() => { updateOpenStatus(false); signOut(); }}>{messages.SignOut.Action}</MenuItem>
          </Menu>
        </Grid>
      </div>;
    };
    

        it('should open and close menu', async () => {
          const { getByTestId } = render(<><UserMenu/>
            <input data-testid="other"/>
          </>, { state });
    
          fireEvent.click(getByTestId('menu-toggle'));
    
          expect(MockMenu).toHaveBeenLastCalledWith(expect.objectContaining({ open: true }), {});
    
          fireEvent.focus(getByTestId('other'));
    
          expect(MockMenu).toHaveBeenLastCalledWith(expect.objectContaining({ open: false }), {});
        });
    

    我也试过了 fireEvent.click(getByTestId('other')); 没有成功。

    question 因为酶有一个解决办法,就是使用 tree.find(Menu).simulate("close"); react-testing-library .

    0 回复  |  直到 6 年前
        1
  •  6
  •   Dylan Walker    6 年前

    您可以通过单击菜单生成的背景来触发关闭。我发现最简单的方法就是通过屏幕选择背景 getByRole('presentation') @testing-library .

    测试代码:

    it('should open and close the menu', () => {
      const { getByTestId, getByRole } = render(<UserMenu />);
    
      fireEvent.click(getByTestId('menu-toggle'));
    
      // Get the backdrop, then get the firstChild because this is where the event listener is attached
      fireEvent.click(getByRole('presentation').firstChild));
    
      expect(MockMenu).toHaveBeenLastCalledWith(expect.objectContaining({ open: false }), {});
    });
    
    推荐文章