scratch-www/test/unit/components/username-step.test.jsx

128 lines
5.3 KiB
React
Raw Normal View History

const React = require('react');
const {shallowWithIntl} = require('../../helpers/intl-helpers.jsx');
2019-10-30 15:34:52 -04:00
let mockedValidateUsernameRemotely = jest.fn(() => (
/* eslint-disable no-undef */
2019-10-30 15:34:52 -04:00
Promise.resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationUsernameNotAllowed'})
/* eslint-enable no-undef */
));
jest.mock('../../../src/lib/validate.js', () => (
{
...(jest.requireActual('../../../src/lib/validate.js')),
validateUsernameRemotely: mockedValidateUsernameRemotely
}
));
// must come after validation mocks, so validate.js will be mocked before it is required
const UsernameStep = require('../../../src/components/join-flow/username-step.jsx');
2019-10-30 15:34:52 -04:00
describe('UsernameStep tests', () => {
afterEach(() => {
jest.clearAllMocks();
});
test('send correct props to formik', () => {
const wrapper = shallowWithIntl(<UsernameStep />);
const formikWrapper = wrapper.dive();
expect(formikWrapper.props().initialValues.username).toBe('');
expect(formikWrapper.props().initialValues.password).toBe('');
expect(formikWrapper.props().initialValues.passwordConfirm).toBe('');
expect(formikWrapper.props().initialValues.showPassword).toBe(true);
expect(formikWrapper.props().validateOnBlur).toBe(false);
expect(formikWrapper.props().validateOnChange).toBe(false);
expect(formikWrapper.props().validate).toBe(formikWrapper.instance().validateForm);
expect(formikWrapper.props().onSubmit).toBe(formikWrapper.instance().handleValidSubmit);
});
test('handleValidSubmit passes formData to next step', () => {
const formikBag = {
setSubmitting: jest.fn()
};
const formData = {item1: 'thing', item2: 'otherthing'};
const mockedOnNextStep = jest.fn();
const wrapper = shallowWithIntl(
<UsernameStep
onNextStep={mockedOnNextStep}
/>
);
const instance = wrapper.dive().instance();
instance.handleValidSubmit(formData, formikBag);
expect(formikBag.setSubmitting).toHaveBeenCalledWith(false);
expect(mockedOnNextStep).toHaveBeenCalledWith(formData);
});
2019-10-30 15:34:52 -04:00
});
describe('validateUsernameRemotelyWithCache test', () => {
// mockedValidateUsernameRemotely = jest.fn(() => (
// /* eslint-disable no-undef */
// Promise.resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationUsernameNotAllowed'})
// /* eslint-enable no-undef */
// ));
test('validateUsernameRemotelyWithCache calls validate.validateUsernameRemotely', done => {
2019-10-30 15:34:52 -04:00
const wrapper = shallowWithIntl(<UsernameStep />);
const instance = wrapper.dive().instance();
instance.validateUsernameRemotelyWithCache('newUniqueUsername55')
.then(response => {
expect(mockedValidateUsernameRemotely).toHaveBeenCalled();
expect(response.valid).toBe(false);
expect(response.errMsgId).toBe('registration.validationUsernameNotAllowed');
done();
});
});
test('validateUsernameRemotelyWithCache, called twice with different data, makes two remote requests', done => {
const wrapper = shallowWithIntl(
<UsernameStep />
);
const instance = wrapper.dive().instance();
instance.validateUsernameRemotelyWithCache('newUniqueUsername55')
.then(response => {
expect(mockedValidateUsernameRemotely).toHaveBeenCalledTimes(1);
expect(response.valid).toBe(false);
expect(response.errMsgId).toBe('registration.validationUsernameNotAllowed');
})
.then(() => {
// make the same request a second time
instance.validateUsernameRemotelyWithCache('secondDifferent66')
.then(response => {
expect(mockedValidateUsernameRemotely).toHaveBeenCalledTimes(2);
expect(response.valid).toBe(false);
expect(response.errMsgId).toBe('registration.validationUsernameNotAllowed');
done();
});
});
});
test('validateUsernameRemotelyWithCache, called twice with same data, only makes one remote request', done => {
const wrapper = shallowWithIntl(
<UsernameStep />
);
const instance = wrapper.dive().instance();
instance.validateUsernameRemotelyWithCache('newUniqueUsername55')
.then(response => {
expect(mockedValidateUsernameRemotely).toHaveBeenCalledTimes(1);
expect(response.valid).toBe(false);
expect(response.errMsgId).toBe('registration.validationUsernameNotAllowed');
})
.then(() => {
// make the same request a second time
instance.validateUsernameRemotelyWithCache('newUniqueUsername55')
.then(response => {
expect(mockedValidateUsernameRemotely).toHaveBeenCalledTimes(1);
expect(response.valid).toBe(false);
expect(response.errMsgId).toBe('registration.validationUsernameNotAllowed');
done();
});
});
});
});