0

私はテストに慣れていないので、これが私がテストする「想定」されているものであるかどうかさえわかりませんが、次のようになります。

非同期アクション作成者のテストを作成する方法については、 https://github.com/reactjs/redux/blob/master/docs/recipes/WritingTests.mdの例に従っています。ここに私がテストしているコードがあります:

export function receiveRepresentatives(json) {
  return {
    type: RECEIVE_REPRESENTATIVES,
    representatives: json.objects
  }
}

export function getRepresentatives (zipcode) {
  return dispatch => {
    dispatch(changeFetching())
    return fetch('/api/representatives' + zipcode)
        .then(response => response.json())
        .then(json => dispatch(receiveRepresentatives(json))) 
  }
}

私のテスト フレームワークは、nock と configureMockStore を備えた mocha/chai です。/api/representative への呼び出しを nock でモックしたいのですが、方法がわかりません。

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

describe('async actions', () => {
  afterEach(() => {
    nock.cleanAll()
  })

  it('creates RECEIVE_REPRESENTATIVES when fetching representatives has been done', (done) => {
    nock('http://localhost')
      .get('/api/representatives')
      .reply('200', { objects: { name: 'Barbara Lee'} } )

    const expectedActions = [
      { type: RECEIVE_REPRESENTATIVES, representatives: { objects: { name: 'Barbara Lee'} } }
    ]

    const store = mockStore({}, expectedActions, done)
    store.dispatch(getRepresentatives(94611))
      .then(() => {
        const actions = store.getActions()
        expect(actions[0].type).toEqual(receiveRepresentatives())

        done()
      })
  })
})
4

0 に答える 0