Skip to content

Commit

Permalink
solution 4.4: helper functions and unit tests, step2
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 9, 2023
1 parent 0f1a759 commit 2585dc7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 77 additions & 1 deletion tests/blog.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
8 changes: 8 additions & 0 deletions utils/list_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 2585dc7

Please sign in to comment.