跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • React testing library 测试 multi-select 问题
未分類
5 4 月 2020

React testing library 测试 multi-select 问题

React testing library 测试 multi-select 问题

資深大佬 : hantsy 10

React testing library handleChange.mockClear 没有重置调用次数。

 expect(handleChange).toHaveBeenCalledTimes(2); 

源文件:

it('handles changes in multi select box', () => {     const handleChange = jest.fn();      const {container, getByRole} = render(        <select multiple role='select' onChange={handleChange}>          <option value=''>---</option>          <option value='one'>one</option>          <option value='two'>two</option>          <option value='three'>threeone</option>       </select>      )     const elements = container.querySelectorAll('option');     const select = getByRole('select');     expect(elements.length).toEqual(4);     expect(handleChange).not.toHaveBeenCalled();     //expect(elements[1].selected).toBe(true);      userEvent.selectOptions(select, ['two']);     expect(handleChange).toHaveBeenCalledTimes(1);     expect(elements[2].selected).toBe(true);      handleChange.mockClear();      userEvent.selectOptions(select, ['two', 'three']);     expect(handleChange).toHaveBeenCalledTimes(2);     expect(elements[2].selected).toBe(true);     expect(elements[3].selected).toBe(true);   }); 

大佬有話說 (1)

  • 主 資深大佬 : hantsy

    原来 userEvent.selectOptions(select, [‘two’, ‘three’]); 设置 selected values,两个值的数组会调用两次 onChange 。

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具