|
| 1 | +const mongoose = require("mongoose"); |
| 2 | +const supertest = require("supertest"); |
| 3 | +const app = require("../app"); |
| 4 | +const Blog = require("../models/blog"); |
| 5 | +const { initialBlogs, blogsInDb } = require("./test_helper"); |
| 6 | + |
| 7 | +const api = supertest(app); |
| 8 | + |
| 9 | +beforeEach(async() => { |
| 10 | + await Blog.deleteMany({}); |
| 11 | + await Blog.insertMany(initialBlogs); |
| 12 | +}); |
| 13 | + |
| 14 | +test("get blogs", async () => { |
| 15 | + const response = await api |
| 16 | + .get("/api/blogs") |
| 17 | + .expect(200) |
| 18 | + .expect("Content-Type", /application\/json/); |
| 19 | + |
| 20 | + expect(response.body).toHaveLength(0); |
| 21 | +}); |
| 22 | + |
| 23 | +test("save a blog", async () => { |
| 24 | + const newBlog = { |
| 25 | + title: "Type wars", |
| 26 | + author: "Unknown", |
| 27 | + url: "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html", |
| 28 | + likes: 2, |
| 29 | + }; |
| 30 | + |
| 31 | + await api |
| 32 | + .post("/api/blogs") |
| 33 | + .send(newBlog) |
| 34 | + .expect(201) |
| 35 | + .expect("Content-Type", /application\/json/); |
| 36 | + |
| 37 | + const res = await api.get("/api/blogs"); |
| 38 | + expect(res.body).toHaveLength(initialBlogs.length + 1); |
| 39 | + const blogs = res.body.map((blog) => blog.title); |
| 40 | + expect(blogs).toContainEqual("Type wars"); |
| 41 | +}); |
| 42 | + |
| 43 | +test("verify blog list has a unique identifier", async () => { |
| 44 | + const { body: blogs } = await api.get("/api/blogs"); |
| 45 | + |
| 46 | + expect(blogs.forEach((blog) => expect(blog.id).toBeDefined())); |
| 47 | +}); |
| 48 | + |
| 49 | +test("if the likes property is missing, default value to 0", async () => { |
| 50 | + const newBlog = { |
| 51 | + title: "Type wars", |
| 52 | + author: "Unknown", |
| 53 | + url: "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html", |
| 54 | + }; |
| 55 | + |
| 56 | + const { body: savedBlog } = await api |
| 57 | + .post("/api/blogs") |
| 58 | + .send(newBlog) |
| 59 | + .expect(201) |
| 60 | + .expect("Content-Type", /application\/json/); |
| 61 | + |
| 62 | + expect(savedBlog.likes).toBe(0); |
| 63 | +}); |
| 64 | + |
| 65 | +test("return bad request if title and url is missing", async () => { |
| 66 | + const newBlog = { |
| 67 | + author: "John Doe", |
| 68 | + }; |
| 69 | + |
| 70 | + const response = await api.post("/api/blogs").send(newBlog); |
| 71 | + expect(response.statusCode).toBe(400); |
| 72 | + |
| 73 | + const {body:blogs} = await api.get("/api/blogs") |
| 74 | + expect(blogs).toHaveLength(initialBlogs.length) |
| 75 | +}); |
| 76 | + |
| 77 | +test("confirm deletion of a blog", async()=>{ |
| 78 | + const blogsAtStart = await blogsInDb() |
| 79 | + const blogToDelete = blogsAtStart[0] |
| 80 | + |
| 81 | + const response = await api.delete(`/api/blogs/${blogToDelete.id}`) |
| 82 | + expect(response.status).toBe(204) |
| 83 | + |
| 84 | + const {body:blogsAtEnd} = await api.get("/api/blogs") |
| 85 | + expect(blogsAtEnd).toHaveLength(initialBlogs.length - 1) |
| 86 | + const contents = blogsAtEnd.map(blog => blog.title) |
| 87 | + |
| 88 | + expect(contents).not.toContain(blogToDelete.title) |
| 89 | +}) |
| 90 | + |
| 91 | +test("update a blog", async()=>{ |
| 92 | + const blogsAtStart = await blogsInDb(); |
| 93 | + const {title, author, url, id} = blogsAtStart[0] |
| 94 | + |
| 95 | + const blog = {title, url, author, likes: 30} |
| 96 | + |
| 97 | + const {body:updatedBlog} = await api.put(`/api/blogs/${id}`).send(blog).expect(200) |
| 98 | + |
| 99 | + expect(updatedBlog.likes).toBe(30) |
| 100 | +}) |
| 101 | + |
| 102 | +afterAll(async (done) => { |
| 103 | + await mongoose.connection.close(); |
| 104 | + done(); |
| 105 | +}); |
0 commit comments