Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在axios调用后获得更新的包装器_React Native_Jestjs_Axios_React Hooks_Enzyme - Fatal编程技术网

React native 如何在axios调用后获得更新的包装器

React native 如何在axios调用后获得更新的包装器,react-native,jestjs,axios,react-hooks,enzyme,React Native,Jestjs,Axios,React Hooks,Enzyme,我有一个如下所示的函数,我正在尝试测试rowDoubleClicked函数。 我模拟了axios解析值,我可以看到getAccountData函数正在被覆盖,这意味着对dataArray.isLoading的更新将是错误的 但是,在调试包装器时,在我的测试中。它总是点击if语句来呈现loading div,而不是grid组件,我试图弄清楚如何让它呈现grid,以便调用rowdubleclicked函数 我尝试过更新包装器,但它保持不变。 我还尝试在组件上执行awat waitForElement

我有一个如下所示的函数,我正在尝试测试rowDoubleClicked函数。 我模拟了axios解析值,我可以看到getAccountData函数正在被覆盖,这意味着对dataArray.isLoading的更新将是错误的

但是,在调试包装器时,在我的测试中。它总是点击if语句来呈现loading div,而不是grid组件,我试图弄清楚如何让它呈现grid,以便调用rowdubleclicked函数

我尝试过更新包装器,但它保持不变。 我还尝试在组件上执行awat waitForElement,但它只是超时了

import React,{useState}来自“React”;
从“axios”导入axios;
常量MyComponent=(道具)=>{
让网格;
常量数据数组={
错误文本:“”,
行数据:“”,
孤岛加载:是的,
};
const[data,setData]=useState();
如果(未定义!==数据){
dataArray.errorText=data.errorText;
dataArray.isLoading=data.isLoading;
dataArray.rowData=data.rowData;
}
const setShow=props.functions;
const getAccountData=async()=>{
等待axios
.get(props.endpoint)
。然后((结果)=>{
dataArray.rowData=结果;
})
.catch((错误)=>{
dataArray.errorText=错误;
});
dataArray.isLoading=false;
setData(数据阵列);
};
const handleClose=()=>setShow(false);
常量行双击=()=>{
//一些行动
};
如果(dataArray.errorText!=''){
网格=(
错误

); }else if(dataArray.isLoading){ getAccountData(); 网格=( 装载

); }else if(dataArray.rowData!=''){ 网格=; } 返回( {grid} ); }; 导出默认MyComponent;
MyComponentView

import React from 'react'
import MyComponent from ''

const MyComponentView = (props) => {
 
  const [select, setSelect] = React.useState('')
  const [show, setShow] = React.useState(false)
  const [selectedSearchBy, setSearchBy] = React.useState('')
  const [selectedValue, setSearchByValue] = React.useState('')

  const handleSearchIconClick = () => {
    setShow(true)
  }
 
  const handleOnChange = (e) => {
    setSearchBy(e.selectedOptionVal)
    setSearchByValue(e.value)
  }

 
  return (
    <div>
        <form
          action={`${endpoint`}
          method='post'
          onSubmit={handleSubmit}
        >
          <input type='hidden' id='searchBy' name='searchBy' value={selectedSearchBy} />
          <input type='hidden' id='searchValue' name='searchValue' value={selectedValue} />
          <Button data-testid='accessButton' id='accessButton' block color='primary'>
          Search
          </Button>
        </form>
        {show && (
          <MyComponent
            show
            functions={setShow}
            onModalApplyClick={handleApply}
            endpoint={endpoint}
          />
        )}
      </div>
    </div>
  )
}

export default MyComponentView
从“React”导入React
从“”导入MyComponent
常量MyComponentView=(道具)=>{
const[select,setSelect]=React.useState(“”)
const[show,setShow]=React.useState(false)
常量[selectedSearchBy,setSearchBy]=React.useState(“”)
常量[selectedValue,setSearchByValue]=React.useState(“”)
const handleSearchIconClick=()=>{
设置显示(真)
}
常数变化=(e)=>{
设置搜索条件(如selectedOptionVal)
setSearchByValue(e.value)
}
返回(
搜寻
{show&&(
)}
)
}
导出默认MyComponentView
这是我目前的测试

it('在网格上执行双击',异步()=>{
让包装纸;
让网格;
axios.get.mockResolvedValue(dataJSON);
包装=装载(
);
grid=wrapper.find(网格);
wrapper.update();
wait waitForElement(()=>expect(grid.toBeTruthy());
invoke('handleRowDoubleClicked')();
等待行动(()=>Promise.resolve());
});

因此,您的
axios.get.mockResolvedValue
似乎无法正常工作。在这种情况下,我个人只需使用
axios模拟适配器

另外,似乎
waitForElement
已被弃用。用jest的
done()
做一个简单的
setTimeout
怎么样


请发布组件的渲染逻辑。
import MockAdapter from 'axios-mock-adapter';

it('...', (done) => {
    const mock = new MockAdapter(axios);
    mock.onPost().reply(200, dataJSON);

    //your test's logic

    setTimeout(() => {
        expect(grid).toBeTruthy();
    }, 1000);  //or any reasonable delay
});