Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. const supertest = require('supertest')
  2. const { app, server } = require('../index')
  3. const api = supertest(app)
  4. const Blog = require('../models/blog')
  5.  
  6. beforeAll(async () => {
  7. await Blog.remove({})
  8. const blogObjects = initialBlogs.map(blog => new Blog(blog))
  9. const promiseArray = blogObjects.map(blog => blog.save())
  10. await Promise.all(promiseArray)
  11. })
  12.  
  13. test('blogs are returned as json', async () => {
  14. await api.get('/api/blogs')
  15. .expect(200)
  16. .expect('Content-Type', /application\/json/)
  17. })
  18.  
  19. test('there are two blogs', async () => {
  20. const response = await api.get('/api/blogs')
  21. expect(response.body.length).toBe(initialBlogs.length)
  22. })
  23.  
  24. test('the name of the first blog is Pertin musablogi', async () => {
  25. const response = await api.get('/api/blogs')
  26. expect(response.body[0].title).toBe('Pertin musablogi')
  27. })
  28.  
  29. test('a valid blog can be added ', async () => {
  30. const newBlog = {
  31. title: "Sakarin musablogi",
  32. author: "Sakari Östermalm",
  33. url: "www.sakarinmusablogi.com",
  34. likes: 333
  35. }
  36.  
  37. await api
  38. .post('/api/blogs')
  39. .send(newBlog).expect(200)
  40. .expect('Content-Type', /application\/json/)
  41.  
  42. const response = await api.get('/api/blogs')
  43. const contents = response.body.map(blog => blog.title)
  44.  
  45. expect(response.body.length).toBe(3)
  46. expect(contents).toContain('Sakarin musablogi')
  47. })
  48.  
  49. test('if blog likes are undefined, will post blog with 0 likes', async () => {
  50. const newBlog = {
  51. title: "Blogi, josta kukaan ei tykkää",
  52. author: "Joku",
  53. url: "www.blogi.com"
  54. }
  55.  
  56. await api
  57. .post('/api/blogs')
  58. .send(newBlog).expect(200)
  59. .expect('Content-Type', /application\/json/)
  60.  
  61. const response = await api.get('/api/blogs')
  62.  
  63. expect(response.body[3].likes).toBe(0)
  64. })
  65.  
  66. test('blog without title is not added ', async () => {
  67. const newBlog = {
  68. author: "Tuntematon sotilas",
  69. url: "www.tuntematon.com"
  70. }
  71.  
  72. await api
  73. .post('/api/blogs')
  74. .send(newBlog)
  75. .expect(400)
  76.  
  77. const response = await api
  78. .get('/api/blogs')
  79.  
  80. expect(response.body.length).toBe(4)
  81. })
  82.  
  83. test('blog without url is not added ', async () => {
  84. const newBlog = {
  85. title: "Blogi, josta kukaan ei tykkää",
  86. author: "Joku",
  87. }
  88.  
  89. await api
  90. .post('/api/blogs')
  91. .send(newBlog)
  92. .expect(400)
  93.  
  94. const response = await api
  95. .get('/api/blogs')
  96.  
  97. expect(response.body.length).toBe(4)
  98. })
  99.  
  100. afterAll(() => {
  101. server.close()
  102. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement