2 回答

TA贡献1802条经验 获得超5个赞
我最终使用了@testing-library/user-event 库。我fireEvent用替换userEvent为 用于Tab媒体。
我的代码现在看起来像这样:
import userEvent from '@testing-library/user-event';
test('pressing Tab button should move focus to input2 as input1 is empty', () => {
const input1 = container.getElementsByTagName('input')[0];
const input2 = container.getElementsByTagName('input')[1];
input1.focus();
fireEvent.change(input1, { target: { value: '' } });
userEvent.tab(); //this line made it work
expect(input2).toHaveFocus(); //passing now
});

TA贡献1886条经验 获得超2个赞
如果您执行以下操作,函数 fireEvent.keyDown 应该适合您:
组件和测试都必须使用 KeyDown,(使用 KeyPressed 是我的问题之一)
然后可以像这样调用 fireEvent:
fireEvent.keyDown(Element, {key: 'Tab', code: 'Tab', charCode: 9})
添加回答
举报