2019-09-03 17:32:31 -04:00
|
|
|
const React = require('react');
|
|
|
|
const {shallowWithIntl} = require('../../helpers/intl-helpers.jsx');
|
|
|
|
const JoinFlowStep = require('../../../src/components/join-flow/join-flow-step.jsx');
|
2019-08-29 18:34:07 -04:00
|
|
|
const FormikInput = require('../../../src/components/formik-forms/formik-input.jsx');
|
|
|
|
const FormikCheckbox = require('../../../src/components/formik-forms/formik-checkbox.jsx');
|
|
|
|
|
2019-11-04 15:34:17 -05:00
|
|
|
const requestSuccessResponse = {
|
|
|
|
requestSucceeded: true,
|
|
|
|
valid: false,
|
|
|
|
errMsgId: 'registration.validationEmailInvalid'
|
|
|
|
};
|
|
|
|
const requestFailureResponse = {
|
|
|
|
requestSucceeded: false,
|
|
|
|
valid: false,
|
|
|
|
errMsgId: 'general.error'
|
|
|
|
};
|
|
|
|
// mockedValidateEmailRemotely will return a promise resolving with remoteRequestResponse.
|
|
|
|
// Using remoteRequestResponse, rather than using requestSuccessResponse directly,
|
|
|
|
// lets us change where remoteRequestResponse points later, without actually changing
|
|
|
|
// mockedValidateEmailRemotely.
|
|
|
|
let remoteRequestResponse = requestSuccessResponse;
|
|
|
|
let mockedValidateEmailRemotely = jest.fn(() => (
|
2019-09-30 14:07:31 -04:00
|
|
|
/* eslint-disable no-undef */
|
2019-11-04 15:34:17 -05:00
|
|
|
Promise.resolve(remoteRequestResponse)
|
2019-09-30 14:07:31 -04:00
|
|
|
/* eslint-enable no-undef */
|
|
|
|
));
|
|
|
|
|
|
|
|
jest.mock('../../../src/lib/validate.js', () => (
|
|
|
|
{
|
|
|
|
...(jest.requireActual('../../../src/lib/validate.js')),
|
|
|
|
validateEmailRemotely: mockedValidateEmailRemotely
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
// must come after validation mocks, so validate.js will be mocked before it is required
|
|
|
|
const EmailStep = require('../../../src/components/join-flow/email-step.jsx');
|
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
describe('EmailStep test', () => {
|
2019-09-30 14:07:31 -04:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('send correct props to formik', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(<EmailStep />);
|
|
|
|
|
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
|
|
|
expect(emailStepWrapper.props().initialValues.subscribe).toBe(false);
|
|
|
|
expect(emailStepWrapper.props().initialValues.email).toBe('');
|
|
|
|
expect(emailStepWrapper.props().validateOnBlur).toBe(false);
|
|
|
|
expect(emailStepWrapper.props().validateOnChange).toBe(false);
|
|
|
|
expect(emailStepWrapper.props().validate).toBe(emailStepWrapper.instance().validateForm);
|
|
|
|
expect(emailStepWrapper.props().onSubmit).toBe(emailStepWrapper.instance().handleValidSubmit);
|
2019-08-29 18:34:07 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('props sent to JoinFlowStep', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(<EmailStep />);
|
2019-08-29 18:34:07 -04:00
|
|
|
// Dive to get past the intl wrapper
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
2019-08-29 18:34:07 -04:00
|
|
|
// Dive to get past the anonymous component.
|
2019-10-23 15:33:31 -04:00
|
|
|
const joinFlowWrapper = emailStepWrapper.dive().find(JoinFlowStep);
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(joinFlowWrapper).toHaveLength(1);
|
|
|
|
expect(joinFlowWrapper.props().footerContent.props.id).toBe('registration.acceptTermsOfUse');
|
|
|
|
expect(joinFlowWrapper.props().headerImgSrc).toBe('/images/join-flow/email-header.png');
|
|
|
|
expect(joinFlowWrapper.props().innerClassName).toBe('join-flow-inner-email-step');
|
|
|
|
expect(joinFlowWrapper.props().nextButton).toBe('registration.createAccount');
|
|
|
|
expect(joinFlowWrapper.props().title).toBe('registration.emailStepTitle');
|
2019-09-16 20:12:42 -04:00
|
|
|
expect(joinFlowWrapper.props().titleClassName).toBe('join-flow-email-title');
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(joinFlowWrapper.props().waiting).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('props sent to FormikInput for email', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(<EmailStep />);
|
2019-08-29 18:34:07 -04:00
|
|
|
// Dive to get past the intl wrapper
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
2019-08-29 18:34:07 -04:00
|
|
|
// Dive to get past the anonymous component.
|
2019-10-23 15:33:31 -04:00
|
|
|
const joinFlowWrapper = emailStepWrapper.dive().find(JoinFlowStep);
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(joinFlowWrapper).toHaveLength(1);
|
|
|
|
const emailInputWrapper = joinFlowWrapper.find(FormikInput).first();
|
|
|
|
expect(emailInputWrapper.props().id).toEqual('email');
|
|
|
|
expect(emailInputWrapper.props().error).toBeUndefined();
|
|
|
|
expect(emailInputWrapper.props().name).toEqual('email');
|
|
|
|
expect(emailInputWrapper.props().placeholder).toEqual('general.emailAddress');
|
|
|
|
expect(emailInputWrapper.props().validationClassName).toEqual('validation-full-width-input');
|
2019-10-23 15:33:31 -04:00
|
|
|
expect(emailInputWrapper.props().onSetRef).toEqual(emailStepWrapper.instance().handleSetEmailRef);
|
|
|
|
expect(emailInputWrapper.props().validate).toEqual(emailStepWrapper.instance().validateEmail);
|
2019-08-29 18:34:07 -04:00
|
|
|
});
|
2019-09-18 10:26:37 -04:00
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('props sent to FormikCheckbox for subscribe', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(<EmailStep />);
|
2019-08-29 18:34:07 -04:00
|
|
|
// Dive to get past the intl wrapper
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
2019-08-29 18:34:07 -04:00
|
|
|
// Dive to get past the anonymous component.
|
2019-10-23 15:33:31 -04:00
|
|
|
const joinFlowWrapper = emailStepWrapper.dive().find(JoinFlowStep);
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(joinFlowWrapper).toHaveLength(1);
|
|
|
|
const checkboxWrapper = joinFlowWrapper.find(FormikCheckbox).first();
|
|
|
|
expect(checkboxWrapper).toHaveLength(1);
|
|
|
|
expect(checkboxWrapper.first().props().id).toEqual('subscribeCheckbox');
|
|
|
|
expect(checkboxWrapper.first().props().label).toEqual('registration.receiveEmails');
|
|
|
|
expect(checkboxWrapper.first().props().name).toEqual('subscribe');
|
|
|
|
});
|
2019-09-18 10:26:37 -04:00
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('handleValidSubmit passes formData to next step', () => {
|
|
|
|
const formikBag = {
|
|
|
|
setSubmitting: jest.fn()
|
|
|
|
};
|
2019-08-30 13:42:49 -04:00
|
|
|
global.grecaptcha = {
|
2019-08-29 18:34:07 -04:00
|
|
|
execute: jest.fn(),
|
|
|
|
render: jest.fn()
|
|
|
|
};
|
2019-09-03 17:32:31 -04:00
|
|
|
const formData = {item1: 'thing', item2: 'otherthing'};
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-08-29 18:34:07 -04:00
|
|
|
<EmailStep />);
|
|
|
|
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
|
|
|
emailStepWrapper.instance().onCaptchaLoad(); // to setup catpcha state
|
|
|
|
emailStepWrapper.instance().handleValidSubmit(formData, formikBag);
|
2019-08-29 18:34:07 -04:00
|
|
|
|
|
|
|
expect(formikBag.setSubmitting).toHaveBeenCalledWith(false);
|
2019-08-30 13:42:49 -04:00
|
|
|
expect(global.grecaptcha.execute).toHaveBeenCalled();
|
2019-08-29 18:34:07 -04:00
|
|
|
});
|
2019-09-18 10:26:37 -04:00
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('captchaSolved sets token and goes to next step', () => {
|
|
|
|
const props = {
|
|
|
|
onNextStep: jest.fn()
|
|
|
|
};
|
|
|
|
const formikBag = {
|
|
|
|
setSubmitting: jest.fn()
|
|
|
|
};
|
2019-08-30 13:42:49 -04:00
|
|
|
global.grecaptcha = {
|
2019-08-29 18:34:07 -04:00
|
|
|
execute: jest.fn(),
|
|
|
|
render: jest.fn()
|
|
|
|
};
|
2019-09-03 17:32:31 -04:00
|
|
|
const formData = {item1: 'thing', item2: 'otherthing'};
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-08-29 18:34:07 -04:00
|
|
|
<EmailStep
|
|
|
|
{...props}
|
|
|
|
/>);
|
|
|
|
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
2019-08-29 18:34:07 -04:00
|
|
|
// Call these to setup captcha.
|
2019-10-23 15:33:31 -04:00
|
|
|
emailStepWrapper.instance().onCaptchaLoad(); // to setup catpcha state
|
|
|
|
emailStepWrapper.instance().handleValidSubmit(formData, formikBag);
|
2019-08-29 18:34:07 -04:00
|
|
|
|
|
|
|
const captchaToken = 'abcd';
|
2019-10-23 15:33:31 -04:00
|
|
|
emailStepWrapper.instance().captchaSolved(captchaToken);
|
2019-09-03 17:32:31 -04:00
|
|
|
// Make sure captchaSolved calls onNextStep with formData that has
|
|
|
|
// a captcha token and left everything else in the object in place.
|
|
|
|
expect(props.onNextStep).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
'item1': formData.item1,
|
|
|
|
'item2': formData.item2,
|
|
|
|
'g-recaptcha-response': captchaToken
|
|
|
|
}));
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(formikBag.setSubmitting).toHaveBeenCalledWith(true);
|
|
|
|
});
|
2019-09-18 10:26:37 -04:00
|
|
|
|
|
|
|
test('Captcha load error calls error function', () => {
|
|
|
|
const props = {
|
2019-10-23 15:33:31 -04:00
|
|
|
onCaptchaError: jest.fn()
|
2019-09-18 10:26:37 -04:00
|
|
|
};
|
|
|
|
// Set this to null to force an error.
|
|
|
|
global.grecaptcha = null;
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-09-18 10:26:37 -04:00
|
|
|
<EmailStep
|
|
|
|
{...props}
|
2019-10-23 15:33:31 -04:00
|
|
|
/>
|
|
|
|
);
|
2019-09-18 10:26:37 -04:00
|
|
|
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
|
|
|
emailStepWrapper.instance().onCaptchaLoad();
|
|
|
|
expect(props.onCaptchaError).toHaveBeenCalled();
|
2019-09-18 10:26:37 -04:00
|
|
|
});
|
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('validateEmail test email empty', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-08-29 18:34:07 -04:00
|
|
|
<EmailStep />);
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
|
|
|
const val = emailStepWrapper.instance().validateEmail('');
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(val).toBe('general.required');
|
|
|
|
});
|
2019-09-18 10:26:37 -04:00
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('validateEmail test email null', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-08-29 18:34:07 -04:00
|
|
|
<EmailStep />);
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
|
|
|
const val = emailStepWrapper.instance().validateEmail(null);
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(val).toBe('general.required');
|
|
|
|
});
|
2019-09-18 10:26:37 -04:00
|
|
|
|
2019-08-29 18:34:07 -04:00
|
|
|
test('validateEmail test email undefined', () => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-08-29 18:34:07 -04:00
|
|
|
<EmailStep />);
|
2019-10-23 15:33:31 -04:00
|
|
|
const emailStepWrapper = intlWrapper.dive();
|
|
|
|
const val = emailStepWrapper.instance().validateEmail();
|
2019-08-29 18:34:07 -04:00
|
|
|
expect(val).toBe('general.required');
|
|
|
|
});
|
2019-11-04 15:34:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('validateEmailRemotelyWithCache test with successful requests', () => {
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
2019-09-30 14:07:31 -04:00
|
|
|
|
|
|
|
test('validateEmailRemotelyWithCache calls validate.validateEmailRemotely', done => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-09-30 14:07:31 -04:00
|
|
|
<EmailStep />);
|
2019-10-23 15:33:31 -04:00
|
|
|
const instance = intlWrapper.dive().instance();
|
2019-09-30 14:07:31 -04:00
|
|
|
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalled();
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('registration.validationEmailInvalid');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validateEmailRemotelyWithCache, called twice with different data, makes two remote requests', done => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-09-30 14:07:31 -04:00
|
|
|
<EmailStep />
|
|
|
|
);
|
2019-10-23 15:33:31 -04:00
|
|
|
const instance = intlWrapper.dive().instance();
|
2019-09-30 14:07:31 -04:00
|
|
|
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(1);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('registration.validationEmailInvalid');
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// make the same request a second time
|
|
|
|
instance.validateEmailRemotelyWithCache('different-email@some-domain.org')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(2);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('registration.validationEmailInvalid');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validateEmailRemotelyWithCache, called twice with same data, only makes one remote request', done => {
|
2019-10-23 15:33:31 -04:00
|
|
|
const intlWrapper = shallowWithIntl(
|
2019-09-30 14:07:31 -04:00
|
|
|
<EmailStep />
|
|
|
|
);
|
2019-10-23 15:33:31 -04:00
|
|
|
const instance = intlWrapper.dive().instance();
|
2019-09-30 14:07:31 -04:00
|
|
|
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(1);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('registration.validationEmailInvalid');
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// make the same request a second time
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(1);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('registration.validationEmailInvalid');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-08-29 18:34:07 -04:00
|
|
|
});
|
2019-11-04 15:34:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
describe('validateEmailRemotelyWithCache test with failing requests', () => {
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// needs to be wrapped inside beforeEach, because if not, it gets run as this
|
|
|
|
// test file is loaded, and goes into effect before any of the earlier tests run!
|
|
|
|
remoteRequestResponse = requestFailureResponse;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validateEmailRemotelyWithCache calls validate.validateEmailRemotely', done => {
|
|
|
|
const wrapper = shallowWithIntl(
|
|
|
|
<EmailStep />);
|
|
|
|
const instance = wrapper.dive().instance();
|
|
|
|
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalled();
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('general.error');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validateEmailRemotelyWithCache, called twice with different data, makes two remote requests', done => {
|
|
|
|
const wrapper = shallowWithIntl(
|
|
|
|
<EmailStep />
|
|
|
|
);
|
|
|
|
const instance = wrapper.dive().instance();
|
|
|
|
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(1);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('general.error');
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// make the same request a second time
|
|
|
|
instance.validateEmailRemotelyWithCache('different-email@some-domain.org')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(2);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('general.error');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validateEmailRemotelyWithCache, called 2x w/same data, makes 2 requests, since 1st not stored', done => {
|
|
|
|
const wrapper = shallowWithIntl(
|
|
|
|
<EmailStep />
|
|
|
|
);
|
|
|
|
const instance = wrapper.dive().instance();
|
|
|
|
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(1);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('general.error');
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// make the same request a second time
|
|
|
|
instance.validateEmailRemotelyWithCache('some-email@some-domain.com')
|
|
|
|
.then(response => {
|
|
|
|
expect(mockedValidateEmailRemotely).toHaveBeenCalledTimes(2);
|
|
|
|
expect(response.valid).toBe(false);
|
|
|
|
expect(response.errMsgId).toBe('general.error');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|