亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Enzyme、Sinon 和 ChainonChange 測試 onChange 不被調用?

使用 Enzyme、Sinon 和 ChainonChange 測試 onChange 不被調用?

jeck貓 2023-07-20 10:55:51
我正在嘗試為rc-select編寫一些測試。我想做的測試是檢查 onChange 函數是否被調用。到目前為止我所擁有的:測試組件,使用 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)我應該在 find() 調用中使用另一個選擇器嗎?我覺得我已經嘗試了所有的選擇。
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

工作測試:(需要通過包裝器的 props 調用 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;

    });


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號