Upload 4.9

This commit is contained in:
Andrew Trieu
2023-05-31 20:55:51 +03:00
parent 31436ad7fa
commit 1c781aca74
9 changed files with 147 additions and 24 deletions

View File

@@ -4,6 +4,14 @@ const app = require('../app')
const api = supertest(app)
const Blog = require('../models/blog')
const helper = require('./test_helper')
beforeEach(async () => {
await Blog.deleteMany({})
await Blog.insertMany(helper.initialBlogs)
})
test('blogs are returned as json', async () => {
await api
.get('/api/blogs')
@@ -11,6 +19,48 @@ test('blogs are returned as json', async () => {
.expect('Content-Type', /application\/json/)
})
test('all blogs are returned', async () => {
const response = await api.get('/api/blogs')
expect(response.body).toHaveLength(helper.initialBlogs.length)
})
test('a specific blog is within the returned blogs', async () => {
const response = await api.get('/api/blogs')
const contents = response.body.map((r) => r.title)
expect(contents).toContain('Another blog')
})
test('a valid blog can be added ', async () => {
const newBlog = {
title: 'Test',
author: 'Tester',
url: 'http:/test.com',
likes: 999
}
await api
.post('/api/blogs')
.send(newBlog)
.expect(201)
.expect('Content-Type', /application\/json/)
const blogsAtEnd = await helper.blogsInDb()
expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length + 1)
const contents = blogsAtEnd.map((n) => n.title)
expect(contents).toContain('Test')
})
test('blog without content is not added', async () => {
const newBlog = {
likes: 999
}
await api.post('/api/blogs').send(newBlog).expect(400)
const blogsAtEnd = await helper.blogsInDb()
expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length)
})
afterAll(async () => {
await mongoose.connection.close()
})
})

View File

@@ -0,0 +1,45 @@
const Blog = require('../models/blog')
const initialBlogs = [
{
title: 'My blog',
author: 'Andrew',
url: 'www.andrew.eu',
likes: 1000,
},
{
title: 'My blog 2',
author: 'Andrew',
url: 'www.andrew.eu',
likes: 3000,
},
{
title: 'Another blog',
author: 'Hans',
url: 'www.andrew.eu',
likes: 5000,
}
]
const nonExistingId = async () => {
const blog = new Blog({
title: 'Test',
author: 'tester',
url: 'www.test.eu',
likes: 999,
})
await blog.save()
await blog.deleteOne()
return blog._id.toString()
}
const blogsInDb = async () => {
const blogs = await Blog.find({})
return blogs.map(blog => blog.toJSON())
}
module.exports = {
initialBlogs, nonExistingId, blogsInDb
}