Skip to content

Commit

Permalink
ch(test-auth): add test for Auth module
Browse files Browse the repository at this point in the history
- write tests for "auth" component
- write tests for "auth" container

[Deliver #161115790]
  • Loading branch information
veeqtor committed Oct 11, 2018
1 parent c7c0607 commit b0956bd
Show file tree
Hide file tree
Showing 6 changed files with 3,701 additions and 3,479 deletions.
4 changes: 2 additions & 2 deletions client/src/modules/Auth/container/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { errorMessage } from '../../../toasts';
import Button from '../components/GoogleButton';
import Form from '../components/ExtraForm';

class SignIn extends Component {
export class SignIn extends Component {
static propTypes = {
auth: PropTypes.shape({
auth: PropTypes.object.isRequired
Expand Down Expand Up @@ -123,7 +123,7 @@ class SignIn extends Component {
}
}

const mapStateToProps = state => ({
export const mapStateToProps = state => ({
auth: state.auth
});

Expand Down
93 changes: 87 additions & 6 deletions client/src/tests/modules/auth/authMockData.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,92 @@

/* eslint-disable no-undef */

const props = {
handleForm: jest.fn(),
handleChange: jest.fn(),
slackId: 'abndh234456',
githubId: '234535'
extraFormProps: {
handleForm: jest.fn(),
handleChange: jest.fn(),
slackId: 'abndh234456',
githubId: '234535'
},

googleButtonProps: {
handleFailure: jest.fn(),
handleSuccess: jest.fn()
},
containerProps: {
auth: {
auth: {}
},
history: {
push: jest.fn()
},
signUp: jest.fn(),
signIn: jest.fn()
},

containerPropsErrors: {
auth: {
auth: {
errors: 'Not an andela email'
}
}
},
containerPropsMessage: {
auth: {
auth: {
message: 'Login successful'
}
}
},
containerPropsData: {
auth: {
auth: {
data: 'Login successful'
}
},
history: {
push: jest.fn()
}
}
};


const responses = {
success: {
profileObj: {
givenName: 'victor',
email: 'test@andela.com',
googleId: '123456',
imageUrl: 'http://wwww.photo.com'
}
},
error: {
profileObj: {
givenName: 'victor',
email: 'test@google.com',
googleId: '123456',
imageUrl: 'http://wwww.photo.com'
}
}
};

const expected = {
notSuccessful: {
showForm: true,
data: null,
slackId: '',
githubId: ''
},
withMessage: {
showForm: false,
data: null,
slackId: '',
githubId: ''
}
};
export default props;


export {
responses,
props,
expected
};
4 changes: 2 additions & 2 deletions client/src/tests/modules/auth/components/Extraform.test.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { shallow } from 'enzyme';
import Extraform from '../../../../modules/Auth/components/ExtraForm';
import props from '../authMockData';
import { props } from '../authMockData';

describe('<Test Component />', () => {
it('should mount without crashing', () => {
const wrapper = shallow(<Extraform {...props} />);
const wrapper = shallow(<Extraform {...props.extraFormProps} />);
expect(wrapper).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions client/src/tests/modules/auth/components/GoogleButton.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import { shallow } from "enzyme";
import GoogleButton from "../../../../modules/Auth/components/GoogleButton";
import { props } from "../authMockData";

describe("<Google Button Component test />", () => {
it("should mount without crashing", () => {
const wrapper = shallow(<GoogleButton {...props.googleButtonProps} />);
expect(wrapper).toMatchSnapshot();
});
});
89 changes: 89 additions & 0 deletions client/src/tests/modules/auth/container/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from "react";
import { shallow } from "enzyme";
import { SignIn, mapStateToProps } from "../../../../modules/Auth/container";
import { props, responses, expected } from "../authMockData";

describe("<Testing the redux container />", () => {
it("should mount without crashing", () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
expect(wrapper).toMatchSnapshot();
});


it("should test for returned errors on authentication ", () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
wrapper.instance().componentWillReceiveProps(props.containerPropsErrors);
const actual = wrapper.instance().state;
expect(actual).toEqual(expected.notSuccessful);
});


it("should test for returned messages on authentication ", () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
wrapper.instance().componentWillReceiveProps(props.containerPropsMessage);
const actual = wrapper.instance().state;
expect(actual).toEqual(expected.withMessage);
});


it("should test for successful authentication ", () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
wrapper.instance().componentWillReceiveProps(props.containerPropsData);
const actual = props.containerPropsData.history.push;
expect(actual).toHaveBeenCalled();
});


it("should call the complete signUp method", () => {
const spy = jest.fn();
const propsNew = { signUp: spy };
const mockWrapper = shallow(<SignIn
{...props.containerProps}
{...propsNew} />);
mockWrapper.instance().completeSignUp({ preventDefault: jest.fn() });
expect(spy).toHaveBeenCalled();
});


it('Should call the onChange method', () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
wrapper.instance().handleChange({ target: { name: 'name', value: 'victor' } });
const actual = wrapper.instance().state.name;
expect(actual).toEqual('victor');
});


it('Should call the handleSuccess method', () => {
const spy = jest.fn();
const propsNew = { signIn: spy };
const mockWrapper = shallow(<SignIn
{...props.containerProps}
{...propsNew} />);
mockWrapper.instance().handleSuccess(responses.success);
expect(spy).toHaveBeenCalled();
});


it('Should call the handleSuccess method', () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
wrapper.instance().handleSuccess(responses.error);
expect(wrapper).toMatchSnapshot();
});


it('Should call the handleFailure method', () => {
const wrapper = shallow(<SignIn {...props.containerProps} />);
wrapper.instance().handleFailure(responses.error);
const actual = wrapper.instance().state.showForm;
expect(actual).toEqual(false);
});


it('testing map state To Props', () => {
const storeState = mapStateToProps({
auth: true
});
const { auth } = storeState;
expect(auth).toBe(true);
});
});
Loading

0 comments on commit b0956bd

Please sign in to comment.