function Finaltable() {
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState({ loading: true });
const [curIndex, SetcurIndex] = useState(1);
const [perPage, setPerPage] = useState(10);
const [scanAPIdata, setScanData] = useState([]);
/*istanbul ignore next*/
const dispatch = useDispatch();
let [scanCount, setScanCount] = useState([]);
//dispatch({type: 'fetchdata'}),
/*istanbul ignore next*/
const account_id = useSelector(state => state.app.user ? state.app.user.crm_account_id : '');
let pagination = GetPager(scanCount, curIndex, perPage);
let SortData = getCookie("SortScan");
const defaultsort = [
{
"field": "name",
"direction": "ascending",
id: "Profile Name", desc: "false"
}
];
const status = () => { return SortData === undefined || SortData.length === 0 ? defaultsort : JSON.parse(SortData); };
const [sort, setSort] = useState(status);
useEffect(() => {
dispatch({ type: sagaActions.FETCH_DATA_SAGA });
let finalData = {
account_id: account_id,
range: {
start: pagination.startIndex,
end: pagination.endIndex
},
order: sort
};
postScanListAPI(finalData).then((res) => {
setScanCount(res.headers["total-count"]);
setScanData(res);
setLoading({ loading: false });
});`enter code here`
}, [sort, account_id]);
我正在为此编写测试代码,但在使用 jest 模拟 useDispatch 和 useSelector 时出错。
const account_id = useSelector(state => state.app.user ? state.app.user.crm_account_id : '');
const dispatch = useDispatch();
我如何为以上两行编写测试代码。
答案 0 :(得分:0)
为选择器模拟整个 redux。使用 jest.fn() 全局模拟调度