代码之家  ›  专栏  ›  技术社区  ›  Kayote

如何改进这个Jest测试来测试自定义操作

  •  0
  • Kayote  · 技术社区  · 5 年前

    如何改进下面的测试。我觉得我刚刚写的这段代码可以写得更好。

    const fetchResult = auth => dispatch => {
      return dispatch({
        types: [FETCH_REQUEST, FETCH_SUCCESS, FETCH_FAILURE],
        promise: axios({
          method: 'get',
          url: url,
          headers: {
            Authorization: `Token ${auth}`,
          },
        }),
      });
    };
    

    // some code for store declaration & fixture is not shown here
    describe('The boats actions', () => {
      beforeEach(() => {
        // create a fresh store; 
        let store = reduxMockStore(appReducer);
      });
    
      const mockDispatch = f => {
        // mocked the dispatch arg so it lets the func execution to carry on
        let r = f(d => d);
        // right now, r.promise is in 'pending' state
        r.promise.then((result) => {
          expect(result.data).toEqual(ExpectedFixture);
        });
      };
    
      it('should match the expected signature when `fetchResults` is called', () => {
        nock(API_ENDPOINT)
          .get('/adminsearch/?&page=1')
          .reply(200, ExpectedFixture);
        mockDispatch(fetchResult('abc'));
    ...
    
    0 回复  |  直到 5 年前