我正在尝试获取所有操作并匹配操作顺序, 这是代码片段。这是我的测试文件,我试图在该文件上模拟动作文件上的一些内容,并尝试查看是否已分派所有动作。
import {PLACEORDER_SERVICE_ACTIONS,internal} from './placeOrderServiceActions';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import PlaceOrderService from '../../../services/PlaceOrderService/PlaceOrderService';
import NuDataUtil from '../../../utilities/NuDataUtil';
import * as selector from '../../store-selectors/placeOrderSelectors';
import { Service } from './../../../services/ApiService/ApiService';
import Plugin from 'Plugin';
Plugin.exec=jest.fn(() => Promise.resolve(234));
describe('DeleteShippingAddressActions', () => {
let store;
let history;
beforeAll(async() => {
store = configureMockStore([thunk])({
common: {
isGuestUser: true
}
});
});
beforeEach(() => {
store.clearActions();
NuDataUtil.getWidgetData = jest.fn(() => 'widgetData');
selector.mapPlaceOrderRequest =jest.fn(() => 'test');
NuDataUtil.getNuData3DSStatus = jest.fn(() => { return {
enrollmentStatus: false,
bankInfoCollected: false,
transactionStatus: '',
threeDSError: null,
notificationUrl: ''
};
});
NuDataUtil.resetData=jest.fn();
history={
push:jest.fn()
};
});
it('Should fail', async () => {
NuDataUtil.checkAndStart3DS = jest.fn(() => Promise.resolve({}));
placeOrder =jest.fn(() => Promise.resolve({ order: 123,
nuDataDetails:
{
finalPlaceOrderRequest: true,
}}));
PlaceOrderService.invoke =jest.fn((data) => {
if (data.nuDataDetails && data.nuDataDetails.finalPlaceOrderRequest){
return Promise.resolve({payload:{ order:''}});
}
else {
return Promise.resolve({error:{errorCode:'ORD1111'}});
}
});
selector.mapPlaceOrderRequest =jest.fn(() => 123);
await store.dispatch(PLACEORDER_SERVICE_ACTIONS.placeGuestOrder({},history));
expect(store.getActions()).toEqual([]);
expect(history.push).toHaveBeenCalled();
});
});
动作文件
import { actionDefaultSettings } from '../actionDefaultSettings';
import { PLACEORDER_SERVICE_ACTION_TYPES } from '../../store-actiontypes/order/orderActionTypes';
import NuDataUtil from '../../../utilities/NuDataUtil';
import PlaceOrderService from '../../../services/PlaceOrderService/PlaceOrderService';
import {UISTATE_ACTIONS} from '../uiState/uiStateActions';
import {mapPlaceOrderRequest} from '../../store-selectors/placeOrderSelectors';
import {debounce} from '../../../utilities/Debounce';
import Plugin from 'Plugin';
import {generateRandomId} from '../../../utilities/RandomIdGenerator';
import {SIGNAL} from '../../../signal/index';
import constants from '../../../constants/constants';
import ConfigurationModule from 'Configuration';
import { NATIVE_HYBRID_COMM_PLUGIN } from '../../../constants/pluginConstants';
import genericUtils from '../../../utilities/genericUtils';
//import { getMobileOSConstant } from './../../../utilities/commonUtils';
import { CUSTOMER_ACTIONS } from '../customer/customerActions';
/**
* Actions Creators
*/
const placeOrderServiceStarted = (data = {}) => {
return {
...actionDefaultSettings,
type: PLACEORDER_SERVICE_ACTION_TYPES.PLACEORDER_SERVICE_STARTED,
payload: {data},
};
};
const placeOrderServiceCompleted = (data = {}) => {
return {
...actionDefaultSettings,
type: PLACEORDER_SERVICE_ACTION_TYPES.PLACEORDER_SERVICE_COMPLETED,
payload: {data},
};
};
const placeOrderServiceSuccess = (data = {}) => {
return {
...actionDefaultSettings,
type: PLACEORDER_SERVICE_ACTION_TYPES.PLACEORDER_SERVICE_SUCCESS,
payload: {data},
};
};
const placeOrderServiceFailure = (data = {}) => {
return {
...actionDefaultSettings,
type: PLACEORDER_SERVICE_ACTION_TYPES.PLACEORDER_SERVICE_FAILURE,
payload: {data},
};
};
export const PLACEORDER_SERVICE_ACTION_CREATOR = {
placeOrderServiceStarted,
placeOrderServiceCompleted,
placeOrderServiceSuccess,
placeOrderServiceFailure
};
const PlaceOrderRequestId = 'PlaceOrderRequestId';
const executePlaceOrderCall = (data) => {
return async (dispatch) => {
const results = await PlaceOrderService.invoke(data);
return results;
};
};
//TODO: Disable button
const placeGuestOrder = (data, history) => {
return async (dispatch, getState) => {
await placeOrderThunk(data,dispatch, getState, history);
};
};
const placeOrderThunk = async (data,dispatch, getState, history) => {
const kdata = ConfigurationModule.configuration;
try {
const widgetData = NuDataUtil.getWidgetData();
await dispatch(PLACEORDER_SERVICE_ACTION_CREATOR.placeOrderServiceStarted());
let requestData = {};
requestData ={
order: mapPlaceOrderRequest(getState()),
nuData: widgetData,
nuDataDetails : NuDataUtil.getNuData3DSStatus()
};
const results = await dispatch(executePlaceOrderCall(requestData));
console.log(results);
if (results && !results.error) {
console.log('test log');// able to print
//Not able to get in store.getAction()
await dispatch(UISTATE_ACTIONS.resetErrorBanner());
console.log('test log');// able to print
history.push('/order-confirmation');
return true;
} else {
//console.log(results);
if (results && results.error.errorCode === constants.NUDATA_CONSTANTS.errorCodes.FRAUD_ERROR && kdata.FeatureNuData3dsEnabled){
let firstResponseReceived=false;
NuDataUtil.checkAndStart3DS().then(async (nuDataObj) => {
if (!firstResponseReceived) {
firstResponseReceived = true;
nuDataObj.firstResponseReceived = firstResponseReceived;
let input = await placeOrder({ nuDataObj: nuDataObj },getState);
if (getState().common.isGuestUser) {
await dispatch(placeGuestOrder(input,history));
} else {
let token = await dispatch(CUSTOMER_ACTIONS.executeAccessTokenFlow());
if (token) {
input.accessToken = token;
dispatch(placeGuestOrder(input,history));
} else {
genericUtils.displayNotification('Access Token not received.');
}
}
}
});
} else {
if (results)
await dispatch(UISTATE_ACTIONS.showErrorBanner(results.error.message));
}
}
} catch (e) {
if (e && e.error && e.error.message)
dispatch(UISTATE_ACTIONS.showErrorBanner(e.error.message));
//TODO: check for generic errors
if (e && e.error && e.error.code == 'ORDER1004') {
//Set error and redirect to order confirmation page
await dispatch(PLACEORDER_SERVICE_ACTION_CREATOR.placeOrderServiceFailure(e.error));
history.push('/order-confirmation');
} else {
if (e && e.error && e.error.message)
dispatch(UISTATE_ACTIONS.showErrorBanner(e.error.message));
}
}
};
async function placeOrder(data,getState){
const kdata = ConfigurationModule.configuration;
var input = { order: mapPlaceOrderRequest(getState())};
if (data.FeatureRemedyPlaceOrderEnabled || kdata.FeatureNuData3dsEnabled || kdata.FeatureFraudOtpPlaceOrderEnabled) {
return await populateNuDataParams(input,data);
}
else {
return input;
}
}
async function populateNuDataParams(input, data) {
const kdata = ConfigurationModule.configuration;
var nuDataObj = NuDataUtil.getNuData3DSStatus();
input.nuDataDetails = {};
if (kdata.FeatureNuData3dsEnabled) {
input.nuDataDetails = {
enrollmentStatus: nuDataObj.enrollmentStatus,
bankInfoCollected: nuDataObj.bankInfoCollected,
transactionStatus: nuDataObj.transactionStatus,
threeDSError: nuDataObj.threeDSError,
notificationUrl: nuDataObj.notificationUrl
};
if (data && data.nuDataObj && data.nuDataObj.firstResponseReceived) {
input.nuDataDetails.finalPlaceOrderRequest = true;
}
}
return await invokeNuDataPlugin(input);
}
async function invokeNuDataPlugin(input){
let response = await Plugin.exec(NATIVE_HYBRID_COMM_PLUGIN.name,
NATIVE_HYBRID_COMM_PLUGIN.actions.GET_NUDATA_SESSION_ID);
if (response){
var nds = window.ndsapi;
if (nds && nds.setSessionId){
nds.setSessionId(response);
}
//set nuData parameters in the input parameter of the place order API
input.nuDataDetails.sessionID = response;
input.nuDataDetails.channel = 'mobile'; //TODO: Once TCOM is implemented add logic to switch between TCOM or MCOM
input.nuDataDetails.ndpdData = NuDataUtil.getWidgetData();
return input;
} else {
return input;
}
}
export const internal = {
placeOrderThunk,
executePlaceOrderCall,
placeOrder
};
export const PLACEORDER_SERVICE_ACTIONS = {
executePlaceOrderCall,
placeGuestOrder
};
export default {
PLACEORDER_SERVICE_ACTION_CREATOR,
PLACEORDER_SERVICE_ACTIONS
};
我想调用placeGuestOrder()并测试所有动作是否都以正确的顺序执行。我可以在store.getActions()中获得placeOrderServiceStarted()动作,但是当我没有从动作文件中的以下块中分派任何动作时,
if (results && !results.error)
store.getActions()不返回它。只是想知道我在这里干什么。