From 7f801f53203cd2a5b351ab33cdc465df544bca49 Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Fri, 8 Jun 2018 10:39:11 +0100 Subject: [PATCH] added more tests --- .vscode/launch.json | 14 +++++++++++ .../12_spa/books/__tests__/books.test.js | 23 +++++++++++-------- exercises/12_spa/books/modules/books.js | 10 +++++++- 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f0cac87 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Jest Tests", + "program": "${workspaceFolder}/exercises/12_spa/books/node_modules/.bin/jest", + "args": [ + "-i" + ] + } + ] +} \ No newline at end of file diff --git a/exercises/12_spa/books/__tests__/books.test.js b/exercises/12_spa/books/__tests__/books.test.js index ade6b75..f593373 100644 --- a/exercises/12_spa/books/__tests__/books.test.js +++ b/exercises/12_spa/books/__tests__/books.test.js @@ -14,12 +14,6 @@ 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) @@ -30,18 +24,29 @@ describe('searchGoogle', () => { describe('extractFields', () => { - test('extract title fields', () => { + 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) - //console.log(bookData) expect(Array.isArray(bookData)).toBeTruthy() expect(bookData.length).toBe(2) + }) + + 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') }) test('extract ISBN13 data', () => { - + const bookData = books.extractFields(goodData) + expect(bookData[0].isbn).toBe(9780201616460) + expect(bookData[1].isbn).toBe(9789793780146) }) }) diff --git a/exercises/12_spa/books/modules/books.js b/exercises/12_spa/books/modules/books.js index 80ea570..544546d 100644 --- a/exercises/12_spa/books/modules/books.js +++ b/exercises/12_spa/books/modules/books.js @@ -39,8 +39,16 @@ module.exports.extractFields = jsonStr => { const json = JSON.parse(jsonStr) if(!Array.isArray(json.items)) throw new Error('no book data found') for(const n of json.items) { + let item = {} console.log(n) - bookArray.push({title: n.volumeInfo.title}) + item.title = n.volumeInfo.title + for(const m in n.industryIdentifiers) { + //console.log(m) + if(m.type === 'ISBN_13') { + // xxx + } + } + bookArray.push(item) } return bookArray }