Skip to content

Commit

Permalink
hasError and addError tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Nov 13, 2016
1 parent c3ee61c commit b619d12
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ const Accounts = {
},
validateLogin({ user, password }) {
if (isEmpty(trim(user))) {
this.dispatch({
type: ADD_ERROR,
payload: {
form: 'login',
field: 'user',
error: 'A username or email is required.',
},
this.addError({
form: 'login',
field: 'user',
error: 'A username or email is required.',
});
}
if (isEmpty(trim(password))) {
Expand All @@ -141,14 +138,7 @@ const Accounts = {
},
});
}
return this.hasError('login');
},
hasError(form) {
const formState = this.getAccountsState(this.store.getState()).forms[form];
// Checks all the form's fields for errors and the top level form error
const hasError = keys(formState.fields).reduce((prev, curr) =>
!prev && curr.errors.length > 0) || formState.errors.length > 0;
return hasError;
return !this.hasError('login');
},
login({ user, password }) {
this.setLoading(true);
Expand All @@ -162,8 +152,13 @@ const Accounts = {
field: 'password',
value: password,
});
this.validateLogin({ user, password });
return this.client.login({ user, password })
return Promise.resolve()
.then(() => {
if (!this.validateLogin({ user, password })) {
throw new Error('test error');
}
})
.then(() => this.client.login({ user, password }))
.then(({ accessToken, refreshToken }) => {
// Clear the existing login form
this.dispatch({
Expand Down Expand Up @@ -212,6 +207,20 @@ const Accounts = {
},
});
},
addError({ form, field, error }) {
this.dispatch({
type: ADD_ERROR,
payload: { form, field, error },
});
},
hasError(form) {
const formState = this.getAccountsState(this.store.getState()).forms[form];
// Checks all the form's fields for errors and the top level form error
const hasError = keys(formState.fields).reduce((prev, curr) =>
prev || (prev === false && formState.fields[curr].errors.length > 0)
, false) || formState.errors.length > 0;
return hasError;
},
};


Expand Down
47 changes: 47 additions & 0 deletions src/Accounts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import sinonChai from 'sinon-chai';
import chaiAsPromised from 'chai-as-promised';
import 'localstorage-polyfill';
import Accounts from './Accounts';
import createStore from './createStore';

chai.use(sinonChai);
chai.use(chaiAsPromised);

describe('Accounts', () => {
beforeEach(() => {
Accounts.store = createStore({
reducers: {
accounts: Accounts.reducer,
},
});
});
it('throws error on invalid keys', () => {
() => Accounts.ui.config({
'bad key': 'bad value',
Expand All @@ -35,6 +43,45 @@ describe('Accounts', () => {
expect(password.errors).to.include.members(['Password is required.']);
});
});
describe('addError', () => {
it('adds an error to a field', () => {
Accounts.addError({
form: 'login',
field: 'user',
error: 'error',
});
const errors = Accounts.store.getState().accounts.forms.login.fields.user.errors;
expect(errors).to.eql(['error']);
});
it('adds an error to a form', () => {
Accounts.addError({
form: 'login',
error: 'error',
});
const errors = Accounts.store.getState().accounts.forms.login.errors;
expect(errors).to.eql(['error']);
});
});
describe('hasError', () => {
it('returns true if field has error', () => {
Accounts.addError({
form: 'login',
field: 'user',
error: 'error',
});
expect(Accounts.hasError('login')).to.eql(true);
});
it('return true if has no errors', () => {
expect(Accounts.hasError('login')).to.eql(false);
});
it('returns true if form has error', () => {
Accounts.addError({
form: 'login',
error: 'error',
});
expect(Accounts.hasError('login')).to.eql(true);
});
});
describe('setLoading', () => {
it('set loading', () => {
Accounts.setLoading(true);
Expand Down

0 comments on commit b619d12

Please sign in to comment.