This is a BookDonation application and I am trying to test my code using Chai I don't Know how to do it and if I run it it gives me error.
getBook(bookName) : Returns the book object if it exists, otherwise null
getBooksWIthPageCountMoreThanX(pageCount) : Returns all the books that have a pageCount more than the given pageCount. Eg. If the user calls the function with pageCount=1000. It should return all the books that have pagecount >1000
getAuthorBooks(author) : Returns all the books authored by that specific author. Note: some books have more than one author. You should consider those too and return them as well.
getAuthorsBookCount() : Returns a map that contains author name and a count of books their authored .
BooksRepo class
class BooksRepo {
constructor() {
this.fse = require('fs-extra');
this.catalogFilePath = '../data/catalog.books.json';
}
async readFileAsync(filePath) {
let data = await this.fse.readFile(filePath);
let parsedData = await JSON.parse(data);
return parsedData;
}
async getBook(bookName) {
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.title == bookName);
return book;
}
async getBooksWIthPageCountMoreThanX(pagecount){
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.pageCount > pagecount);
return book;
}
async getAuthorBooks(author){
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.authors.contains(author));
return book;
}
async getAuthorsBookCount(){
let authArray = [];
let authCountArray = [];
for (let i = 0; i < inputJson.length; i++) {
authArray = inputJson[i].authors;
for (let j = 0; j < authArray.length; j++) {
authCountArray[authArray[j]] = ((authCountArray[authArray[j]] === undefined) ? 0 : authCountArray[authArray[j]]) + 1;
}
}
return authCountArray[authName];
}
async getBooksbyCatagory(category){
let books = await this.readFileAsync(this.catalogFilePath);
let book = books.find(b => b.categories == category);
return book;
}
}
module.exports = BooksRepo ;
app class
let expect = require('chai').expect
let BooksRepo = require('./BooksRepo');
let booksRepo = new BooksRepo();
describe("BooksRepo class test case" , ()=>{
it('search for book ', () => {
expect(booksRepo.getBook('Code Generation in Action')).to.equal()
});
it('get book with page count ', () => {
expect(booksRepo.getBooksWIthPageCountMoreThanX(1000)).to.equal(null)
});
it('get book with page count ', () => {
expect(booksRepo.getAuthorBooks('Charlie Collins')).to.equal('Charlie Collins')
});
it('get author and number of books he wrote ', () => {
expect(booksRepo.getAuthorsBookCount().to.equal()
});
it('get Books by Catagory ', () => {
expect(booksRepo.getBooksbyCatagory().to.equal()
});
});
catalogFile: Example of how it structured
[
{
"_id": 1,
"title": "Unlocking Android",
"isbn": "1933988673",
"pageCount": 416,
"publishedDate": {
"$date": "2009-04-01T00:00:00.000-0700"
},
"thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson.jpg",
"shortDescription": "Unlocking Android: A Developer's Guide provides concise...",
"longDescription": "Android is an open source mobile phone platform based on the Linux operating ...",
"status": "PUBLISH",
"authors": [
"W. Frank Ableson",
"Charlie Collins",
"Robi Sen"
],
"categories": [
"Open Source",
"Mobile"
]
},