mirror of
https://github.com/hknsh/project-knedita.git
synced 2024-11-28 17:41:15 +00:00
Created update post test
This commit is contained in:
parent
c5e928aeec
commit
735695a7d7
5 changed files with 113 additions and 28 deletions
|
@ -16,7 +16,9 @@ describe('POST /post/create', () => {
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await prisma.post.deleteMany({
|
await prisma.post.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
content: '4764ba063310b6f8bab31e8348b2188a'
|
author: {
|
||||||
|
username
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -33,11 +35,13 @@ describe('POST /post/create', () => {
|
||||||
content: '4764ba063310b6f8bab31e8348b2188a'
|
content: '4764ba063310b6f8bab31e8348b2188a'
|
||||||
}).set('Authorization', `Bearer ${token}`).expect(200)
|
}).set('Authorization', `Bearer ${token}`).expect(200)
|
||||||
|
|
||||||
expect(response.body).toHaveProperty('id')
|
expect(response.body).toEqual(expect.objectContaining({
|
||||||
expect(response.body).toHaveProperty('content')
|
id: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('authorId')
|
content: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('createdAt')
|
authorId: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('updatedAt')
|
createdAt: expect.any(String),
|
||||||
|
updatedAt: expect.any(String)
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with 400 status code if the user send no token', async () => {
|
it('should respond with 400 status code if the user send no token', async () => {
|
||||||
|
|
|
@ -1,19 +1,41 @@
|
||||||
import prisma from '../../db'
|
import prisma from '../../db'
|
||||||
import app from '../../app'
|
import app from '../../app'
|
||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
|
import signUpNewUser from '../utils/create-user'
|
||||||
|
|
||||||
// Post id at the body
|
let token = ''; let username = ''
|
||||||
// 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
|
|
||||||
|
|
||||||
describe('DELETE /post/delete', () => {
|
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 () => {
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -37,11 +37,13 @@ describe('POST /post/info', () => {
|
||||||
it('should respond with 200 status code and return some info about the post', async () => {
|
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)
|
const response = await request(app).get(`/post/info?id=${postId}`).expect(200)
|
||||||
|
|
||||||
expect(response.body).toHaveProperty('id')
|
expect(response.body).toEqual(expect.objectContaining({
|
||||||
expect(response.body).toHaveProperty('content')
|
id: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('createdAt')
|
content: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('updatedAt')
|
createdAt: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('author')
|
updatedAt: expect.any(String),
|
||||||
|
author: expect.any(Object)
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with 400 status code if the post does not exists', async () => {
|
it('should respond with 400 status code if the post does not exists', async () => {
|
||||||
|
|
|
@ -1,9 +1,64 @@
|
||||||
import prisma from '../../db'
|
import prisma from '../../db'
|
||||||
import app from '../../app'
|
import app from '../../app'
|
||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
|
import signUpNewUser from '../utils/create-user'
|
||||||
|
|
||||||
|
let token = ''; let username = ''
|
||||||
|
|
||||||
describe('PUT /post/update', () => {
|
describe('PUT /post/update', () => {
|
||||||
test('should ignore', () => {
|
beforeAll(async () => {
|
||||||
expect(1 + 1).toBe(2)
|
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)
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -9,8 +9,8 @@ describe('PUT /user/update', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const user = await signUpNewUser()
|
const user = await signUpNewUser()
|
||||||
|
|
||||||
username = user.username
|
username = user.username ?? ''
|
||||||
token = user.token
|
token = user.token ?? ''
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
@ -33,8 +33,10 @@ describe('PUT /user/update', () => {
|
||||||
.send(fieldsToUpdate)
|
.send(fieldsToUpdate)
|
||||||
.set('Authorization', `Bearer ${token}`).expect(200)
|
.set('Authorization', `Bearer ${token}`).expect(200)
|
||||||
|
|
||||||
expect(response.body).toHaveProperty('displayName')
|
expect(response.body).toEqual(expect.objectContaining({
|
||||||
expect(response.body).toHaveProperty('username')
|
displayName: expect.any(String),
|
||||||
expect(response.body).toHaveProperty('createdAt')
|
username: expect.any(String),
|
||||||
|
createdAt: expect.any(String)
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue