aboutsummaryrefslogtreecommitdiff
path: root/part4
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2023-06-19 12:40:23 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2023-06-19 12:40:23 -0400
commit8c8249626dc3d50463d4ad89e75fb084faf46ba8 (patch)
tree503612d6763a2c3c2e13f26c619665fe148499ec /part4
parentd9cec6010008040cc2cbbe43be1d16ebbad44e39 (diff)
ex4.4 more unit tests and helper functions with jestHEADmain
Diffstat (limited to 'part4')
-rw-r--r--part4/blog/tests/list_helper.test.js66
-rw-r--r--part4/blog/utils/list_helper.js15
2 files changed, 80 insertions, 1 deletions
diff --git a/part4/blog/tests/list_helper.test.js b/part4/blog/tests/list_helper.test.js
index e10f374..a2a8f94 100644
--- a/part4/blog/tests/list_helper.test.js
+++ b/part4/blog/tests/list_helper.test.js
@@ -6,3 +6,69 @@ test('dummy returns one', () => {
const result = listHelper.dummy(blogs)
expect(result).toBe(1)
})
+
+describe('total likes', () => {
+ 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
+
+ }
+ ] //end of list blogs
+
+ test('multiple blogs in a list, returns the sum of all the likes', () => {
+ const result = listHelper.totalLikes(blogs)
+ expect(result).toBe(36)
+ })
+
+
+})
diff --git a/part4/blog/utils/list_helper.js b/part4/blog/utils/list_helper.js
index e52c23c..871bbc3 100644
--- a/part4/blog/utils/list_helper.js
+++ b/part4/blog/utils/list_helper.js
@@ -1,5 +1,18 @@
+const logger = require('../utils/logger.js')
const dummy = (blogs) => {
return 1
}
-module.exports = {dummy}
+
+const totalLikes = (blogs) => {
+ likes = 0
+ for (const blog of blogs) {
+ likes += blog.likes
+ }
+
+ return likes
+}
+
+module.exports = {
+ dummy, totalLikes
+}