From 735695a7d79095c48dc6444cfa28f6808e8ea926 Mon Sep 17 00:00:00 2001 From: CookieDasora Date: Tue, 11 Jul 2023 16:54:50 -0300 Subject: [PATCH] Created update post test --- src/tests/post/post-create.spec.ts | 16 +++++--- src/tests/post/post-delete.spec.ts | 42 ++++++++++++++++----- src/tests/post/post-info.spec.ts | 12 +++--- src/tests/post/post-update.spec.ts | 59 +++++++++++++++++++++++++++++- src/tests/user/user-update.spec.ts | 12 +++--- 5 files changed, 113 insertions(+), 28 deletions(-) diff --git a/src/tests/post/post-create.spec.ts b/src/tests/post/post-create.spec.ts index 4c605ef..b901860 100644 --- a/src/tests/post/post-create.spec.ts +++ b/src/tests/post/post-create.spec.ts @@ -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 () => { diff --git a/src/tests/post/post-delete.spec.ts b/src/tests/post/post-delete.spec.ts index 7839645..b8004fb 100644 --- a/src/tests/post/post-delete.spec.ts +++ b/src/tests/post/post-delete.spec.ts @@ -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) }) }) diff --git a/src/tests/post/post-info.spec.ts b/src/tests/post/post-info.spec.ts index 0faae4c..7a499a3 100644 --- a/src/tests/post/post-info.spec.ts +++ b/src/tests/post/post-info.spec.ts @@ -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 () => { diff --git a/src/tests/post/post-update.spec.ts b/src/tests/post/post-update.spec.ts index 3dc05cc..9e848e7 100644 --- a/src/tests/post/post-update.spec.ts +++ b/src/tests/post/post-update.spec.ts @@ -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) + })) }) }) diff --git a/src/tests/user/user-update.spec.ts b/src/tests/user/user-update.spec.ts index 3c20cd0..dfcb917 100644 --- a/src/tests/user/user-update.spec.ts +++ b/src/tests/user/user-update.spec.ts @@ -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) + })) }) })