2 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
我最終使用了@testing-library/user-event 庫。我fireEvent用替換userEvent為 用于Tab媒體。
我的代碼現(xiàn)在看起來像這樣:
? ? 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貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
如果您執(zhí)行以下操作,函數(shù) fireEvent.keyDown 應(yīng)該適合您:
組件和測(cè)試都必須使用 KeyDown,(使用 KeyPressed 是我的問題之一)
然后可以像這樣調(diào)用 fireEvent:
??fireEvent.keyDown(Element,?{key:?'Tab',?code:?'Tab',?charCode:?9})
添加回答
舉報(bào)