import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import moxios from 'moxios';
import {
submitRegistrationStepOneForm, // <-- this is an action that returns returns a promise
} from '../RegistrationActions';
const USER_ID = 999;
const USER_EMAIL = 'lucky_star@milkyway.test';
const userDataForStepOne = { email: USER_EMAIL };
const responseForUserRequest = { content: { id: USER_ID } };
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
describe('Registration action tests for form', () => {
beforeEach(() => moxios.install());
afterEach(() => moxios.uninstall());
it('submitRegistrationStepOneForm success should trigger correct dispatches', () => {
const initialState = {};
const store = mockStore(initialState);
// simplest way is to use a regex to match url
moxios.stubRequest(/users.*/, {
status: 201, // <-- you can replace this with a 5xx to test failure cases
response: responseForUserRequest,
});
return store.dispatch(submitRegistrationStepOneForm({
userData: userDataForStepOne,
}))
.then(() => {
// in my case I used to run 2 dispatches on success call:
const actions = store.getActions();
expect(actions.length).toEqual(2);
expect(actions[0]).toEqual({ type: 'SUBMIT_REGISTRATION_FORM_DATA' });
expect(actions[1]).toEqual({
type: 'SUBMIT_REGISTRATION_STEP_ONE_SUCCESS',
payload: { userId: USER_ID },
});
// ... more tests here if needed
});
});
// ....
submitRegistrationStepOneForm 动作:
export const submitRegistrationStepOneForm = ({ userData, sourceArticle }) => {
return (dispatch) => {
dispatch(submitRegistrationDataAction());
// signUpStepOne can be replaced directly with the axios promise
return signUpStepOne({ userData })
.then((body) => {
const { content: { id } } = body;
// ... some function calls and 2 dispatches here like:
// dispatch(registrationSuccess({ userId: id }));
})
.catch((error) => {
console.log('step one error', error); // eslint-disable-line
dispatch(registrationFailureAction());
});
};
};