diff --git a/README.md b/README.md index d421cc3..eee2a88 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ Answers to [openfullstack.com](https://fullstackopen.com) course exercises from - [Exercises 4.1.-4.2.](https://fullstackopen.com/en/part4/structure_of_backend_application_introduction_to_testing#exercises-4-1-4-2) _Details solutions: [4.1](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/01104c7798ad53f35ee73994826f5cc3602b9b3e) | [4.2](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/7c913309759236084783f4bdfdec443c5b9e19d5)_ +- [Exercises 4.3.-4.7.](https://fullstackopen.com/en/part4/structure_of_backend_application_introduction_to_testing#exercises-4-3-4-7) + _Details solutions: [4.3](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/0f1a759e78310de711531ed481917496e2f50ec8) | [4.4]() | [4.5]() | [4.6]() | [4.7]()_ + ### b. Testing the backend ### c. User administration diff --git a/tests/blog.test.js b/tests/blog.test.js index b09b23c..1cd9654 100644 --- a/tests/blog.test.js +++ b/tests/blog.test.js @@ -1,9 +1,85 @@ const listHelper = require('../utils/list_helper') -// describe('test arrays', () => { test('dummy returns one', () => { const blogs = [] const result = listHelper.dummy(blogs) expect(result).toBe(1) }) + +describe('total likes', () => { + test('of empty list is zero', () => { + const blogs = [] + const result = listHelper.totalLikes(blogs) + expect(result).toBe(0) + }) + + test('when list has only one blog equals the likes of that', () => { + const blogs = [ + { + _id: '23234323242dssfsf', + title: 'Test 2nd blog title', + author: 'Test of blog author', + url: 'Test of blog url ', + likes: 6, + }, + ] + const result = listHelper.totalLikes(blogs) + expect(result).toBe(6) + }) + + test('of a bigger list is calculated right', () => { + const blogs = [ + { + _id: '5a422a851b54a676234d17f7', + title: 'React patterns', + author: 'Michael Chan', + url: 'https://reactpatterns.com/', + likes: 7, + __v: 0, + }, + { + _id: '5a422aa71b54a676234d17f8', + title: 'Go To Statement Considered Harmful', + author: 'Edsger W. Dijkstra', + url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html', + likes: 5, + __v: 0, + }, + { + _id: '5a422b3a1b54a676234d17f9', + title: 'Canonical string reduction', + author: 'Edsger W. Dijkstra', + url: 'http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html', + likes: 12, + __v: 0, + }, + { + _id: '5a422b891b54a676234d17fa', + title: 'First class tests', + author: 'Robert C. Martin', + url: 'http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll', + likes: 10, + __v: 0, + }, + { + _id: '5a422ba71b54a676234d17fb', + title: 'TDD harms architecture', + author: 'Robert C. Martin', + url: 'http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html', + likes: 0, + __v: 0, + }, + { + _id: '5a422bc61b54a676234d17fc', + title: 'Type wars', + author: 'Robert C. Martin', + url: 'http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html', + likes: 2, + __v: 0, + }, + ] + const result = listHelper.totalLikes(blogs) + expect(result).toBe(36) + }) +}) diff --git a/utils/list_helper.js b/utils/list_helper.js index 7d102e8..0123366 100644 --- a/utils/list_helper.js +++ b/utils/list_helper.js @@ -3,6 +3,14 @@ const dummy = (blogs) => { return 1 } +const totalLikes = (blogs) => { + const reducer = (sum, item) => { + return sum + item.likes + } + return blogs.length === 0 ? 0 : blogs.reduce(reducer, 0) +} + module.exports = { dummy, + totalLikes, }