Skip to content

Commit

Permalink
solution 4.11*: Blog list tests, step4
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 10, 2023
1 parent d9fe5ba commit 379490b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm test
## b. Testing the backend

- [Exercises 4.8.-4.12.](https://fullstackopen.com/es/part4/probando_el_backend#ejercicios-4-8-4-12)
_Details solutions: [4.8](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/f4361d4a4973e98d2368596056f9257e1487565d) | [4.9](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/6b9903229f75ed7def60d4f81a386e61997a9341) | [4.10]() | [4.11]() | [4.12]()_
_Details solutions: [4.8](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/f4361d4a4973e98d2368596056f9257e1487565d) | [4.9](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/6b9903229f75ed7def60d4f81a386e61997a9341) | [4.10](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/d9fe5ba9dffc9e2d5c1b74bb88028436dd4629eb) | [4.11]() | [4.12]()_

## c. User administration

Expand Down
1 change: 1 addition & 0 deletions controllers/blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ blogsRouter.get('/', async (request, response) => {
})

blogsRouter.post('/', async (request, response) => {
request.body.likes = request.body.likes || 0
const blog = new Blog(request.body)
const result = blog.save()
response.status(201).json(result)
Expand Down
10 changes: 8 additions & 2 deletions models/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ const blogSchema = new mongoose.Schema({
required: true,
},
author: String,
url: String,
likes: Number,
url: {
type: String,
required: true,
},
likes: {
type: Number,
default: 0,
},
})

blogSchema.set('toJSON', {
Expand Down
16 changes: 16 additions & 0 deletions tests/note_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ test('unique identifier property of the blog posts is named id,', async () => {
expect(contents.id).toBeDefined()
})

test('if the likes property is missing from the request, it will default to the value 0', async () => {
const newBlog = {
title: 'Fugas o la ansiedad de sentirse vivo',
author: 'A. Pacheco',
url: 'https://unlibroenmimochila.blogspot.com/2017/12/fugas-o-la-ansiedad-de-sentirse-vivo.html',
}
// const response = await api.post('/api/blogs').send(newBlog)
let blogObject = new Blog(newBlog)
try {
const response = await blogObject.save()
expect(response.likes).toBe(0)
} catch (error) {
console.log(error)
}
})

// test('blog without title is not added', async () => {
// const newBlog = {
// author: 'A. Pacheco',
Expand Down

0 comments on commit 379490b

Please sign in to comment.