const mongoose = require('mongoose') const supertest = require('supertest') 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') .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('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() })