在反应测试库中按shift并点击多次



如何在react测试库中一直保持shift并点击

我已经试过了,但是没有成功

const layerElement = screen.getByText("Layer one");
fireEvent.click(layerElement, { shiftKey: true });
const groupElement = screen.getByText("Group 1");
fireEvent.click(groupElement, { shiftKey: true });
fireEvent.contextMenu(groupElement, { shiftKey: true });

我自己还没有尝试过,但我几乎可以肯定你可以实现,但使用最新的userEventapi。它是RTL的补充库,可以更好地模拟用户交互,而不仅仅是触发抽象的用户事件,因此通常建议在fireEventapi上使用它。你可以在这里阅读userEventapi以及如何安装和设置它

之后,您应该能够使用user.keyboarduser.click的组合来实现您的目标。

docs-user.keyboard允许您模拟按键而不释放它:

"键可以通过添加>到描述符的末尾。如果这会导致重复的keydown事件,您可以添加重复次数。如果在此之后也要释放密钥,在描述符末尾添加斜杠/。">

keyboard('{a>}') // press "a" without releasing it
keyboard('{a>5}') // press "a" without releasing it and trigger 5 keydown
keyboard('{a>5/}') // press "a" for 5 keydown and then release it

先前按下的键可以通过在描述符前面加上前缀来解除/。

keyboard('{/a}') // release a previously pressed a

所以我认为你应该可以这样做:

user.keyboard("{Shift>}");
user.click(component);
user.keyboard("{/Shift}");

最新更新