diff --git a/exercises/12_spa/books/__tests__/books.test.js b/exercises/12_spa/books/__tests__/books.test.js index 990524e..ade6b75 100644 --- a/exercises/12_spa/books/__tests__/books.test.js +++ b/exercises/12_spa/books/__tests__/books.test.js @@ -3,17 +3,45 @@ /* eslint-disable no-magic-numbers */ +const fs = require('fs') const books = require('../modules/books') jest.mock('../modules/google') -console.log(__dirname) +//console.log(__dirname) + +let goodData describe('searchGoogle', () => { + beforeAll( () => { + const path = './modules/__mocks__/__mockData__/' + goodData = fs.readFileSync(`${path}java.json`, 'utf8') + expect(typeof goodData).toBe('string') + }) + test('make a simple API call', async() => { const search = 'java' const data = await books.searchGoogle(search) - console.log(data) + //console.log(data) + return data }) }) + +describe('extractFields', () => { + + test('extract title fields', () => { + const bookData = books.extractFields(goodData) + //console.log(bookData) + expect(Array.isArray(bookData)).toBeTruthy() + expect(bookData.length).toBe(2) + console.log(bookData) + expect(bookData[0].title).toBe('Java Book One') + expect(bookData[1].title).toBe('Java Book Two') + }) + + test('extract ISBN13 data', () => { + + }) + +}) diff --git a/exercises/12_spa/books/__tests__/google.test.js b/exercises/12_spa/books/__tests__/google.test.js new file mode 100644 index 0000000..956cddf --- /dev/null +++ b/exercises/12_spa/books/__tests__/google.test.js @@ -0,0 +1,19 @@ + +'use strict' + +/* eslint-disable no-magic-numbers */ + +const google = require('../modules/google') + + +describe('searchString', () => { + + test('check response is valid', async() => { + const data = await google.search('java') + expect(typeof data).toBe('string') + const json = JSON.parse(data) + expect(Array.isArray(json.items)).toBeTruthy() + expect(json.items.length).toBe(2) + }) + +}) diff --git a/exercises/12_spa/books/__tests__/utility.test.js b/exercises/12_spa/books/__tests__/utility.test.js index a23d54a..716649e 100644 --- a/exercises/12_spa/books/__tests__/utility.test.js +++ b/exercises/12_spa/books/__tests__/utility.test.js @@ -1,6 +1,8 @@ 'use strict' +/* eslint-disable no-magic-numbers */ + const utility = require('../modules/utility') describe('buildString', () => { diff --git a/exercises/12_spa/books/modules/__mocks__/google.js b/exercises/12_spa/books/modules/__mocks__/google.js index d690faf..63eae18 100644 --- a/exercises/12_spa/books/modules/__mocks__/google.js +++ b/exercises/12_spa/books/modules/__mocks__/google.js @@ -10,12 +10,12 @@ const fs = require('fs') * @param {String} searchString the URL to use for the query */ module.exports.search = async searchString => { - console.log('MOCK function searchGoogle') - console.log(__dirname) + //console.log('MOCK function searchGoogle') + //console.log(__dirname) const file = `./modules/__mocks__/__mockData__/${searchString}.json` - console.log(file) + //console.log(file) const data = fs.readFileSync(file) const json = JSON.parse(data) - console.log(json) + //console.log(json) return JSON.stringify(json, null, 2) } diff --git a/exercises/12_spa/books/modules/books.js b/exercises/12_spa/books/modules/books.js index 669e1b0..80ea570 100644 --- a/exercises/12_spa/books/modules/books.js +++ b/exercises/12_spa/books/modules/books.js @@ -9,16 +9,16 @@ const google = require('./google') const minQueryLen = 3 -module.exports.search = request => new Promise( async resolve => { - console.log(request.query) - if(!request.query.q || request.query.q.length <= minQueryLen) { - console.log('no querystring') - return resolve({status: 200, data: []}) - } - const url = this.buildString('java', 2) - const data = await rest(url) - console.log(data) -}) +// module.exports.search = request => new Promise( async resolve => { +// console.log(request.query) +// if(!request.query.q || request.query.q.length <= minQueryLen) { +// console.log('no querystring') +// return resolve({status: 200, data: []}) +// } +// const url = this.buildString('java', 2) +// const data = await rest(url) +// //console.log(data) +// }) /* -------------------------------------------------------------------------- */ @@ -27,9 +27,20 @@ module.exports.search = request => new Promise( async resolve => { * @param {String} searchString the URL to use for the query */ module.exports.searchGoogle = async searchString => { - console.log('calling function searchGoogle') - console.log(searchString) + //console.log('calling function searchGoogle') + //console.log(searchString) const data = await google.search(searchString) - console.log(data) + //console.log(data) return data } + +module.exports.extractFields = jsonStr => { + const bookArray = [] + const json = JSON.parse(jsonStr) + if(!Array.isArray(json.items)) throw new Error('no book data found') + for(const n of json.items) { + console.log(n) + bookArray.push({title: n.volumeInfo.title}) + } + return bookArray +} diff --git a/exercises/12_spa/books/modules/google.js b/exercises/12_spa/books/modules/google.js index 40919f7..3e407c7 100644 --- a/exercises/12_spa/books/modules/google.js +++ b/exercises/12_spa/books/modules/google.js @@ -11,10 +11,10 @@ const utility = require('./utility') * @param {String} searchString the URL to use for the query */ module.exports.search = async searchString => { - console.log('REGULAR function searchGoogle') + //console.log('REGULAR function searchGoogle') const url = utility.buildString(searchString, 2) - console.log(url) + //console.log(url) const data = await rest(url) - console.log(data.entity) + //console.log(data.entity) return data.entity }