Upload 4.12

This commit is contained in:
Andrew Trieu
2023-05-31 21:14:33 +03:00
parent 1c781aca74
commit 0988a44fe4
5 changed files with 88 additions and 44 deletions

View File

@@ -12,53 +12,84 @@ beforeEach(async () => {
await Blog.insertMany(helper.initialBlogs)
})
test('blogs are returned as json', async () => {
await api
.get('/api/blogs')
.expect(200)
.expect('Content-Type', /application\/json/)
describe('getting blogs', () => {
test('blogs are returned as json', async () => {
await api
.get('/api/blogs')
.expect(200)
.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('all blogs are returned', async () => {
const response = await api.get('/api/blogs')
expect(response.body).toHaveLength(helper.initialBlogs.length)
describe('adding 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 title or url is not added', async () => {
const newBlog = {
author: 'Tester',
likes: 999
}
await api.post('/api/blogs').send(newBlog).expect(400)
const blogsAtEnd = await helper.blogsInDb()
expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length)
})
test('blog without likes is added with 0 likes', async () => {
const newBlog = {
title: 'Another test',
author: 'Tester',
url: 'http:/test.com'
}
await api
.post('/api/blogs')
.send(newBlog)
.expect(201)
.expect('Content-Type', /application\/json/)
const blogsAtEnd = await helper.blogsInDb()
const addedBlog = blogsAtEnd.find(blog => blog.title === 'Another test')
expect(addedBlog.likes).toBe(0)
})
})
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)
describe('blog id check', () => {
test('blog id is defined', async () => {
const response = await api.get('/api/blogs')
expect(response.body[0].id).toBeDefined()
})
})
afterAll(async () => {