Skip to content
Permalink
Browse files
working search facility
  • Loading branch information
Mark Tyers committed Jun 9, 2018
1 parent 7f801f5 commit 41b91509a395234f941b8ececd991a4eabb0391f
Show file tree
Hide file tree
Showing 12 changed files with 599 additions and 31 deletions.
@@ -0,0 +1,2 @@
{
}
@@ -8,9 +8,7 @@ const books = require('../modules/books')

jest.mock('../modules/google')

//console.log(__dirname)

let goodData
const path = './modules/__mocks__/__mockData__/'

describe('searchGoogle', () => {

@@ -20,33 +18,111 @@ describe('searchGoogle', () => {
//console.log(data)
return data
})

})

describe('extractFields', () => {

let goodData

beforeAll( () => {
const path = './modules/__mocks__/__mockData__/'
goodData = fs.readFileSync(`${path}java.json`, 'utf8')
expect(typeof goodData).toBe('string')
})

test('extracted data is in an array', () => {
const bookData = books.extractFields(goodData)
expect(Array.isArray(bookData)).toBeTruthy()
expect(bookData.length).toBe(2)
expect(bookData.length).toBe(20)
})

test('passing string without books array', () => {
expect(() => books.extractFields('{"name": "Mark"}'))
.toThrowError('no book data found')
})

test('passing object instead of string', () => {
expect(() => books.extractFields({name: 'Mark'}))
.toThrowError('parameter has invalid data type')
})

test('extract title fields', () => {
const bookData = books.extractFields(goodData)
console.log(bookData)
expect(bookData[0].title).toBe('Java Book One')
expect(bookData[1].title).toBe('Java Book Two')
expect(bookData[0].title).toBe('Thinking in Java')
expect(bookData[1].title).toBe('Practical Java')
})

test('extract ISBN13 data', () => {
const bookData = books.extractFields(goodData)
expect(bookData[0].isbn).toBe(9780201616460)
expect(bookData[1].isbn).toBe(9789793780146)
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(() => books.buildTable('bad parameter'))
.toThrowError('invalid parameter data type')
})

test('check that parameter is an array (not object)', () => {
expect(() => books.buildTable({name: 'Mark'}))
.toThrowError('invalid parameter data type')
})

test('check the function returns a string', () => {
const table = books.buildTable(goodData)
expect(typeof table).toBe('string')
})

test('build 2 column table', async() => {
const table = books.buildTable(goodData)
// compare text using regex to remove whitespace
expect(table.replace(/\s/g, '')).toBe(goodHTML.replace(/\s/g, ''))
})

})

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() => {
req.query.r = 'something'
delete req.query.q
const result = await books.search(req)
expect(result.replace(/\s/g, '')).toBe('')
})

})
@@ -13,7 +13,7 @@ describe('searchString', () => {
expect(typeof data).toBe('string')
const json = JSON.parse(data)
expect(Array.isArray(json.items)).toBeTruthy()
expect(json.items.length).toBe(2)
expect(json.items.length).toBe(20)
})

})
@@ -4,11 +4,15 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Template</title>
<meta name="description" content="The HTML5 Template">
<meta name="author" content="Joe Bloggs">
<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>
@@ -27,10 +27,10 @@ const port = 8080

app.get('/', async(req, res) => {
try {
console.log(`query ${req.query.q}`)
const bookList = await books.search(req)
console.log(typeof bookList)
console.log(bookList)
res.render('index', {locals: {books: books}})
res.render('index', {locals: {books: bookList}})
} catch(err) {
console.log(err.message)
}
@@ -0,0 +1,80 @@
[
{
"title": "Thinking in Java",
"isbn": 9780131002876
},
{
"title": "Practical Java",
"isbn": 9780201616460
},
{
"title": "Java in a Time of Revolution",
"isbn": 9789793780146
},
{
"title": "Java",
"isbn": 9780764535437
},
{
"title": "The History of Java"
},
{
"title": "The Religion of Java",
"isbn": 9780226285108
},
{
"title": "Natural Language Processing with Java",
"isbn": 9781784398941
},
{
"title": "Java and Modern Europe",
"isbn": 9780700704330
},
{
"title": "Java Web Development Illuminated",
"isbn": 9780763734237
},
{
"title": "Introduction to Neural Networks with Java",
"isbn": 9781604390087
},
{
"title": "TCP/IP Sockets in Java",
"isbn": 9780080568782
},
{
"title": "Java in a Nutshell",
"isbn": 9780596007737
},
{
"title": "Java and XSLT",
"isbn": 9780596001438
},
{
"title": "Wicked Cool Java",
"isbn": 9781593270612
},
{
"title": "Java NIO Ron Hitchens",
"isbn": 9780596002886
},
{
"title": "Effective Java",
"isbn": 9780132778046
},
{
"title": "Java I/O",
"isbn": 9781449390884
},
{
"title": "The History of Java"
},
{
"title": "Programming with Java",
"isbn": 9780070141698
},
{
"title": "Concurrent Programming in Java",
"isbn": 9780201310092
}
]

0 comments on commit 41b9150

Please sign in to comment.