第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

按下選項(xiàng)卡按鈕時(shí)如何測(cè)試哪個(gè)輸入具有焦點(diǎn)

按下選項(xiàng)卡按鈕時(shí)如何測(cè)試哪個(gè)輸入具有焦點(diǎn)

Qyouu 2023-06-09 10:42:17
我正在構(gòu)建一個(gè) React 自動(dòng)完成組件并使用 Jest 和 React-testing-library 對(duì)其進(jìn)行測(cè)試。我有兩個(gè)輸入。當(dāng) input1(具有自動(dòng)完成功能)處于焦點(diǎn)時(shí),Tab如果 input1 不為空,則單擊該按鈕應(yīng)該使用文本自動(dòng)填充 input1,或者如果 input1 為空,則將焦點(diǎn)移至 input2(這是表單的默認(rèn)行為)。表單.jsx    const onKeyDown = (e) => {        if(e.key === 'Tab' && e.target.value !== '') {            setInput1Text('some-autocomplete-text');            e.preventDefault(); //prevent focus from moving to the next input(input2)        }    };表單.test.jsx    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];        fireEvent.change(input1, { target: { value: '' } });        fireEvent.keyDown(input1, { key: 'Tab' });        expect(input2).toBe(document.activeElement) //failing, activeElement returns <Body>        // OR        expect(input2).toHaveFocus(); //failing as well.    });目前,在我的測(cè)試中,document.activeElement不斷返回Body元素,但我希望它返回兩個(gè)輸入中的任何一個(gè)。也expect(input2).toHaveFocus()失敗了。如何測(cè)試焦點(diǎn)是否已從 input1 移至 input2?
查看完整描述

2 回答

?
12345678_0001

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

? ? });

查看完整回答
反對(duì) 回復(fù) 2023-06-09
?
MM們

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})
查看完整回答
反對(duì) 回復(fù) 2023-06-09
  • 2 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)