Created update post test

This commit is contained in:
Hackntosh 2023-07-11 16:54:50 -03:00
parent c5e928aeec
commit 735695a7d7
5 changed files with 113 additions and 28 deletions

View file

@ -16,7 +16,9 @@ describe('POST /post/create', () => {
afterAll(async () => {
await prisma.post.deleteMany({
where: {
content: '4764ba063310b6f8bab31e8348b2188a'
author: {
username
}
}
})
@ -33,11 +35,13 @@ describe('POST /post/create', () => {
content: '4764ba063310b6f8bab31e8348b2188a'
}).set('Authorization', `Bearer ${token}`).expect(200)
expect(response.body).toHaveProperty('id')
expect(response.body).toHaveProperty('content')
expect(response.body).toHaveProperty('authorId')
expect(response.body).toHaveProperty('createdAt')
expect(response.body).toHaveProperty('updatedAt')
expect(response.body).toEqual(expect.objectContaining({
id: expect.any(String),
content: expect.any(String),
authorId: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String)
}))
})
it('should respond with 400 status code if the user send no token', async () => {

View file

@ -1,19 +1,41 @@
import prisma from '../../db'
import app from '../../app'
import request from 'supertest'
import signUpNewUser from '../utils/create-user'
// Post id at the body
// User token at the header
// Create new post
// Create new user
// Auth the user
// Create the post with the token
// Send the request with the post id and the token
// Should delete the post successfully
let token = ''; let username = ''
describe('DELETE /post/delete', () => {
beforeAll(async () => {
const user = await signUpNewUser()
token = user.token ?? ''
username = user.username ?? ''
})
afterAll(async () => {
await prisma.user.deleteMany({
where: {
username
}
})
await prisma.$disconnect()
})
it('should delete the post successfully', async () => {
expect(1 + 1).toBe(2)
const response = await request(app)
.post('/post/create')
.send({
content: 'lorem ipsum'
})
.set('Authorization', `Bearer ${token}`).expect(200)
await request(app)
.post('/post/delete')
.send({
postId: response.body.id
})
.set('Authorization', `Bearer ${token}`).expect(200)
})
})

View file

@ -37,11 +37,13 @@ describe('POST /post/info', () => {
it('should respond with 200 status code and return some info about the post', async () => {
const response = await request(app).get(`/post/info?id=${postId}`).expect(200)
expect(response.body).toHaveProperty('id')
expect(response.body).toHaveProperty('content')
expect(response.body).toHaveProperty('createdAt')
expect(response.body).toHaveProperty('updatedAt')
expect(response.body).toHaveProperty('author')
expect(response.body).toEqual(expect.objectContaining({
id: expect.any(String),
content: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
author: expect.any(Object)
}))
})
it('should respond with 400 status code if the post does not exists', async () => {

View file

@ -1,9 +1,64 @@
import prisma from '../../db'
import app from '../../app'
import request from 'supertest'
import signUpNewUser from '../utils/create-user'
let token = ''; let username = ''
describe('PUT /post/update', () => {
test('should ignore', () => {
expect(1 + 1).toBe(2)
beforeAll(async () => {
const user = await signUpNewUser()
username = user.username ?? ''
token = user.token ?? ''
})
afterAll(async () => {
await prisma.post.deleteMany({
where: {
author: {
username
}
}
})
await prisma.user.deleteMany({
where: {
username
}
})
await prisma.$disconnect()
})
it('should create a new post and update the content of it', async () => {
const post = await request(app).post('/post/create').send({
content: 'Lorem'
}).set('Authorization', `Bearer ${token}`).expect(200)
expect(post.body).toHaveProperty('id')
const fieldsToUpdate = {
postId: post.body.id,
content: 'Lorem ipsum'
}
const response = await request(app)
.put('/post/update')
.send(fieldsToUpdate)
.set('Authorization', `Bearer ${token}`).expect(200)
// Post content should be Lorem Ipsum
if (post.body.content === response.body.content) {
throw new Error('Post didn\'t update')
}
expect(response.body).toEqual(expect.objectContaining({
id: expect.any(String),
content: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
author: expect.any(Object)
}))
})
})

View file

@ -9,8 +9,8 @@ describe('PUT /user/update', () => {
beforeAll(async () => {
const user = await signUpNewUser()
username = user.username
token = user.token
username = user.username ?? ''
token = user.token ?? ''
})
afterAll(async () => {
@ -33,8 +33,10 @@ describe('PUT /user/update', () => {
.send(fieldsToUpdate)
.set('Authorization', `Bearer ${token}`).expect(200)
expect(response.body).toHaveProperty('displayName')
expect(response.body).toHaveProperty('username')
expect(response.body).toHaveProperty('createdAt')
expect(response.body).toEqual(expect.objectContaining({
displayName: expect.any(String),
username: expect.any(String),
createdAt: expect.any(String)
}))
})
})