jeck貓
2023-07-20 10:55:51
我正在嘗試為rc-select編寫一些測試。我想做的測試是檢查 onChange 函數(shù)是否被調(diào)用。到目前為止我所擁有的:測試組件,使用 rc-select 的樣式版本:ReactSelectTestComponent.jsximport React from 'react';import Select from '../../packages/lab/src/select/Select.jsx';const ReactSelectTestComponent = (props) => {? ? const { options } = props;? ? const onChange = (event) => {? ? ? ? if (props.onChange) {? ? ? ? ? ? props.onChange(event)? ? ? ? }? ? }? ? return (? ? ? ? <div>? ? ? ? ? ? <Select? ? ? ? ? ? ? ? name='test'? ? ? ? ? ? ? ? value='one'? ? ? ? ? ? ? ? options={options}? ? ? ? ? ? ? ? onChange={onChange}? ? ? ? ? ? />? ? ? ? </div >? ? )}export default ReactSelectTestComponent;選擇.test.js:import SelectTestComponent from './SelectTestComponent'import sinon from 'sinon';import { expect as chaiExpect } from 'chai';? // Using Expect styledescribe('Testing Select component', () => {? ? it('should call onChange', () => {? ? ? ? const onChange = sinon.spy();? ? ? ? const options =? ? ? ? ? ? [? ? ? ? ? ? ? ? { label: 'one', value: 'one' },? ? ? ? ? ? ? ? { label: 'two', value: 'two' }? ? ? ? ? ? ];? ? ? ? const wrapper = mount(<SelectTestComponent onChange={onChange} options={options} />);? ? ? ? console.log("wrapper.debug()", wrapper.debug())? ? ? ? const selectWrapper = wrapper.find('Select').first();? ? ? ? selectWrapper.simulate('change', { target: { value: "testtest" } })? ? ? ? selectWrapper.update()? ? ? ? chaiExpect(onChange.called).to.be.true;? ? });})但是,我收到此錯誤。Expected value? ?true? ? Received:? ? ? false? ??? ? Message:? ? ? expected false to be true? ? ? 38 |? ? ? ? ?selectWrapper.update()? ? ? 39 |?? ? > 40 |? ? ? ? ?chaiExpect(onChange.called).to.be.true;? ? ? ? ?|? ? ? ? ?^? ? ? 41 |? ? ?});? ? ? 42 |?? ? ? 43 |? ? ?/*? ? ? at Object.<anonymous> (tests/src/Select.test.js:40:9)我應(yīng)該在 find() 調(diào)用中使用另一個選擇器嗎?我覺得我已經(jīng)嘗試了所有的選擇。
1 回答

神不在的星期二
TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個贊
工作測試:(需要通過包裝器的 props 調(diào)用 onChange。
it("Checking if onChange prop function is called", () => {
const onChange = sinon.spy();
const wrapper = mount(
<SelectTestComponent onChange={onChange} options={options} />
);
const func = wrapper.find("Select").props().onChange;
console.log(wrapper.find("Select").props());
func({ target: { value: "test" } });
chaiExpect(onChange.called).to.be.true;
});
添加回答
舉報(bào)
0/150
提交
取消