Skip to content

Commit

Permalink
Add unit tests for the endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Madhawa97 committed Aug 23, 2023
1 parent b0454d7 commit 4d7623a
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/routes/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { startServer } from '../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import { dataSource } from '../../configs/dbConfig'

const randomString = Math.random().toString(36)
const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

let server: Express
let agent: supertest.SuperAgentTest

describe('Register and login', () => {
beforeAll(async () => {
server = await startServer(port)
agent = supertest.agent(server)

const testUser = {
email: `test${randomString}@gmail.com`,
password: '123'
}

await supertest(server)
.post('/api/auth/register')
.send(testUser)
.expect(201)

await agent.post('/api/auth/login').send(testUser).expect(200)
}, 5000)

describe('Create mentor route', () => {
it('should return a 200 with a mentor object and the message', async () => {
const mentorInfo = {

Check failure on line 32 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹` with `······`
"application": [

Check failure on line 33 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹"application"` with `········application`
{

Check failure on line 34 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹` with `··········`
"question": "What is your country?",

Check failure on line 35 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹↹"question":·"What·is·your·country?"` with `············question:·'What·is·your·country?'`
"answers": "Singapore"

Check failure on line 36 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹↹"answers":·"Singapore"` with `············answers:·'Singapore'`
},

Check failure on line 37 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹` with `··········`
{

Check failure on line 38 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹` with `··········`
"question": "What is your expertise?",

Check failure on line 39 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹↹"question":·"What·is·your·expertise?"` with `············question:·'What·is·your·expertise?'`
"answers": "Software Engineering"

Check failure on line 40 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹↹"answers":·"Software·Engineering"` with `············answers:·'Software·Engineering'`
},

Check failure on line 41 in src/routes/mentor/mentor.route.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `↹↹↹↹↹` with `··········`
{
"question": "What is your mentoring startegy?",
"answers": "I will provide my insights"
}
],
"categoryId": "60b5b847-99a2-4e47-b35b-81b4284311dd"
}

const response = await agent.post('/api/mentors').send(mentorInfo).expect(200)

expect(response.body).toHaveProperty('mentor')
expect(response.body).toHaveProperty('message')
})

it('should return a 409 when the user is already a mentor or has an pending invitation', async () => {
const mentorInfo = {
"application": [
{
"question": "What is your country?",
"answers": "Singapore"
},
{
"question": "What is your expertise?",
"answers": "Software Engineering"
},
{
"question": "What is your mentoring startegy?",
"answers": "I will provide my insights"
}
],
"categoryId": "60b5b847-99a2-4e47-b35b-81b4284311dd"
}

await supertest(server).post('/api/mentors').send(mentorInfo).expect(401)
})

it('should return a 404 when the category id is not valid', async () => {
const mentorInfo = {
"application": [
{
"question": "What is your country?",
"answers": "Singapore"
},
{
"question": "What is your expertise?",
"answers": "Software Engineering"
},
{
"question": "What is your mentoring startegy?",
"answers": "I will provide my insights"
}
],
"categoryId": "60b5b847-99a2-4e47-b35b-81b4284311xx"
}

await supertest(server).post('/api/mentors').send(mentorInfo).expect(401)
})

it('should return a 401 when a valid access token is not provided', async () => {
await supertest(server).post('/api/mentors').send({}).expect(401)
})

afterAll(async () => {
await dataSource.destroy()
})
})
})

0 comments on commit 4d7623a

Please sign in to comment.