Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
feat(#105): added tests for auth sub path
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Gasser committed Apr 24, 2019
1 parent e2433aa commit 81c883d
Show file tree
Hide file tree
Showing 11 changed files with 870 additions and 57 deletions.
11 changes: 9 additions & 2 deletions src/auth/login/__tests__/Container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { logInUser } from '../../../redux/auth';

const { select, mapDispatchToProps } = __testables__;

describe('auth container test suite', () => {
describe('auth login container test suite', () => {
it('should return container state', () => {
expect(select({})).toEqual({
const state = select({});

expect(state).toEqual({
isAuthenticated: false,
loginRequest: undefined,
});

expect(Object.keys(state)).toEqual([
'isAuthenticated',
'loginRequest',
]);
});

it('should return redux actions', () => {
Expand Down
6 changes: 2 additions & 4 deletions src/auth/login/__tests__/LoginForm.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/* global testUtils */
import React from 'react';

import * as Yup from 'yup';
import { mount } from 'enzyme';

import { TextInput, CheckBox, Field } from '../../../ui/form/Form';
import Message from '../../../ui/form/Message';
import Button from '../../../ui/form/Button';

import EnhancedLoginForm, { __testables__ } from '../LoginForm';
import { __testables__ } from '../LoginForm';
const {
formikConfig,
formikEnhancer,
validationSchema,
mapPropsToValues,
handleSubmit,
Expand Down Expand Up @@ -76,7 +74,7 @@ describe('Login form test suite', () => {
expect(checkbox.props().error).toBeTruthy();
});

it('should render error if touched already', () => {
it('should render error if not yet touched', () => {
// generate error state
const props = {
...getFormProps(),
Expand Down
42 changes: 31 additions & 11 deletions src/auth/login/__tests__/LoginView.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { mount } from 'enzyme';
import toJson from 'enzyme-to-json';

import 'jest-styled-components';
import { MemoryRouter } from 'react-router-dom';

Expand All @@ -9,23 +9,28 @@ import LoginForm from '../LoginForm';
import { AuthHeader, AuthFooter } from '../../AuthComponents';

describe('LoginView test suite', () => {
const initialProps = {
isAuthenticated: false,
histroy: {
push: jest.fn(),
},
loginRequest: {
error: null,
loading: false,
},
};
let initialProps;

const getLoginView = (props) => mount(
<MemoryRouter>
<LoginView {...props} />
</MemoryRouter>
);

beforeEach(() => {
initialProps = {
isAuthenticated: false,
histroy: {
push: jest.fn(),
},
loginRequest: {
error: null,
loading: false,
},
logInUser: jest.fn(),
};
});

it('should render', () => {
const wrapper = getLoginView(initialProps);
expect(wrapper).toBeTruthy();
Expand Down Expand Up @@ -55,4 +60,19 @@ describe('LoginView test suite', () => {
const loginForm = wrapper.find(LoginForm);
expect(loginForm.props().error).toEqual(message);
});

it('should handle onSubmit', () => {
const wrapper = getLoginView(initialProps);
expect(wrapper.exists()).toBeTruthy();

const loginForm = wrapper.find(LoginForm);
expect(loginForm.exists()).toBeTruthy();

// fire submit
expect(initialProps.logInUser).not.toHaveBeenCalled();
loginForm.props().onSubmit();

// check callback
expect(initialProps.logInUser).toHaveBeenCalled();
});
});
54 changes: 38 additions & 16 deletions src/auth/register/CheckEmailForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ import Message from '../../ui/form/Message';
import ButtonGroup from '../../ui/form/ButtonGroup';

// formik setups
const formikEnhancer = withFormik({
validationSchema: Yup.object().shape({
email: Yup.string()
.email('Invalid email address')
.required('Email is required!'),
}),
mapPropsToValues: (obj) => {
const { user } = obj;
return {
...user,
}
},
handleSubmit: (payload, { props }, b, c) => {
props.onSubmit(payload);
},
displayName: 'CheckEmailForm',
const validationSchema = Yup.object().shape({
email: Yup.string()
.email('Invalid email address')
.required('Email is required!'),
});

const mapPropsToValues = (obj) => {
const { user } = obj;
return {
...user,
}
};

const handleSubmit = (payload, { props }, b, c) => {
props.onSubmit(payload);
};

const formikConfig = {
validationSchema,
mapPropsToValues,
handleSubmit,
displayName: 'CheckEmailForm',
};

const formikEnhancer = withFormik(formikConfig);

// styled
const StyledCheckEmailForm = styled.form``;

Expand Down Expand Up @@ -75,13 +83,15 @@ const CheckEmailForm = (props) => {
buttonStyle="link"
onClick={handleReset}
disabled={!dirty || submitting}
testId="jestResetButton"
>Cancel</Button>
<Button
type="submit"
disabled={submitting}
buttonStyle="primary"
style={{ marginLeft: '1rem' }}
loading={submitting}
testId="jestSubmitButton"
>Check email</Button>
</ButtonGroup>
</StyledCheckEmailForm>
Expand All @@ -95,13 +105,25 @@ EnhancedCheckEmailForm.propTypes = {
email: PropTypes.string,
}),
validEmail: PropTypes.bool,
error: PropTypes.string,
};

EnhancedCheckEmailForm.defaultProps = {
user: {
email: '',
},
validEmail: null,
error: '',
};


export const __testables__ = {
formikConfig,
formikEnhancer,
validationSchema,
mapPropsToValues,
handleSubmit,
CheckEmailForm,
};

export default EnhancedCheckEmailForm;
5 changes: 5 additions & 0 deletions src/auth/register/Container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ const mapDispatchToProps = ({
invalidateEmail,
});

export const __testables__ = {
select,
mapDispatchToProps,
};

export default connect(select, mapDispatchToProps)(RegisterView);
76 changes: 52 additions & 24 deletions src/auth/register/RegisterForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,40 @@ import Message from '../../ui/form/Message';
import ButtonGroup from '../../ui/form/ButtonGroup';

// formik setups
const formikEnhancer = withFormik({
validationSchema: Yup.object().shape({
password: Yup.string()
.required('Password is required!'),
firstname: Yup.string()
.min(2)
.max(30)
.required('Firstname is required!'),
lastname: Yup.string()
.min(2)
.max(30)
.required('Lastname is required!'),
}),
mapPropsToValues: (obj) => {
const { user } = obj;
return {
...user,
}
},
handleSubmit: (payload, { props }, b, c) => {
props.onSubmit(payload);
},
displayName: 'RegisterForm',
const validationSchema = Yup.object().shape({
password: Yup.string()
.required('Password is required!'),
firstname: Yup.string()
.min(2)
.max(30)
.required('Firstname is required!'),
lastname: Yup.string()
.min(2)
.max(30)
.required('Lastname is required!'),
});

const mapPropsToValues = (obj) => {
const { user } = obj;
return {
...user,
}
};

const handleSubmit = (payload, { props }, b, c) => {
props.onSubmit(payload);
};


const formikConfig = {
validationSchema,
mapPropsToValues,
handleSubmit,
displayName: 'RegisterForm',
};

const formikEnhancer = withFormik(formikConfig);

// styled
const StyledRegisterForm = styled.form``;

Expand Down Expand Up @@ -69,6 +78,7 @@ const RegisterForm = ({
<Field
id="email"
label="Email"
disabled
>
<TextInput
id="email"
Expand Down Expand Up @@ -126,6 +136,7 @@ const RegisterForm = ({
<Field
id="remember"
label="Remember me"
error={touched.remember && errors.remember}
>
<CheckBox
id="remember"
Expand All @@ -137,20 +148,22 @@ const RegisterForm = ({
onBlur={handleBlur}
/>
</Field>
{error && <Message appearance="error">{error}</Message>}
{error !== null && <Message appearance="error">{error}</Message>}
<ButtonGroup>
<Button
type="button"
buttonStyle="link"
onClick={handleOnReset}
disabled={submitting}
testId="jestResetButton"
>Go back</Button>
<Button
type="submit"
disabled={submitting}
buttonStyle="primary"
style={{ marginLeft: '1rem' }}
loading={submitting}
testId="jestSubmitButton"
>Signup</Button>
</ButtonGroup>
</StyledRegisterForm>
Expand All @@ -166,7 +179,11 @@ EnhancedRegisterForm.propTypes = {
password: PropTypes.string,
remember: PropTypes.bool,
}),
error: PropTypes.string,
validEmail: PropTypes.bool,
submitting: PropTypes.bool.isRequired,
onSubmit: PropTypes.func.isRequired,
onCancel: PropTypes.func.isRequired,
};

EnhancedRegisterForm.defaultProps = {
Expand All @@ -176,6 +193,17 @@ EnhancedRegisterForm.defaultProps = {
password: '',
remember: false,
},
validEmail: undefined,
error: '',
};

export const __testables__ = {
formikConfig,
formikEnhancer,
validationSchema,
mapPropsToValues,
handleSubmit,
RegisterForm,
};

export default EnhancedRegisterForm;
1 change: 1 addition & 0 deletions src/auth/register/RegisterView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const RegisterView = ({
RegisterView.propTypes = {
isAuthenticated: PropTypes.bool.isRequired,
signupRequest: HOCRequestPropTypes.isRequired,
checkEmailRequest: HOCRequestPropTypes.isRequired,
validEmail: PropTypes.bool,
signupUser: PropTypes.func.isRequired,
checkEmail: PropTypes.func.isRequired,
Expand Down
Loading

0 comments on commit 81c883d

Please sign in to comment.