小编pun*_*ira的帖子

为对象的一个​​属性设置默认 props

我正在尝试为获取对象的组件设置默认道具。问题是我需要将默认属性应用于对象缺少的属性,但它仅在未设置整个对象时才有效:

const App = ({data}) =>{
    return(
        <div></div>
    )
}

App.defaultProps = {
    data : {
        C : "blah"
    }
}

App.propTypes = {
    data : PropTypes.shape({
        A : PropTypes.string.isRequired,
        B : PropTypes.string.isRequired,
        C : PropTypes.string.isRequired,
    }).isRequired
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

reactjs react-proptypes

6
推荐指数
1
解决办法
3133
查看次数

如何在 redux-saga 中测试 api 调用

我有这个传奇效果,它调用 API 并在成功时分派一个操作:

export function* getThemEffect() {
  try {
    yield put(requestActoin());
    const data: AxiosResponse<ServerResponseSchema> = yield call(getStuff);
    yield put(successAction(data.data.data));
  } catch (err: any) {
    yield put(failureAction(err?.response?.data || null));
  }
}
Run Code Online (Sandbox Code Playgroud)

这是辅助函数:

export function getStuff() {
  const config: AxiosRequestConfig = {
    method: "GET",
    url: "https://somewhere.com/api/get"
  };
  return axios(config);
}
Run Code Online (Sandbox Code Playgroud)

这个传奇的测试服看起来像这样:

import * as api from "../api";

const getStuffSpy = jest.spyOn(api, "getStuff");

describe("search saga", () => {
   let gen: Generator, response: any, getStuffMock: jest.Mock;
   beforeEach(() => {
      getStuffSpy.mockClear();
      gen = getThemEffect();
      getStuffMock …
Run Code Online (Sandbox Code Playgroud)

testing reactjs jestjs redux-saga react-redux

4
推荐指数
1
解决办法
2940
查看次数