diff --git a/Part_4/blog-list/tests/mostLikes.test.js b/Part_4/blog-list/tests/mostLikes.test.js new file mode 100644 index 0000000..6132065 --- /dev/null +++ b/Part_4/blog-list/tests/mostLikes.test.js @@ -0,0 +1,84 @@ +const mostLikes = require('../utils/list_helper').mostLikes + +describe('Most likes', () => { + const emptyList = [] + + const listWithOneBlog = [ + { + _id: "5a422a851b54a676234d17f7", + title: "React patterns", + author: "Michael Chan", + url: "https://reactpatterns.com/", + likes: 7, + __v: 0 + } + ] + const listWithMultipleBlogs = [ + { + _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 + } + ] + + test('for emply list, returns an empty object', () => { + const result = mostLikes(emptyList) + + expect(result).toEqual({}) + }) + + test('for list with only one blog, returns the details for the same blog', () => { + const result = mostLikes(listWithOneBlog) + + expect(result).toEqual({ author: 'Michael Chan', likes: 7 }) + }) + + test('for multiple blogs, return the author and total likes from the blog author with the most likes', () => { + const result = mostLikes(listWithMultipleBlogs) + + expect(result).toEqual({ author: 'Edsger W. Dijkstra', likes: 17}) + }) +}) diff --git a/Part_4/blog-list/utils/list_helper.js b/Part_4/blog-list/utils/list_helper.js index c0ec2f2..b925df6 100644 --- a/Part_4/blog-list/utils/list_helper.js +++ b/Part_4/blog-list/utils/list_helper.js @@ -37,16 +37,36 @@ const mostBlogs = (blogs) => { const authorWithMostBlogs = _.maxBy(_.keys(blogCount), (author) => blogCount[author]) - if (authorWithMostBlogs.length > 1) { - return authorWithMostBlogs[0] + return authorWithMostBlogs +} + +const mostLikes = (blogs) => { + if (blogs.length === 0) { + return {} } - return authorWithMostBlogs + const authorLikes = _.reduce(blogs, (result, blog) => { + if (!result[blog.author]) { + result[blog.author] = 0; + } + result[blog.author] += blog.likes + return result + }, {}) + + const authorWithMostLikes = _.maxBy(_.keys(authorLikes), author => authorLikes[author] ) + + const retObj = { + author: authorWithMostLikes, + likes: authorLikes[authorWithMostLikes] + } + + return retObj } module.exports = { dummy, totalLikes, favBlog, - mostBlogs + mostBlogs, + mostLikes }