Ivan - Fix unit testing [skip ci]

This commit is contained in:
ivan 2016-08-11 04:28:53 -03:00
parent 123c008636
commit 35cf17b114
8 changed files with 73 additions and 156 deletions

View File

@ -1,47 +0,0 @@
const App = requireUnit('app/App', {
});
describe('App component', function () {
describe('when reacting to CommonStore', function () {
let app;
beforeEach(function () {
app = TestUtils.renderIntoDocument(
<App><span>MOCK_CHILD</span></App>
);
app.context = {
router: {
push: stub()
},
location: {
pathname: 'MOCK_PATH'
}
};
spy(app, 'forceUpdate');
});
it('should update with i18n', function () {
app.context.router.push.reset();
app.forceUpdate.reset();
app.onCommonStoreChanged('i18n');
expect(app.context.router.push).to.have.been.calledWith('MOCK_PATH');
});
it('should redirect when logged in', function () {
app.context.router.push.reset();
app.onCommonStoreChanged('logged');
expect(app.context.router.push).to.have.been.calledWith('/app/dashboard');
});
it('should redirect when logged out', function () {
app.context.router.push.reset();
app.onCommonStoreChanged('loggedOut');
expect(app.context.router.push).to.have.been.calledWith('/app');
});
});
});

View File

@ -1,27 +0,0 @@
const DashboardLayout = requireUnit('app/main/dashboard/dashboard-layout', {
'app/main/dashboard/dashboard-menu': ReactMock()
});
describe('Dashboard page', function () {
afterEach(function () {
UserStore.isLoggedIn.returns(false);
});
it('should trigger common action if user is not logged', function () {
CommonActions.loggedOut.reset();
UserStore.isLoggedIn.returns(false);
TestUtils.renderIntoDocument(<DashboardLayout />);
expect(CommonActions.loggedOut).to.have.been.called;
});
it('should not trigger common action user if is logged', function () {
CommonActions.loggedOut.reset();
UserStore.isLoggedIn.returns(true);
TestUtils.renderIntoDocument(<DashboardLayout />);
expect(CommonActions.loggedOut).to.not.have.been.called;
});
});

View File

@ -1,3 +1,6 @@
const SessionActionsMock = require('actions/__mocks__/session-actions-mock');
const APICallMock = require('lib-app/__mocks__/api-call-mock');
const SubmitButton = ReactMock();
const Button = ReactMock();
const Input = ReactMock();
@ -8,6 +11,9 @@ const Widget = ReactMock();
const WidgetTransition = ReactMock();
const MainHomePageLoginWidget = requireUnit('app/main/main-home/main-home-page-login-widget', {
'react-redux': ReduxMock,
'actions/session-actions': SessionActionsMock,
'lib-app/api-call': APICallMock,
'core-components/submit-button': SubmitButton,
'core-components/button': Button,
'core-components/input': Input,
@ -24,9 +30,12 @@ describe('Login/Recover Widget', function () {
let loginWidget, loginForm, widgetTransition, inputs, checkbox, component,
forgotPasswordButton, submitButton;
beforeEach(function () {
component = TestUtils.renderIntoDocument(
<MainHomePageLoginWidget />
let dispatch = stub();
function renderComponent(props = {session: {pending: false, failed: false}}) {
component = reRenderIntoDocument(
<MainHomePageLoginWidget dispatch={dispatch} {...props}/>
);
widgetTransition = TestUtils.scryRenderedComponentsWithType(component, WidgetTransition)[0];
loginWidget = TestUtils.scryRenderedComponentsWithType(component, Widget)[0];
@ -43,7 +52,9 @@ describe('Login/Recover Widget', function () {
}
}
};
});
}
beforeEach(renderComponent);
it('should control form errors by prop', function () {
expect(loginForm.props.errors).to.deep.equal({});
@ -53,25 +64,38 @@ describe('Login/Recover Widget', function () {
it('should trigger login action when submitted', function () {
let mockSubmitData = {email: 'MOCK_VALUE', password: 'MOCK_VALUE'};
let actionMock = {};
SessionActionsMock.login.returns(actionMock);
dispatch.reset();
UserActions.login.reset();
loginForm.props.onSubmit(mockSubmitData);
expect(UserActions.login).to.have.been.calledWith(mockSubmitData);
expect(SessionActionsMock.login).to.have.been.calledWith(mockSubmitData);
expect(dispatch).to.have.been.calledWith(actionMock);
});
it('should set loading true in the form when submitted', function () {
let mockSubmitData = {email: 'MOCK_VALUE', password: 'MOCK_VALUE'};
loginForm.props.onSubmit(mockSubmitData);
it('should set loading true if session login is pending', function () {
expect(loginForm.props.loading).to.equal(false);
renderComponent({
session: {
pending: true,
failed: false
}
});
expect(loginForm.props.loading).to.equal(true);
});
it('should add error and stop loading if login fails', function () {
component.refs.loginForm.refs.password.focus.reset();
component.onUserStoreChanged('LOGIN_FAIL');
component.setState({
loginFormErrors: {}
});
renderComponent({
session: {
pending: false,
failed: true
}
});
expect(loginForm.props.errors).to.deep.equal({password: 'Invalid password'});
expect(loginForm.props.loading).to.equal(false);
expect(component.refs.loginForm.refs.password.focus).to.have.been.called;
});
it('should show back side if \'Forgot your password?\' link is clicked', function () {
@ -85,9 +109,11 @@ describe('Login/Recover Widget', function () {
let recoverWidget, recoverForm, widgetTransition, emailInput, component,
backToLoginButton, submitButton;
let dispatch = stub();
beforeEach(function () {
component = TestUtils.renderIntoDocument(
<MainHomePageLoginWidget />
<MainHomePageLoginWidget dispatch={dispatch} session={{pending: false, failed: false}} />
);
widgetTransition = TestUtils.scryRenderedComponentsWithType(component, WidgetTransition)[0];
recoverWidget = TestUtils.scryRenderedComponentsWithType(component, Widget)[1];
@ -111,12 +137,15 @@ describe('Login/Recover Widget', function () {
expect(recoverForm.props.errors).to.deep.equal({email: 'MOCK_ERROR'});
});
it('should trigger sendRecoverPassword action when submitted', function () {
it('should call sendRecoverPassword when submitted', function () {
let mockSubmitData = {email: 'MOCK_VALUE'};
APICallMock.call.reset();
UserActions.sendRecoverPassword.reset();
recoverForm.props.onSubmit(mockSubmitData);
expect(UserActions.sendRecoverPassword).to.have.been.calledWith(mockSubmitData);
expect(APICallMock.call).to.have.been.calledWith({
path: '/user/send-recover-password',
data: mockSubmitData
});
});
it('should set loading true in the form when submitted', function () {
@ -128,7 +157,8 @@ describe('Login/Recover Widget', function () {
it('should add error and stop loading when send recover fails', function () {
component.refs.recoverForm.refs.email.focus.reset();
component.onUserStoreChanged('SEND_RECOVER_FAIL');
component.onRecoverPasswordFail();
expect(recoverForm.props.errors).to.deep.equal({email: 'Email does not exist'});
expect(recoverForm.props.loading).to.equal(false);
expect(component.refs.recoverForm.refs.email.focus).to.have.been.called;
@ -138,7 +168,7 @@ describe('Login/Recover Widget', function () {
let message = TestUtils.scryRenderedComponentsWithType(component, Message)[0];
expect(message).to.equal(undefined);
component.onUserStoreChanged('SEND_RECOVER_SUCCESS');
component.onRecoverPasswordSent();
message = TestUtils.scryRenderedComponentsWithType(component, Message)[0];
expect(recoverForm.props.loading).to.equal(false);

View File

@ -1,26 +0,0 @@
const MainHomePage = requireUnit('app/main/main-home/main-home-page', {
});
describe('Main home page', function () {
afterEach(function () {
UserStore.isLoggedIn.returns(false);
});
it('should trigger common action if user is currently logged', function () {
CommonActions.logged.reset();
UserStore.isLoggedIn.returns(true);
TestUtils.renderIntoDocument(<MainHomePage />);
expect(CommonActions.logged).to.have.been.called;
});
it('should not trigger common action user if is not logged', function () {
CommonActions.logged.reset();
UserStore.isLoggedIn.returns(false);
TestUtils.renderIntoDocument(<MainHomePage />);
expect(CommonActions.logged).to.not.have.been.called;
});
});

View File

@ -124,9 +124,13 @@ class MainHomePageLoginWidget extends React.Component {
}
getLoginFormErrors() {
return _.extend({}, this.state.loginFormErrors, {
password: (this.props.session.failed) ? i18n('ERROR_PASSWORD') : null
});
let errors = _.extend({}, this.state.loginFormErrors);
if (this.props.session.failed) {
errors.password = i18n('ERROR_PASSWORD');
}
return errors;
}
onLoginFormSubmit(formState) {

View File

@ -1,3 +1,5 @@
const APICallMock = require('lib-app/__mocks__/api-call-mock');
const SubmitButton = ReactMock();
const Button = ReactMock();
const Input = ReactMock();
@ -6,6 +8,7 @@ const Message = ReactMock();
const Widget = ReactMock();
const MainRecoverPasswordPage = requireUnit('app/main/main-recover-password/main-recover-password-page', {
'lib-app/api-call': APICallMock,
'core-components/submit-button': SubmitButton,
'core-components/button': Button,
'core-components/input': Input,
@ -31,12 +34,16 @@ describe('Recover Password form', function () {
});
it('should trigger recoverPassword action when submitted', function () {
UserActions.sendRecoverPassword.reset();
APICallMock.call.reset();
recoverForm.props.onSubmit({password: 'MOCK_VALUE'});
expect(UserActions.recoverPassword).to.have.been.calledWith({
password: 'MOCK_VALUE',
token: 'SOME_TOKEN',
email: 'SOME_EMAIL'
expect(APICallMock.call).to.have.been.calledWith({
path: '/user/recover-password',
data: {
password: 'MOCK_VALUE',
token: 'SOME_TOKEN',
email: 'SOME_EMAIL'
}
});
});
@ -46,7 +53,7 @@ describe('Recover Password form', function () {
});
it('should show message when recover fails', function () {
component.onUserStoreChanged('INVALID_RECOVER');
component.onPasswordRecoverFail();
expect(recoverForm.props.loading).to.equal(false);
let message = TestUtils.scryRenderedComponentsWithType(component, Message)[0];
@ -56,7 +63,7 @@ describe('Recover Password form', function () {
});
it('should show message when recover success', function () {
component.onUserStoreChanged('VALID_RECOVER');
component.onPasswordRecovered();
expect(recoverForm.props.loading).to.equal(false);
let message = TestUtils.scryRenderedComponentsWithType(component, Message)[0];

View File

@ -1,27 +0,0 @@
const MainSignupPage = requireUnit('app/main/main-signup/main-signup-page', {
'react-google-recaptcha': ReactMock()
});
describe('Signup page', function () {
afterEach(function () {
UserStore.isLoggedIn.returns(false);
});
it('should trigger common action if user is currently logged', function () {
CommonActions.logged.reset();
UserStore.isLoggedIn.returns(true);
TestUtils.renderIntoDocument(<MainSignupPage />);
expect(CommonActions.logged).to.have.been.called;
});
it('should not trigger common action user if is not logged', function () {
CommonActions.logged.reset();
UserStore.isLoggedIn.returns(false);
TestUtils.renderIntoDocument(<MainSignupPage />);
expect(CommonActions.logged).to.not.have.been.called;
});
});

View File

@ -32,3 +32,6 @@ global.reRenderIntoDocument = (function () {
return ReactDOM.render(jsx, div);
}
})();
global.ReduxMock = {
connect: stub().returns(stub().returnsArg(0))
};