使用 fetch-mock 测试 React Redux 异步操作 - "Cannot read property 'then' of undefined"
Testing React Redux Async Actions with fetch-mock - "Cannot read property 'then' of undefined"
我在测试 react redux 异步操作时遇到了一些问题,由于某些原因 store.dispatch returns 未定义并且 .then 方法不起作用。
当我 console.log store.dispatch(actions.requestChartData()) 它给我以下错误:
fetch-mock: No fallback response defined for GET to
https://swapi.co/api/people
表示没有模拟响应并且未定义。
我不知道我的错误在哪里,在 url 或者我遗漏了什么。任何帮助将不胜感激
这是我的 action.js 代码:
import {
ROOT_URL,
API_KEY,
FETCH_POSTS,
CREATE_POST,
REQUEST_CHART_DATA_PENDING,
REQUEST_CHART_DATA_SUCCESS,
REQUEST_CHART_DATA_FAILED
} from '../constants/constants'
export const requestChartData = () => (dispatch) => {
dispatch({type: REQUEST_CHART_DATA_PENDING})
fetch('https://swapi.co/api/people')
.then(response => response.json())
.then(data => dispatch({type: REQUEST_CHART_DATA_SUCCESS,
labels: data.results.map(label => label.name),
chartData: data.results.map(chartData => chartData.height )}))
.catch(error => dispatch({type: REQUEST_CHART_DATA_FAILED, payload: error}))
}
还有我的 action.test.js 文件:
import * as actions from '../../actions/index'
import fetchMock from 'fetch-mock'
import {
ROOT_URL,
API_KEY,
FETCH_POSTS,
CREATE_POST,
REQUEST_CHART_DATA_PENDING,
REQUEST_CHART_DATA_SUCCESS,
REQUEST_CHART_DATA_FAILED
} from '../../constants/constants'
import thunkMiddleware from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
const mockStore = configureMockStore([thunkMiddleware])
describe('fetching api url', () => {
afterEach(() => {
fetchMock.restore()
})
it('creates REQUEST_CHART_DATA_SUCCESS when fetching has been done', () => {
fetchMock.getOnce('https://swapi.co/api/people', {
body: { todos: ['do something'] },
headers: { 'content-type': 'application/json' }
})
const expectedActions =
{ type: REQUEST_CHART_DATA_SUCCESS, body: { todos: ['do something'] } }
const store = mockStore({ todos: [] })
return store.dispatch(actions.requestChartData()).then(() => {
expect(store.getActions()).toEqual(expectedActions)
})
})
})
在您的 requestChartData 操作创建器中,return fetch 而不是仅仅调用它。
return fetch('/你的-api-路径')
我在测试 react redux 异步操作时遇到了一些问题,由于某些原因 store.dispatch returns 未定义并且 .then 方法不起作用。
当我 console.log store.dispatch(actions.requestChartData()) 它给我以下错误:
fetch-mock: No fallback response defined for GET to https://swapi.co/api/people
表示没有模拟响应并且未定义。
我不知道我的错误在哪里,在 url 或者我遗漏了什么。任何帮助将不胜感激
这是我的 action.js 代码:
import {
ROOT_URL,
API_KEY,
FETCH_POSTS,
CREATE_POST,
REQUEST_CHART_DATA_PENDING,
REQUEST_CHART_DATA_SUCCESS,
REQUEST_CHART_DATA_FAILED
} from '../constants/constants'
export const requestChartData = () => (dispatch) => {
dispatch({type: REQUEST_CHART_DATA_PENDING})
fetch('https://swapi.co/api/people')
.then(response => response.json())
.then(data => dispatch({type: REQUEST_CHART_DATA_SUCCESS,
labels: data.results.map(label => label.name),
chartData: data.results.map(chartData => chartData.height )}))
.catch(error => dispatch({type: REQUEST_CHART_DATA_FAILED, payload: error}))
}
还有我的 action.test.js 文件:
import * as actions from '../../actions/index'
import fetchMock from 'fetch-mock'
import {
ROOT_URL,
API_KEY,
FETCH_POSTS,
CREATE_POST,
REQUEST_CHART_DATA_PENDING,
REQUEST_CHART_DATA_SUCCESS,
REQUEST_CHART_DATA_FAILED
} from '../../constants/constants'
import thunkMiddleware from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
const mockStore = configureMockStore([thunkMiddleware])
describe('fetching api url', () => {
afterEach(() => {
fetchMock.restore()
})
it('creates REQUEST_CHART_DATA_SUCCESS when fetching has been done', () => {
fetchMock.getOnce('https://swapi.co/api/people', {
body: { todos: ['do something'] },
headers: { 'content-type': 'application/json' }
})
const expectedActions =
{ type: REQUEST_CHART_DATA_SUCCESS, body: { todos: ['do something'] } }
const store = mockStore({ todos: [] })
return store.dispatch(actions.requestChartData()).then(() => {
expect(store.getActions()).toEqual(expectedActions)
})
})
})
在您的 requestChartData 操作创建器中,return fetch 而不是仅仅调用它。 return fetch('/你的-api-路径')