Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
1,080 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
'use strict' | ||
|
||
/* eslint-disable no-magic-numbers */ | ||
|
||
const fs = require('fs') | ||
const books = require('../modules/books') | ||
|
||
jest.mock('../modules/google') | ||
|
||
const path = './modules/__mocks__/__mockData__/' | ||
|
||
describe('search', () => { | ||
|
||
let req | ||
|
||
beforeEach( () => { | ||
req = JSON.parse(fs.readFileSync(`${path}req.json`, 'utf8')) | ||
}) | ||
|
||
test('make a search with valid request', async() => { | ||
const result = await books.search(req) | ||
const expected = fs.readFileSync(`${path}validTable.txt`, 'utf8') | ||
// compare text using regex to remove whitespace | ||
expect(result.replace(/\s/g, '')).toBe(expected.replace(/\s/g, '')) | ||
}) | ||
|
||
test('make a search with no query parameters', async() => { | ||
delete req.query | ||
const result = await books.search(req) | ||
expect(result.replace(/\s/g, '')).toBe('') | ||
}) | ||
|
||
test('make a search with missing q query parameter', async() => { | ||
try { | ||
delete req.query.q | ||
await books.search(req) | ||
} catch(err) { | ||
expect(err.message).toMatch('invalid isbn') | ||
} | ||
}) | ||
|
||
}) | ||
|
||
describe('details', () => { | ||
|
||
let req | ||
|
||
beforeEach( () => { | ||
req = JSON.parse(fs.readFileSync(`${path}req.json`, 'utf8')) | ||
}) | ||
|
||
test('return details of a book using a valid isbn number', async() => { | ||
console.log(req) | ||
const result = await books.details(req) | ||
const expected = fs.readFileSync(`${path}9781785885587.json`, 'utf8') | ||
expect(result.replace(/\s/g, '')).toBe(expected.replace(/\s/g, '')) | ||
}) | ||
|
||
// test('request a book with no params', async() => { | ||
// delete req.params | ||
// await books.details(req).rejects.toEqual(Error('invalid isbn')) | ||
// }) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
'use strict' | ||
|
||
/* eslint-disable no-magic-numbers */ | ||
|
||
const google = require('../modules/google') | ||
|
||
|
||
describe('search', () => { | ||
|
||
test('check response is valid', async() => { | ||
const base = 'https://www.googleapis.com/books/v1/volumes' | ||
const fields = 'items(id,volumeInfo(title,industryIdentifiers))' | ||
const url = `${base}?maxResults=20&fields=${fields}&q=java` | ||
const data = await google.search(url) | ||
expect(typeof data).toBe('string') | ||
const json = JSON.parse(data) | ||
expect(Array.isArray(json.items)).toBeTruthy() | ||
expect(json.items.length).toBe(20) | ||
}) | ||
|
||
}) | ||
|
||
describe('getBook', async() => { | ||
|
||
test('get book with valid isbn', async() => { | ||
const fields = 'items(volumeInfo(title,authors,description,publisher))' | ||
const url = `https://www.googleapis.com/books/v1/volumes?fields=${fields}&q=isbn:9781785885587` | ||
const data = await google.getBook(url) | ||
expect(typeof data).toBe('string') | ||
const json = JSON.parse(data) | ||
expect(json.title).toBeTruthy() | ||
expect(json.authors).toBeTruthy() | ||
expect(Array.isArray(json.authors)) | ||
}) | ||
|
||
// toThrowError does not support promises! | ||
test('try to use invalid isbn number', async() => { | ||
try { | ||
const fields = 'items(volumeInfo(title,authors,description,publisher))' | ||
const url = `https://www.googleapis.com/books/v1/volumes?fields=${fields}&q=isbn:9781785885581` | ||
await google.getBook(url) | ||
} catch(err) { | ||
expect(err.message).toMatch('invalid isbn') | ||
} | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
'use strict' | ||
|
||
/* eslint-disable no-magic-numbers */ | ||
|
||
const fs = require('fs') | ||
|
||
const utility = require('../modules/utility') | ||
|
||
const path = './modules/__mocks__/__mockData__/' | ||
|
||
describe('buildString', () => { | ||
|
||
test('build url with default count', () => { | ||
const str = utility.buildString('java') | ||
expect(str) | ||
.toBe('https://www.googleapis.com/books/v1/volumes?maxResults=20&fields=items(id,volumeInfo(title,industryIdentifiers))&q=java') | ||
}) | ||
|
||
test('build url specifying count', () => { | ||
const str = utility.buildString('java', 2) | ||
expect(str) | ||
.toBe('https://www.googleapis.com/books/v1/volumes?maxResults=2&fields=items(id,volumeInfo(title,industryIdentifiers))&q=java') | ||
}) | ||
|
||
test('throw error if count too large', () => { | ||
expect(() => utility.buildString('java', 21)) | ||
.toThrowError('invalid count parameter') | ||
}) | ||
|
||
test('throw error if count 0', () => { | ||
expect(() => utility.buildString('java', 0)) | ||
.toThrowError('invalid count parameter') | ||
}) | ||
|
||
test('throw error if count negative', () => { | ||
expect(() => utility.buildString('test', -1)) | ||
.toThrowError('invalid count parameter') | ||
}) | ||
|
||
}) | ||
|
||
describe('buildBookURL', () => { | ||
|
||
test('build url with valid isbn13 number', () => { | ||
const str = utility.buildBookURL(9780226285108) | ||
expect(str) | ||
.toBe('https://www.googleapis.com/books/v1/volumes?fields=items(volumeInfo(title,authors,description,publisher))&q=isbn:9780226285108') | ||
}) | ||
|
||
test('passing isbn as a string', () => { | ||
expect(() => utility.buildBookURL('9780226285108')) | ||
.toThrowError('parameter has invalid data type') | ||
}) | ||
|
||
test('passing isbn number that is too long', () => { | ||
expect(() => utility.buildBookURL(97802262851081)) | ||
.toThrowError('invalid isbn') | ||
}) | ||
|
||
test('passing isbn number that is too short', () => { | ||
expect(() => utility.buildBookURL(978022628510)) | ||
.toThrowError('invalid isbn') | ||
}) | ||
|
||
}) | ||
|
||
describe('extractFields', () => { | ||
|
||
let goodData | ||
|
||
beforeAll( () => { | ||
goodData = fs.readFileSync(`${path}java.json`, 'utf8') | ||
expect(typeof goodData).toBe('string') | ||
}) | ||
|
||
test('extracted data is in an array', () => { | ||
const bookData = utility.extractFields(goodData) | ||
expect(Array.isArray(bookData)).toBeTruthy() | ||
expect(bookData.length).toBe(20) | ||
}) | ||
|
||
test('passing string without books array', () => { | ||
expect(() => utility.extractFields('{"name": "Mark"}')) | ||
.toThrowError('no book data found') | ||
}) | ||
|
||
test('passing object instead of string', () => { | ||
expect(() => utility.extractFields({name: 'Mark'})) | ||
.toThrowError('parameter has invalid data type') | ||
}) | ||
|
||
test('extract title fields', () => { | ||
const bookData = utility.extractFields(goodData) | ||
expect(bookData[0].title).toBe('Thinking in Java') | ||
expect(bookData[1].title).toBe('Practical Java') | ||
}) | ||
|
||
test('extract ISBN13 data', () => { | ||
const bookData = utility.extractFields(goodData) | ||
expect(bookData[0].isbn).toBe(9780131002876) | ||
expect(bookData[1].isbn).toBe(9780201616460) | ||
}) | ||
|
||
}) | ||
|
||
describe('build table string', () => { | ||
|
||
let goodData | ||
let goodHTML | ||
|
||
beforeAll( () => { | ||
goodData = JSON.parse(fs.readFileSync(`${path}extractedData.json`, 'utf8')) | ||
goodHTML = fs.readFileSync(`${path}validTable.txt`, 'utf8') | ||
}) | ||
|
||
test('check parameter is an object', () => { | ||
expect(typeof goodData).toBe('object') | ||
}) | ||
|
||
test('thow error if parameter is not an object', () => { | ||
expect(() => utility.buildTable('bad parameter')) | ||
.toThrowError('invalid parameter data type') | ||
}) | ||
|
||
test('check that parameter is an array (not object)', () => { | ||
expect(() => utility.buildTable({name: 'Mark'})) | ||
.toThrowError('invalid parameter data type') | ||
}) | ||
|
||
test('check the function returns a string', () => { | ||
const table = utility.buildTable(goodData) | ||
expect(typeof table).toBe('string') | ||
}) | ||
|
||
test('build 2 column table', async() => { | ||
const table = utility.buildTable(goodData) | ||
// compare text using regex to remove whitespace | ||
expect(table.replace(/\s/g, '')).toBe(goodHTML.replace(/\s/g, '')) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
<!doctype html> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Google Book Search</title> | ||
<meta name="description" content="Google Book Search"> | ||
<meta name="author" content="Mark Tyers"> | ||
</head> | ||
<body> | ||
<form action="/" method="get"> | ||
<input type="text" name="q" value=""> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
${books} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const express = require('express') | ||
const es6Renderer = require('express-es6-template-engine') | ||
const bodyParser = require('body-parser') | ||
const app = express() | ||
app.use(express.static('public')) | ||
app.use(bodyParser.urlencoded({ extended: true })) | ||
|
||
app.engine('html', es6Renderer) | ||
app.set('views', 'html') | ||
app.set('view engine', 'html') | ||
|
||
const books = require('./modules/books') | ||
|
||
const port = 8080 | ||
|
||
// const status = { | ||
// ok: 200, | ||
// created: 201, | ||
// notFound: 404, | ||
// notAcceptable: 406, | ||
// conflict: 409 | ||
// } | ||
|
||
app.get('/', async(req, res) => { | ||
try { | ||
console.log(`query ${req.query.q}`) | ||
const bookList = await books.search(req) | ||
console.log(bookList) | ||
res.render('index', {locals: {books: bookList}}) | ||
} catch(err) { | ||
console.log(err.message) | ||
} | ||
}) | ||
|
||
app.listen(port, () => console.log(`app listening on port ${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"title": "Node.Js Design Patterns - Second Edition", | ||
"authors": [ | ||
"Mario Casciaro", | ||
"Luciano Mammino" | ||
], | ||
"publisher": "Packt Publishing", | ||
"description": "Get the best out of Node.js by mastering its most powerful components and patterns to create modular and scalable applications with easeAbout This Book- Create reusable patterns and modules by leveraging the new features of Node.js .- Understand the asynchronous single thread design of node and grasp all its features and patterns to take advantage of various functions.- This unique guide will help you get the most out of Node.js and its ecosystem.Who This Book Is ForThe book is meant for developers and software architects with a basic working knowledge of JavaScript who are interested in acquiring a deeper understanding of how to design and develop enterprise-level Node.js applications.Basic knowledge of Node.js is also helpful to get the most out of this book.What You Will Learn- Design and implement a series of server-side JavaScript patterns so you understand why and when to apply them in different use case scenarios- Become comfortable with writing asynchronous code by leveraging constructs such as callbacks, promises, generators and the async-await syntax- Identify the most important concerns and apply unique tricks to achieve higher scalability and modularity in your Node.js application- Untangle your modules by organizing and connecting them coherently- Reuse well-known techniques to solve common design and coding issues- Explore the latest trends in Universal JavaScript, learn how to write code that runs on both Node.js and the browser and leverage React and its ecosystem to implement universal applicationsIn DetailNode.js is a massively popular software platform that lets you use JavaScript to easily create scalable server-side applications. It allows you to create efficient code, enabling a more sustainable way of writing software made of only one language across the full stack, along with extreme levels of reusability, pragmatism, simplicity, and collaboration. Node.js is revolutionizing the web and the way people and companies create their software.In this book, we will take you on a journey across various ideas and components, and the challenges you would commonly encounter while designing and developing software using the Node.js platform. You will also discover the \"Node.js way\" of dealing with design and coding decisions.The book kicks off by exploring the basics of Node.js describing it's asynchronous single-threaded architecture and the main design patterns. It then shows you how to master the asynchronous control flow patterns,and the stream component and it culminates into a detailed list of Node.js implementations of the most common design patterns as well as some specific design patterns that are exclusive to the Node.js world.Lastly, it dives into more advanced concepts such as Universal Javascript, and scalability' and it's meant to conclude the journey by giving the reader all the necessary concepts to be able to build an enterprise grade application using Node.js.Style and approachThis book takes its intended readers through a comprehensive explanation to create a scalable and efficient real-time server-side apps." | ||
} |
Oops, something went wrong.