Reactjs 使用多个调度测试操作(加载、错误和成功)
如何为具有多个调度(加载、错误和成功)的操作创建测试,请参见我的操作之一:Reactjs 使用多个调度测试操作(加载、错误和成功),reactjs,jestjs,redux-thunk,Reactjs,Jestjs,Redux Thunk,如何为具有多个调度(加载、错误和成功)的操作创建测试,请参见我的操作之一: import axios from 'axios'; import { CompaniesActionTypes } from './types'; export const getCompanies = () => async (dispatch: any) => { dispatch({ type: CompaniesActionTypes.LOADING })
import axios from 'axios';
import { CompaniesActionTypes } from './types';
export const getCompanies = () => async (dispatch: any) => {
dispatch({
type: CompaniesActionTypes.LOADING
})
try {
const response = await axios.get('app/admin/companies');
dispatch({
type: CompaniesActionTypes.GET_COMPANIES,
payload: response.data
})
} catch (error) {
console.log(error.message);
dispatch({
type: CompaniesActionTypes.ERROR,
payload: 'There was an error while requesting list of companies, please try again later.'
})
}
}
要了解更多信息,以下是此场景的我的减速机:
import { CompaniesActionTypes, CompaniesState } from './types';
import { Reducer } from 'redux';
const INITIAL_STATE: CompaniesState = {
data: [],
loading: false,
error: ''
}
export const reducer: Reducer<CompaniesState> = (state = INITIAL_STATE, action) => {
switch (action.type) {
case CompaniesActionTypes.GET_COMPANIES:
return {...state, data: action.payload, loading: false, error: ''}
case CompaniesActionTypes.LOADING:
return {...state, loading: true};
case CompaniesActionTypes.ERROR:
return {...state, error: action.payload, loading: false};
default:
return state
}
}
对于第一个动作测试,我收到以下错误:
应为:{“payload”:{},“type”:“@@companys/GET_companys”}
已收到:[匿名函数]
在本例中,我对调度有问题:
- Expected
+ Received
Array [
Object {
- "payload": Array [
- Object {
- "id": 1,
- "name": "Company Test 1",
+ "type": "@@companies/LOADING_COMPANIES",
},
Object {
- "id": 2,
- "name": "Company Test 2",
- },
- Object {
- "id": 3,
- "name": "Company Test 3",
- },
- ],
- "type": "@@companies/GET_COMPANIES",
+ "payload": "There was an error while requesting list of companies, please try again later.",
+ "type": "@@companies/ERROR_COMPANIES",
},
]
这是怎么回事
- “类型”:“@@companys/LOADING_companys”
- “类型”:“@@companys/GET_companys”
- “类型”:“@@companys/ERROR\u companys”
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
describe('actions', () => {
afterEach(() => {
fetchMock.restore()
})
it('creates GET_COMPANIES when fetching companies', () => {
fetchMock.getOnce('app/admin/client/companies', {
body: mock,
headers: { 'content-type': 'application/json' }
})
const expectedActions = [{ type: CompaniesActionTypes.GET_COMPANIES, payload: mock }]
const store = mockStore({})
return store.dispatch(actions.getCompanies()).then(() => {
expect(store.getActions()).toEqual(expectedActions)
})
})
})
- Expected
+ Received
Array [
Object {
- "payload": Array [
- Object {
- "id": 1,
- "name": "Company Test 1",
+ "type": "@@companies/LOADING_COMPANIES",
},
Object {
- "id": 2,
- "name": "Company Test 2",
- },
- Object {
- "id": 3,
- "name": "Company Test 3",
- },
- ],
- "type": "@@companies/GET_COMPANIES",
+ "payload": "There was an error while requesting list of companies, please try again later.",
+ "type": "@@companies/ERROR_COMPANIES",
},
]