Skip to content

Commit

Permalink
chore(client-test): setup client side testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adesege committed Dec 4, 2017
1 parent 18f1ac9 commit 9261f94
Show file tree
Hide file tree
Showing 104 changed files with 5,230 additions and 1,004 deletions.
6 changes: 6 additions & 0 deletions .bash
@@ -0,0 +1,6 @@
#!/bin/bash
while read FILENAME; do
LCOV_INPUT_FILES="$LCOV_INPUT_FILES -a \"$FILENAME\""
done < <( find $1 -name lcov.info )

eval lcov "${LCOV_INPUT_FILES}" -o $1/$2
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -8,4 +8,7 @@ coverage/
.nyc_output/
.DS_STORE
client/build
.vscode
.vscode
e2e-tests
e2e-test-output
nightwatch.**
10 changes: 5 additions & 5 deletions .travis.yml
Expand Up @@ -19,17 +19,17 @@ node_js:
- node
notifications:
email: false
webhooks: https://coveralls.io/webhook?repo_token=COVERALLS_REPO_TOKEN
services:
- postgresql
before_script:
- npm install -g codeclimate-test-reporter
- npm install -g coveralls
- npm install -g sequelize
- npm install -g codeclimate-test-reporter coveralls sequelize jest istanbul-combine
- psql -c 'drop database if exists travis;' -U postgres
- psql -c 'create database travis;' -U postgres
- npm run build
script:
- npm run coveralls
after_success:
- codeclimate-test-reporter < coverage/lcov.info
- coveralls < coverage/lcov.info
- istanbul-combine -d merged-coverage -p summary -r lcov client/coverage/coverage-*.json coverage/coverage-*.json
- codeclimate-test-reporter < merged-coverage/lcov.info
- coveralls < merged-coverage/lcov.info
15 changes: 14 additions & 1 deletion client/.babelrc
@@ -1,6 +1,19 @@
{
"presets": ["env", "react"],
"plugins": ["transform-object-rest-spread", "transform-es2015-destructuring"],
"plugins": [
"transform-object-rest-spread",
"transform-es2015-destructuring",
[
"module-resolver",
{
"root": ["./src"],
"alias":{
"form": ["./src/components/form"],
"Modal": ["./src/components/Modal"]
}
}
]
],
"env": {
"production": {
"plugins": [
Expand Down
4 changes: 3 additions & 1 deletion client/.env.sample
Expand Up @@ -18,4 +18,6 @@ DISQUS_SHORT_NAME=
ROOT_URL=

TIMEZONE=
SOCKET_URL=
SOCKET_URL=

API_VERSION=
3 changes: 2 additions & 1 deletion client/.eslintrc
Expand Up @@ -5,7 +5,8 @@
"node": true,
"es6": true,
"mocha": true,
"browser": true
"browser": true,
"jest": true
},
"settings": {
"import/resolver": {
Expand Down
3 changes: 2 additions & 1 deletion client/.eslintrc.json
Expand Up @@ -5,7 +5,8 @@
"node": true,
"es6": true,
"mocha": true,
"browser": true
"browser": true,
"jest": true
},
"settings": {
"import/resolver": {
Expand Down
41 changes: 41 additions & 0 deletions client/__tests__/__mocks__/book.js
@@ -0,0 +1,41 @@
export const bookData = {
title: 'A new title',
coverPhotoPath: '',
documentPath: ''
};

export const response = {
message: ['Book added successfully'],
book: {
title: 'A new book title',
id: 1
},
id: 1
};

export const searchResponse = {
data: {
...response.book
}
};

export const getBookResponse = {
books: { ...response.book },
pagination: {
pageSize: 1,
totalCount: 10,
page: 0,
pageCount: 1,
limit: 10
},
message: ['There was an unexpected error']
};

export const responseFailure = {
message: ['The title field is required']
};

export const signinResponseFailure = {
message: ['Sorry, we can\'t find this account']
};

1 change: 1 addition & 0 deletions client/__tests__/__mocks__/file.js
@@ -0,0 +1 @@
export default {};
46 changes: 46 additions & 0 deletions client/__tests__/__mocks__/localStorage.js
@@ -0,0 +1,46 @@

/**
* @class localStorage
*/
class localStorage {
/**
* @static
* @param {any} key
* @param {any} value
* @returns {object} setItem
* @memberOf localStorage
*/
static setItem(key, value) {
return Object.assign(localStorage, { [key]: value });
}

/**
* @static
* @param {any} key
* @returns {object} item
* @memberOf localStorage
*/
static getItem(key) {
return localStorage[key];
}

/**
* @static
* @param {any} key
* @returns {undefined}
* @memberOf localStorage
*/
static removeItem(key) {
delete localStorage[key];
}

/**
* @static
* @returns {undefined}
* @memberOf localStorage
*/
static clear() {
localStorage = {};
}
}
export default localStorage;
1 change: 1 addition & 0 deletions client/__tests__/__mocks__/style.js
@@ -0,0 +1 @@
export default {};
17 changes: 17 additions & 0 deletions client/__tests__/__mocks__/user.js
@@ -0,0 +1,17 @@

export const response = {
message: ['Your account has been created successfully'],
payload: {
group: 'user',
userId: 1,
}
};

export const signupResponseFailure = {
message: ['The name field is required']
};

export const signinResponseFailure = {
message: ['Sorry, we can\'t find this account']
};

1 change: 1 addition & 0 deletions client/__tests__/__mocks__/worker.js
@@ -0,0 +1 @@
export default Object.create(null);
145 changes: 145 additions & 0 deletions client/__tests__/actions/auth.spec.js
@@ -0,0 +1,145 @@
import moxios from 'moxios';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import expect from 'expect';

import {
login,
logout
} from 'actions/auth';
import { userSignupRequestAction } from 'actions/signupActions';
import { response, signupResponseFailure, signinResponseFailure } from '../__mocks__/user';

/* eslint-disable max-nested-callbacks */

const mockStore = configureMockStore([
thunk
]);

describe('# Auth', () => {
beforeEach(() => moxios.install());
afterEach(() => moxios.uninstall());

describe('# Signup', () => {
it('creates SET_CURRENT_USER when signup action is successful', () => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: response,
});
});

const expectedActions = [
{
type: 'ADD_FLASH_MESSAGE',
message: {
text: ['Your account has been created successfully'],
type: 'success'
}
},
{ type: 'SET_CURRENT_USER', user: response.payload }
];

const store = mockStore({ });
return store.dispatch(userSignupRequestAction({}))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

it('should not create a user when signup action fails', () => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 400,
response: signupResponseFailure,
});
});

const expectedActions = [
{
type: 'ADD_FLASH_MESSAGE',
message: {
text: ['The name field is required'],
type: 'error'
}
}
];

const store = mockStore({});
return store.dispatch(userSignupRequestAction({})).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});

describe('# Signin', () => {
it('should create SET_CURRENT_USER when signin action is successful', () => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: response,
});
});

const expectedActions = [
{ type: 'SET_CURRENT_USER', user: response.payload }
];

const store = mockStore({ });
return store.dispatch(login({}))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

it('should not log a user in when signin action fails', () => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 400,
response: signinResponseFailure,
});
});

const expectedActions = [
{
type: 'ADD_FLASH_MESSAGE',
message: {
text: ['Sorry, we can\'t find this account'],
type: 'error'
},
}
];

const store = mockStore({ });
return store.dispatch(login({}))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});

describe('# Logout', () => {
it('should log a user out when the logout action is dispatched', (done) => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: response,
});
});

const expectedActions = [
{ type: 'SET_CURRENT_USER', user: {} }
];

const store = mockStore({ });
store.dispatch(logout());
expect(store.getActions()).toEqual(expectedActions);
done();
});
});
});

0 comments on commit 9261f94

Please sign in to comment.