Skip to content

Commit

Permalink
chore(implementation of client side tests)
Browse files Browse the repository at this point in the history
 - Implementation of tests on bookReducer
 - Building app from commandlien to fix jquery issue
  • Loading branch information
Benny Ogidan authored and Benny Ogidan committed Dec 20, 2017
1 parent c86766b commit 9965f16
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 99 deletions.
153 changes: 96 additions & 57 deletions client/__test__/reducers/bookReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import bookReducer from '../../src/app/reducers/bookReducers';
import { searchBookSuccess } from '../../src/app/actions/searchBooks';
import { fetchBorrowedBooks,
fetchBooks,
fetchRecentBooks,
fetchSelectedBookSuccess,
} from '../../src/app/actions/fetchBooks';
import { createBookSuccess,
import { loanHistorySuccess } from '../../src/app/actions/loanHistory';
import {
createBookSuccess,
updateBookSuccess,
deleteBookSuccess
} from '../../src/app/actions/admin/books';
import {
fetchBooksCategoriesSuccess
Expand All @@ -15,6 +19,13 @@ import { returnBookSuccess } from '../../src/app/actions/returnBooks';
let action;
let newState;

const initialState = {
allBooksList: {},
borrowedBooksList: { },
overdueBooksList: { books: [] },
bookOperations: {}
};

const allBooksList = {
books: [
{
Expand All @@ -29,12 +40,24 @@ const allBooksList = {
}
};

const borrowedBookData = {
books: [
{
id: 1, bookId: 1, title: 'AndelaOne', author: 'Dinobi', quantity: 10
}
],
pagination: {
pageSize: 1, pageNumber: 1, pageCount: 1, totalCount: 1
}
};

const newBook = {
createdBook: {
id: 3, title: 'Sterling', author: 'Oare', quantity: 2
},
updatedBook: {
id: 2, title: 'Amarachi', author: 'Akon'
updatedBookData: {
message: 'Amarachi has been updated successfully',
updatedBook: { id: 2, title: 'Amarachi', author: 'Akon' }
},
bookToBorrow: {
id: 4, title: 'Amarachi', author: 'Akon', categoryId: 1, quantity: 4
Expand All @@ -49,7 +72,7 @@ describe('Book Reducer', () => {

it('should handle action type SEARCH_BOOK_SUCCESS', () => {
action = searchBookSuccess(allBooksList);
newState = bookReducer({}, action);
newState = bookReducer(initialState, action);
expect(newState.allBooksList).toEqual(allBooksList);
expect(newState.allBooksList.books).toEqual(allBooksList.books);
});
Expand All @@ -62,61 +85,77 @@ describe('Book Reducer', () => {
});

it('should handle action type UPDATE_BOOK_SUCCESS', () => {
console.log(newState,'oldey but goodey00000000000000');
action = updateBookSuccess(newBook.updatedBook);
console.log(action,'----------');
action = updateBookSuccess(newBook.updatedBookData);
newState = bookReducer(newState, action);
console.log(newState,'newbie00000000000000');
expect(newState.allBooksList.books).toHaveLength(3);
//expect(newState.allBooksList.books[0]).toEqual(newBook.updatedBook);
expect(newState.allBooksList.books[0])
.toEqual(newBook.updatedBookData.updatedBook);
expect(newState.allBooksList.books[0].title)
.not.toEqual('PewdiPie');
});

it('should handle action to FETCH_BORROWED_BOOKS', () => {
action = fetchBorrowedBooks(borrowedBookData);
newState = bookReducer(newState, action);
expect(newState).not.toEqual(bookReducer({}, action));
expect(newState.borrowedBooksList.books).toEqual(borrowedBookData.books);
expect(newState.borrowedBooksList.books[0].id).toBe(1);
});

it('should handle action to FETCH_ALL_BOOKS', () => {
action = fetchBooks(allBooksList);
newState = bookReducer(newState, action);
expect(newState).not.toEqual(bookReducer({}, action));
expect(newState.allBooksList).toEqual(allBooksList);
});

// it('should handle action to FETCH_BORROWED_BOOKS', () => {
// action = fetchBorrowedBooks(allBooksList.books);
// newState = bookReducer(newState, action);
// expect(newState).not.toEqual(bookReducer({}, action));
// expect(newState.borrowedBooksList).toEqual(allBooksList.books);
// expect(newState.borrowedBooksList[1].id).toBe(2);
// });

// it('should handle action to FETCH_ALL_BOOKS', () => {
// action = fetchBooks(allBooksList.books);
// newState = bookReducer(newState, action);
// expect(newState).not.toEqual(bookReducer({}, action));
// expect(newState.borrowedBooksList).toEqual(allBooksList.books);
// expect(newState.borrowedBooksList[1].id).toBe(2);
// });

// it('should handle action to FETCH_SELECTED_BOOK_SUCCESS', () => {
// action = fetchSelectedBookSuccess(newBook.createdBook);
// newState = bookReducer(newState, action);
// expect(newState).not.toEqual(bookReducer({}, action));
// expect(newState.book).toEqual(newBook.createdBook);
// });

// it('should handle action to FETCH_SELECTED_BOOK_SUCCESS', () => {
// action = fetchSelectedBookSuccess(newBook.createdBook);
// newState = bookReducer(newState, action);
// expect(newState).not.toEqual(bookReducer({}, action));
// expect(newState.book).toEqual(newBook.createdBook);
// });

// it('should handle action to FETCH_BOOKS_FOR_CATEGORIES_SUCCESS', () => {
// action = fetchBooksCategoriesSuccess(allBooksList.books);
// newState = bookReducer(newState, action);
// expect(newState).not.toEqual(bookReducer({}, action));
// expect(newState.allBooksList).toEqual(allBooksList.books);
// });

// it('should handle action to RETURN_BOOK_SUCCESS', () => {
// console.log(newState, '=======');
// action = returnBookSuccess(allBooksList.books[1]);
// console.log(action, 'i Am an action');
// newState = bookReducer(newState, action);
// console.log(newState, '....................');
// // console.log(newState,'vvvvvvv', action, '>>>>>>>>>????')

// // expect(newState).not.toEqual(bookReducer({}, action));
// // expect(newState.returnedBook).toEqual(newBook.bookToBorrow);
// });
it('should handle action to FETCH_ALL_RECENT_BOOKS', () => {
action = fetchRecentBooks(allBooksList);
newState = bookReducer(newState, action);
expect(newState).not.toEqual(bookReducer({}, action));
expect(newState.allBooksList).toEqual(allBooksList);
});

it('should handle action to FETCH_SELECTED_BOOK_SUCCESS', () => {
action = fetchSelectedBookSuccess(newBook.createdBook);
newState = bookReducer(newState, action);
expect(newState).not.toEqual(bookReducer({}, action));
expect(newState.book).toEqual(newBook.createdBook);
});

it('should handle action to FETCH_SELECTED_BOOK_SUCCESS', () => {
action = fetchSelectedBookSuccess(newBook.createdBook);
newState = bookReducer(newState, action);
expect(newState).not.toEqual(bookReducer({}, action));
expect(newState.book).toEqual(newBook.createdBook);
});

it('should handle action to FETCH_BOOKS_FOR_CATEGORIES_SUCCESS', () => {
action = fetchBooksCategoriesSuccess(allBooksList);
newState = bookReducer(newState, action);
expect(newState).not.toEqual(bookReducer({}, action));
expect(newState.allBooksList).toEqual(allBooksList);
});

it('should handle action to RETURN_BOOK_SUCCESS', () => {
action = returnBookSuccess(borrowedBookData.books[0]);
newState = bookReducer(newState, action);
expect(newState.borrowedBooksList.books).toHaveLength(0);
expect(newState.borrowedBooksList.books).toEqual([]);
});

it('should handle action to LOAN_HISTORY_SUCCESS', () => {
action = loanHistorySuccess(borrowedBookData);
newState = bookReducer(newState, action);
expect(newState.bookOperations.books).toHaveLength(1);
expect(newState.bookOperations.books).toEqual(borrowedBookData.books);
});

it('should handle action to DELETE_BOOK_SUCCESS', () => {
action = deleteBookSuccess(allBooksList.books[0]);
newState = bookReducer(newState, action);
console.log(newState,'>>>>>>>',action)
// expect(newState.borrowedBooksList.books).toHaveLength(0);
// expect(newState.borrowedBooksList.books).toEqual([]);
});
});
12 changes: 3 additions & 9 deletions client/src/app/actions/admin/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
import {
CREATE_BOOK_SUCCESS,
UPDATE_BOOK_SUCCESS,
DELETE_BOOK_FAILURE,
DELETE_BOOK_SUCCESS
} from '../actionType';
import api from '../api';
Expand All @@ -21,16 +20,12 @@ export const updateBookSuccess = book =>
type: UPDATE_BOOK_SUCCESS,
book
});
export const DeleteBookSuccess = book =>
export const deleteBookSuccess = book =>
({
type: DELETE_BOOK_SUCCESS,
book
});
export const DeleteBookFailure = error =>
({
type: DELETE_BOOK_FAILURE,
error
});


/**
* async helper function: add Book to the database
Expand Down Expand Up @@ -74,10 +69,9 @@ export const deleteBookAction = bookId => dispatch => api
.admin
.deleteBook(bookId)
.then((response) => {
dispatch(DeleteBookSuccess(response));
dispatch(deleteBookSuccess(response));
dispatch(showSuccessNotification(response));
})
.catch((error) => {
dispatch(showErrorNotification({ error }));
dispatch(DeleteBookFailure({ error }));
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ class DisplayAllBooks extends React.Component {
*
*/
handleClick() {
$(document).ready(() => {
$('#add-admin-book-modal').modal('open');
});

}


Expand Down
20 changes: 11 additions & 9 deletions client/src/app/components/presentation/common/book/DisplayBook.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ class Book extends React.Component {
this
.props
.deleteBookAction(id);
this
.props
this.props
.fetchAllBooks(this.props.offset, this.props.limit);
swal('Deleted!', 'Your file has been deleted.', 'success');
} else {
Expand All @@ -93,7 +92,9 @@ class Book extends React.Component {
*
*
* @description Edit button click
*
* @method handleEdit
*
* @memberof Book
* @param {object} book
* @returns {function} a function that fetches the book id
Expand All @@ -102,9 +103,7 @@ class Book extends React.Component {
this
.props
.fetchSelectedBook(book.id);
$(document).ready(() => {
$('#admin-book-modal').modal('open');
});
$('#admin-book-modal').modal('open');
}
/**
*
Expand All @@ -122,13 +121,16 @@ class Book extends React.Component {
{this.props.isAdmin === true
? (
<div>
<a
<Button
floating
icon="mode_edit"
href="#admin-book-modal"
onClick={() => this.handleEdit(this.props.book)}
className="btn-floating #f57c00 orange waves-effect waves-light darken-2 book-icons-1 modal-trigger"
><i class="large material-icons">mode_edit</i>
className="#f57c00 orange darken-2 book-icons-1 modal-trigger"
waves="light"
>
Edit
</a>
</Button>

<Button
onClick={() => this.handleDelete(this.props.book.id)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class DisplayBookModal extends React.Component {
*
*
* @param {param} event
*
* @returns {function} function
*
* @memberOf DisplayBookModal
*/
handleReturnClick = () => {
Expand All @@ -61,6 +63,7 @@ class DisplayBookModal extends React.Component {
*
*
* @param {param} loanStatus
*
* @returns {Component} Component
*
* @memberOf DisplayBookModal
Expand Down Expand Up @@ -183,7 +186,7 @@ class DisplayBookModal extends React.Component {
}

DisplayBookModal.defaultProps = {
book: {},
book: [],
borrowedBooksList: []
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { PropTypes } from 'prop-types';

/**
* @description Component for Navigation
*
* @class NavigationBar
*
* @param {object} props
*
* @extends {Component}
*
* @return {object} Navigation fixed navigation bar
*/
const Navigation = (props) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ class EditBookModal extends React.Component {

EditBookModal.defaultProps = {
header: 'Edit Book',
book: null
// book: null
};


EditBookModal.propTypes = {
book: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.object
]),
// book: PropTypes.oneOfType([
// PropTypes.string,
// PropTypes.number,
// PropTypes.object
// ]),
header: PropTypes.string,
updateBookDetails: PropTypes.func.isRequired
};
Expand Down
4 changes: 4 additions & 0 deletions client/src/app/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ i.medium {
.overlay-main {
max-width: 100%;
}

.card {
max-width: 55%
}

}

Expand Down
Loading

0 comments on commit 9965f16

Please sign in to comment.