我有根据API状态进行多次分派的操作。
开始>成功|失败
该测试用例一次分发(SUCCESS)正常工作,但是当我对其进行修改并向其添加更多分发时。它崩溃了。
Action.js
export const getOrder = (orderNumber, requestMethod = request)=> {
return function (dispatch) {
dispatch({
type:ORDER.GET_ORDER_DETAIL_START
});
return requestMethod(apiServices.getOrderParams(orderNumber)).then(order=>{
const {success, message, data} = order.data;
const result = orderApiSignatureTransformer(data);
const payload = GlobalConfig.resultDto(success,message,result);
dispatch({
type:ORDER.GET_ORDER_DETAIL_SUCCESS,
payload
});
return payload
}).catch(()=>{
dispatch({
type:ORDER.GET_ORDER_DETAIL_FAILURE
});
})
}
}
Action.test.js
describe('ordersActions', ()=> {
it('should get an order for a given order number', () => {
const order = {
orderNumber: 111,
status: "OPEN",
dateUpdated: "01/01/2019"
}
const expectedActions = [
{ type: ORDER.GET_ORDER_DETAIL_START },
{ type: ORDER.GET_ORDER_DETAIL_FAILURE },
{ type: ORDER.GET_ORDER_DETAIL_SUCCESS, payload:order },
]
const store = mockStore({ orderDetail: [], orderDetailStatus: null });
const mockRequest = jest.fn(() => Promise.resolve(order));
return store.dispatch(getOrder(111, mockRequest)).then(() => {
// return of async actions
expect(store.getActions()).toEqual(expectedActions)
}).catch((e)=>{
expect(store.getActions()).toEqual(expectedActions)
})
})
})
错误
期望值等于:
[{"type": "GET_ORDER_DETAIL_START"}, {"type": "GET_ORDER_DETAIL_FAILURE"}, {"payload": {"dateUpdated": "01/01/2019", "orderNumber": 111, "status": "OPEN"}, "type": "GET_ORDER_DETAIL_SUCCESS"}]
已收到:
[{"type": "GET_ORDER_DETAIL_START"}, {"type": "GET_ORDER_DETAIL_FAILURE"}]
差异:
- Expected
+ Received
@@ -3,14 +3,6 @@
"type": "GET_ORDER_DETAIL_START",
},
Object {
"type": "GET_ORDER_DETAIL_FAILURE",
},
- Object {
- "payload": Object {
- "dateUpdated": "01/01/2019",
- "orderNumber": 111,
- "status": "OPEN",
- },
- "type": "GET_ORDER_DETAIL_SUCCESS",
- },
]
expect(store.getActions()).toEqual(expectedActions)
}).catch((e)=>{
expect(store.getActions()).toEqual(expectedActions)
})
})
})