scratch-www/test/unit/components/join-flow.test.jsx

403 lines
17 KiB
React
Raw Normal View History

2019-09-04 23:32:39 -04:00
import React from 'react';
const {shallowWithIntl} = require('../../helpers/intl-helpers.jsx');
2019-10-17 23:32:26 -04:00
const defaults = require('lodash.defaultsdeep');
2019-09-04 23:32:39 -04:00
import configureStore from 'redux-mock-store';
import JoinFlow from '../../../src/components/join-flow/join-flow';
import Progression from '../../../src/components/progression/progression.jsx';
import RegistrationErrorStep from '../../../src/components/join-flow/registration-error-step';
2019-09-04 23:32:39 -04:00
describe('JoinFlow', () => {
const mockStore = configureStore();
let store;
2019-10-23 05:20:02 -04:00
const responseBodyMultipleErrs = [
{
msg: 'This field is required.',
errors: {
username: ['This field is required.'],
recaptcha: ['Incorrect, please try again.']
},
success: false
}
];
const responseBodySingleErr = [
{
msg: 'This field is required.',
errors: {
recaptcha: ['Incorrect, please try again.']
},
success: false
}
];
const responseBodySuccess = [
{
msg: 'This field is required.',
errors: {
recaptcha: ['Incorrect, please try again.']
},
success: true
}
];
2019-09-04 23:32:39 -04:00
beforeEach(() => {
store = mockStore({sessionActions: {
refreshSessionWithRetry: jest.fn()
2019-09-04 23:32:39 -04:00
}});
});
const getJoinFlowWrapper = props => {
2019-09-04 23:32:39 -04:00
const wrapper = shallowWithIntl(
<JoinFlow
{...props}
/>
, {context: {store}}
);
2019-09-04 23:32:39 -04:00
return wrapper
.dive() // unwrap redux connect(injectIntl(JoinFlow))
.dive(); // unwrap injectIntl(JoinFlow)
};
2019-09-04 23:32:39 -04:00
2019-10-23 05:20:02 -04:00
test('handleCaptchaError gives state with captcha message', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
joinFlowInstance.setState({});
2019-10-23 05:20:02 -04:00
joinFlowInstance.handleCaptchaError();
expect(joinFlowInstance.state.registrationError).toEqual({
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: false,
2019-10-23 05:20:02 -04:00
errorMsg: 'registration.errorCaptcha'
});
});
2019-09-04 23:32:39 -04:00
test('sendAnalytics calls GTM with correct params', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
global.window.dataLayer = {push: jest.fn()};
global.window.GA_ID = '1234';
joinFlowInstance.sendAnalytics('page-path');
expect(global.window.dataLayer.push).toHaveBeenCalledWith({
event: 'join_flow',
joinFlowStep: 'page-path'
});
});
2019-09-04 23:32:39 -04:00
test('handleAdvanceStep', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-09-04 23:32:39 -04:00
joinFlowInstance.setState({formData: {username: 'ScratchCat123'}, step: 2});
joinFlowInstance.handleAdvanceStep({email: 'scratchcat123@scratch.mit.edu'});
expect(joinFlowInstance.state.formData.username).toBe('ScratchCat123');
expect(joinFlowInstance.state.formData.email).toBe('scratchcat123@scratch.mit.edu');
expect(joinFlowInstance.state.step).toBe(3);
});
test('when state.registrationError has error message, we show RegistrationErrorStep', () => {
const joinFlowWrapper = getJoinFlowWrapper();
joinFlowWrapper.instance().setState({registrationError: 'halp there is a errors!!'});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
const progressionWrapper = joinFlowWrapper.find(Progression);
expect(registrationErrorWrapper).toHaveLength(1);
expect(progressionWrapper).toHaveLength(0);
});
test('when state.registrationError has null error message, we show Progression', () => {
const joinFlowWrapper = getJoinFlowWrapper();
joinFlowWrapper.instance().setState({registrationError: null});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
const progressionWrapper = joinFlowWrapper.find(Progression);
expect(registrationErrorWrapper).toHaveLength(0);
expect(progressionWrapper).toHaveLength(1);
});
test('when state.registrationError has empty error message, we show Progression', () => {
const joinFlowWrapper = getJoinFlowWrapper();
joinFlowWrapper.instance().setState({registrationError: ''});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
const progressionWrapper = joinFlowWrapper.find(Progression);
expect(registrationErrorWrapper).toHaveLength(0);
expect(progressionWrapper).toHaveLength(1);
});
2019-10-17 23:32:26 -04:00
2019-11-01 15:56:54 -04:00
test('when numAttempts is 0 and registrationError errorAllowsTryAgain is true, ' +
'RegistrationErrorStep receives errorAllowsTryAgain prop with value true', () => {
2019-10-17 23:32:26 -04:00
const joinFlowWrapper = getJoinFlowWrapper();
joinFlowWrapper.instance().setState({
numAttempts: 0,
2019-10-23 05:20:02 -04:00
registrationError: {
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: true,
2019-10-23 05:20:02 -04:00
errorMsg: 'halp there is a errors!!'
}
2019-10-17 23:32:26 -04:00
});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
expect(registrationErrorWrapper.first().props().canTryAgain).toEqual(true);
});
2019-11-01 15:56:54 -04:00
test('when numAttempts is 1 and registrationError errorAllowsTryAgain is true, ' +
'RegistrationErrorStep receives errorAllowsTryAgain prop with value true', () => {
2019-10-17 23:32:26 -04:00
const joinFlowWrapper = getJoinFlowWrapper();
joinFlowWrapper.instance().setState({
numAttempts: 1,
2019-10-23 05:20:02 -04:00
registrationError: {
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: true,
2019-10-23 05:20:02 -04:00
errorMsg: 'halp there is a errors!!'
}
2019-10-17 23:32:26 -04:00
});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
expect(registrationErrorWrapper.first().props().canTryAgain).toEqual(true);
});
2019-11-01 15:56:54 -04:00
test('when numAttempts is 2 and registrationError errorAllowsTryAgain is true, ' +
'RegistrationErrorStep receives errorAllowsTryAgain prop with value false', () => {
2019-10-17 23:32:26 -04:00
const joinFlowWrapper = getJoinFlowWrapper();
joinFlowWrapper.instance().setState({
numAttempts: 2,
2019-10-23 05:20:02 -04:00
registrationError: {
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: true,
2019-10-23 05:20:02 -04:00
errorMsg: 'halp there is a errors!!'
}
2019-10-17 23:32:26 -04:00
});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
expect(registrationErrorWrapper.first().props().canTryAgain).toEqual(false);
});
2019-11-01 15:56:54 -04:00
test('when numAttempts is 0 and registrationError errorAllowsTryAgain is false, ' +
'RegistrationErrorStep receives errorAllowsTryAgain prop with value false', () => {
2019-10-17 23:32:26 -04:00
const joinFlowWrapper = getJoinFlowWrapper();
2019-10-23 05:20:02 -04:00
joinFlowWrapper.instance().setState({
numAttempts: 0,
registrationError: {
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: false,
2019-10-23 05:20:02 -04:00
errorMsg: 'halp there is a errors!!'
}
});
const registrationErrorWrapper = joinFlowWrapper.find(RegistrationErrorStep);
expect(registrationErrorWrapper.first().props().canTryAgain).toEqual(false);
});
test('resetState resets entire state, does not leave any state keys out', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-10-17 23:32:26 -04:00
Object.keys(joinFlowInstance.state).forEach(key => {
joinFlowInstance.setState({[key]: 'Different than the initial value'});
});
joinFlowInstance.resetState();
Object.keys(joinFlowInstance.state).forEach(key => {
expect(joinFlowInstance.state[key]).not.toEqual('Different than the initial value');
});
});
test('resetState makes each state field match initial state', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-10-17 23:32:26 -04:00
const stateSnapshot = {};
Object.keys(joinFlowInstance.state).forEach(key => {
stateSnapshot[key] = joinFlowInstance.state[key];
});
joinFlowInstance.resetState();
Object.keys(joinFlowInstance.state).forEach(key => {
expect(stateSnapshot[key]).toEqual(joinFlowInstance.state[key]);
});
});
test('calling resetState results in state.formData which is not same reference as before', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-10-17 23:32:26 -04:00
joinFlowInstance.setState({
formData: defaults({}, {username: 'abcdef'})
});
const formDataReference = joinFlowInstance.state.formData;
joinFlowInstance.resetState();
expect(formDataReference).not.toBe(joinFlowInstance.state.formData);
expect(formDataReference).not.toEqual(joinFlowInstance.state.formData);
});
2019-10-23 05:20:02 -04:00
2019-11-01 15:56:54 -04:00
test('getErrorsFromResponse returns object of errors', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const errorsFromResponse =
joinFlowInstance.getErrorsFromResponse(null, responseBodyMultipleErrs, {statusCode: 200});
expect(errorsFromResponse).toEqual([
{
fieldName: 'username',
errorStr: 'This field is required.'
}, {
fieldName: 'recaptcha',
errorStr: 'Incorrect, please try again.'
}
]);
2019-10-23 05:20:02 -04:00
});
2019-11-01 15:56:54 -04:00
test('getErrorsFromResponse called with non-null err returns empty array', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const errorsFromResponse =
joinFlowInstance.getErrorsFromResponse({}, responseBodyMultipleErrs, {statusCode: 200});
expect(errorsFromResponse).toEqual([]);
2019-10-23 05:20:02 -04:00
});
2019-11-01 15:56:54 -04:00
test('getErrorsFromResponse called with non-200 status code returns empty array', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const errorsFromResponse =
joinFlowInstance.getErrorsFromResponse({}, responseBodyMultipleErrs, {statusCode: 400});
expect(errorsFromResponse).toEqual([]);
2019-10-23 05:20:02 -04:00
});
2019-11-01 15:56:54 -04:00
test('getErrorsFromResponse gets single error, when given response body with only one error', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const errorsFromResponse =
joinFlowInstance.getErrorsFromResponse(null, responseBodySingleErr, {statusCode: 200});
expect(errorsFromResponse.length).toEqual(1);
2019-10-23 05:20:02 -04:00
});
test('getCustomErrMsg string when given response body with multiple errors', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const errorsFromResponse =
joinFlowInstance.getErrorsFromResponse(null, responseBodyMultipleErrs, {statusCode: 200});
const customErrMsg = joinFlowInstance.getCustomErrMsg(errorsFromResponse);
expect(customErrMsg).toEqual('registration.problemsAre: "username: This field is required.; ' +
2019-10-23 05:20:02 -04:00
'recaptcha: Incorrect, please try again."');
});
test('getCustomErrMsg string when given response body with single error', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const errorsFromResponse =
joinFlowInstance.getErrorsFromResponse(null, responseBodySingleErr, {statusCode: 200});
const customErrMsg = joinFlowInstance.getCustomErrMsg(errorsFromResponse);
expect(customErrMsg).toEqual('registration.problemsAre: "recaptcha: Incorrect, please try again."');
2019-10-23 05:20:02 -04:00
});
2019-11-01 15:56:54 -04:00
test('registrationIsSuccessful returns true when given response body with single error', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const success = joinFlowInstance.registrationIsSuccessful(null, responseBodySuccess, {statusCode: 200});
2019-10-23 05:20:02 -04:00
expect(success).toEqual(true);
});
2019-11-01 15:56:54 -04:00
test('registrationIsSuccessful returns false when given status code not 200', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const success = joinFlowInstance.registrationIsSuccessful(null, responseBodySuccess, {statusCode: 500});
2019-10-23 05:20:02 -04:00
expect(success).toEqual(false);
});
2019-11-01 15:56:54 -04:00
test('registrationIsSuccessful returns false when given body with success field false', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const success = joinFlowInstance.registrationIsSuccessful(null, responseBodySingleErr, {statusCode: 200});
2019-10-23 05:20:02 -04:00
expect(success).toEqual(false);
});
2019-11-01 15:56:54 -04:00
test('registrationIsSuccessful returns false when given non null err', () => {
2019-10-23 05:20:02 -04:00
const joinFlowInstance = getJoinFlowWrapper().instance();
2019-11-01 15:56:54 -04:00
const success = joinFlowInstance.registrationIsSuccessful({}, responseBodySuccess, {statusCode: 200});
2019-10-23 05:20:02 -04:00
expect(success).toEqual(false);
});
test('handleRegistrationResponse calls refreshSessionWithRetry() when passed body with success', done => {
2019-10-23 05:20:02 -04:00
const props = {
refreshSessionWithRetry: () => (new Promise(() => { // eslint-disable-line no-undef
done(); // ensures that joinFlowInstance.props.refreshSessionWithRetry() was called
}))
2019-10-23 05:20:02 -04:00
};
const joinFlowInstance = getJoinFlowWrapper(props).instance();
joinFlowInstance.handleRegistrationResponse(null, responseBodySuccess, {statusCode: 200});
});
test('handleRegistrationResponse advances to next step when passed body with success', () => {
const props = {
refreshSessionWithRetry: () => (new Promise(resolve => { // eslint-disable-line no-undef
resolve();
}))
};
const joinFlowInstance = getJoinFlowWrapper(props).instance();
joinFlowInstance.handleRegistrationResponse(null, responseBodySuccess, {statusCode: 200});
process.nextTick(
() => {
expect(joinFlowInstance.state.registrationError).toEqual(null);
expect(joinFlowInstance.state.step).toEqual(1);
expect(joinFlowInstance.state.waiting).toBeFalsy();
}
);
2019-10-23 05:20:02 -04:00
});
test('handleRegistrationResponse when passed body with preset server error', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
joinFlowInstance.handleRegistrationResponse(null, responseBodySingleErr, {statusCode: 200});
expect(joinFlowInstance.state.registrationError).toEqual({
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: false,
2019-10-23 05:20:02 -04:00
errorMsg: 'registration.errorCaptcha'
});
});
test('handleRegistrationResponse with failure response, with error fields missing', () => {
const props = {
refreshSessionWithRetry: jest.fn()
};
const joinFlowInstance = getJoinFlowWrapper(props).instance();
const responseErr = null;
const responseBody = [
{
msg: 'This field is required.',
success: false
}
];
const responseObj = {
statusCode: 200
};
joinFlowInstance.handleRegistrationResponse(responseErr, responseBody, responseObj);
expect(joinFlowInstance.props.refreshSessionWithRetry).not.toHaveBeenCalled();
expect(joinFlowInstance.state.registrationError).toEqual({
errorAllowsTryAgain: false,
errorMsg: null
});
});
2019-10-23 05:20:02 -04:00
test('handleRegistrationResponse when passed body with unfamiliar server error', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
joinFlowInstance.handleRegistrationResponse(null, responseBodyMultipleErrs, {statusCode: 200});
expect(joinFlowInstance.state.registrationError).toEqual({
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: false,
errorMsg: 'registration.problemsAre: "username: This field is required.; ' +
2019-10-23 05:20:02 -04:00
'recaptcha: Incorrect, please try again."'
});
});
test('handleRegistrationResponse with failure response, with no text explanation', () => {
const props = {
refreshSessionWithRetry: jest.fn()
};
const joinFlowInstance = getJoinFlowWrapper(props).instance();
const responseErr = null;
const responseBody = [
{
success: false
}
];
const responseObj = {
statusCode: 200
};
joinFlowInstance.handleRegistrationResponse(responseErr, responseBody, responseObj);
expect(joinFlowInstance.props.refreshSessionWithRetry).not.toHaveBeenCalled();
expect(joinFlowInstance.state.registrationError).toEqual({
errorAllowsTryAgain: false,
errorMsg: null
});
});
2019-10-23 05:20:02 -04:00
test('handleRegistrationResponse when passed non null outgoing request error', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
joinFlowInstance.handleRegistrationResponse({}, responseBodyMultipleErrs, {statusCode: 200});
expect(joinFlowInstance.state.registrationError).toEqual({
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: true
2019-10-23 05:20:02 -04:00
});
});
test('handleRegistrationResponse when passed status 400', () => {
const props = {
refreshSessionWithRetry: jest.fn()
};
const joinFlowInstance = getJoinFlowWrapper(props).instance();
2019-10-23 05:20:02 -04:00
joinFlowInstance.handleRegistrationResponse({}, responseBodyMultipleErrs, {statusCode: 400});
expect(joinFlowInstance.props.refreshSessionWithRetry).not.toHaveBeenCalled();
2019-10-23 05:20:02 -04:00
expect(joinFlowInstance.state.registrationError).toEqual({
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: true
2019-10-23 05:20:02 -04:00
});
});
test('handleRegistrationResponse when passed status 500', () => {
const joinFlowInstance = getJoinFlowWrapper().instance();
joinFlowInstance.handleRegistrationResponse(null, responseBodyMultipleErrs, {statusCode: 500});
expect(joinFlowInstance.state.registrationError).toEqual({
2019-11-01 15:56:54 -04:00
errorAllowsTryAgain: true
2019-10-23 05:20:02 -04:00
});
});
2019-09-04 23:32:39 -04:00
});