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)