Skip to content

Commit

Permalink
update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
janithlahirukariyawasam committed Jan 14, 2024
1 parent 5e826d3 commit 8e4e59e
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 1 deletion.
121 changes: 121 additions & 0 deletions src/routes/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { startServer } from '../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import { dataSource } from '../../configs/dbConfig'
import { mentorApplicationInfo, mockUser } from '../../../mocks'
import { v4 as uuidv4 } from 'uuid'
import Category from '../../entities/category.entity'
import type Mentor from '../../entities/mentor.entity'

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

let server: Express
let agent: supertest.SuperAgentTest
let savedCategory: Category
let savedMentor: Mentor

describe('Mentor application', () => {
beforeAll(async () => {
server = await startServer(port)
agent = supertest.agent(server)

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

await agent.post('/api/auth/login').send(mockUser).expect(200)

const categoryRepository = dataSource.getRepository(Category)
const newCategory = new Category('Random Category', [])
savedCategory = await categoryRepository.save(newCategory)
}, 5000)

describe('Apply as a mentor route', () => {
it('should return a 201 with a mentor and the message', async () => {
const response = await agent
.post('/api/mentors')
.send({ ...mentorApplicationInfo, categoryId: savedCategory.uuid })
.expect(201)

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

it('should return a 409 when the user is already a mentor or has an pending invitation', async () => {
const response = await agent
.post('/api/mentors')
.send({ ...mentorApplicationInfo, categoryId: savedCategory.uuid })
.expect(409)

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

it('should return a 404 when the category id is not valid', async () => {
await agent
.post('/api/mentors')
.send({ ...mentorApplicationInfo, categoryId: uuidv4() })
.expect(404)
})

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

describe('Update Mentor Availability', () => {
it.each([true, false])(
'should update mentor availability and return a 201 with the updated availability',
async (availability) => {
const response = await agent
.put('/api/mentors/me/availability')
.send({ availability })
.expect(200)

expect(response.body).toHaveProperty('availability', availability)
}
)
})

describe('Get mentor details route', () => {
it('should return a 200 with the mentor details', async () => {
const response = await agent
.get(`/api/mentors/${savedMentor.uuid}`)
.expect(200)

expect(response.body).toHaveProperty('mentorId')
expect(response.body).toHaveProperty('category')
expect(response.body).toHaveProperty('profile')
})

it('should return a 404 when the mentor ID is not found', async () => {
const nonExistentId = uuidv4()
await agent.get(`/api/mentors/${nonExistentId}`).expect(404)
})

it('should return a 200 with the mentor details', async () => {
const response = await supertest(server)
.get(`/api/mentors/${savedMentor.uuid}`)
.expect(200)

expect(response.body).toHaveProperty('mentorId')
expect(response.body).toHaveProperty('category')
expect(response.body).toHaveProperty('profile')
})

it('should return a 404 when the mentor ID is not found', async () => {
const nonExistentId = uuidv4()
const response = await supertest(server)
.get(`/api/mentors/${nonExistentId}`)
.expect(404)
expect(response.body).toHaveProperty('error', 'Mentor not found')
})

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

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

View workflow job for this annotation

GitHub Actions / build

Delete `⏎`
})
120 changes: 120 additions & 0 deletions src/routes/profile/profile.route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { startServer } from '../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import { dataSource } from '../../configs/dbConfig'
import { mockUser } from '../../../mocks'

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

let server: Express
let agent: supertest.SuperAgentTest

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

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

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

describe('Get profile route', () => {
it('should return a 200 with a user profile object', async () => {
const response = await agent.get('/api/me/profile').expect(200)

expect(response.body).toHaveProperty('created_at')
expect(response.body).toHaveProperty('updated_at')
expect(response.body).toHaveProperty('primary_email')
expect(response.body).toHaveProperty('contact_email')
expect(response.body).toHaveProperty('first_name')
expect(response.body).toHaveProperty('last_name')
expect(response.body).toHaveProperty('image_url')
expect(response.body).toHaveProperty('linkedin_url')
expect(response.body).toHaveProperty('type')
expect(response.body).toHaveProperty('uuid')
expect(response.body).not.toHaveProperty('password')
})
})

describe('Update profile route', () => {
it('should update the user profile and return a 200', async () => {
const updatedProfile = {
contact_email: '[email protected]',
first_name: 'John',
last_name: 'Doe',
image_url: 'https://example.com/test_profile_image.jpg',
linkedin_url: 'https://www.linkedin.com/in/johndoe'
}

await agent.put('/api/me/profile').send(updatedProfile).expect(200)
})

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

describe('Get my mentor applications route', () => {
it('should return a 200 with mentor applications for the user', async () => {
const response = await agent
.get('/api/me/applications?type=mentor')
.expect(200)

if (
response.body.message === 'No mentor applications found for the user'
) {
expect(response.body).not.toHaveProperty('mentor applications')
} else {
expect(response.body).toHaveProperty('mentor applications')
expect(response.body['mentor applications']).toBeInstanceOf(Array)
expect(response.body['mentor applications']).toHaveProperty('state')
expect(response.body['mentor applications']).toHaveProperty('category')
expect(response.body['mentor applications']).toHaveProperty(
'availability'
)
expect(response.body['mentor applications']).toHaveProperty('uuid')
expect(response.body['mentor applications']).toHaveProperty(
'created_at'
)
expect(response.body['mentor applications']).toHaveProperty(
'updated_at'
)
}
})

it('should return a 404 for an invalid application type', async () => {
const response = await agent
.get('/api/me/applications?type=invalidType')
.expect(404)

expect(response.body).toHaveProperty(
'message',
'Invalid application type'
)
})

it('should return a 401 when a valid access token is not provided', async () => {
await supertest(server)
.get('/api/me/applications?type=mentor')
.expect(401)
})
})

describe('Delete profile route', () => {
it('should delete the user profile and return a 200', async () => {
await agent.delete('/api/me/profile').expect(200)
})

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

afterAll(async () => {
await dataSource.destroy()
})
})
68 changes: 68 additions & 0 deletions src/services/mentor.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { getAllMentors } from './mentor.service'
import { dataSource } from '../configs/dbConfig'

jest.mock('../configs/dbConfig', () => ({
dataSource: {
getRepository: jest.fn()
}
}))

describe('getAllMentors', () => {
it('should get all mentors without category', async () => {
const mockMentorRepository = createMockMentorRepository([
{ id: 1, name: 'Mentor 1' }
])
;(dataSource.getRepository as jest.Mock).mockReturnValueOnce(
mockMentorRepository
)

const result = await getAllMentors(undefined)

expect(result.statusCode).toBe(200)
expect(result.message).toBe('Mentors found')
expect(result.mentors).toEqual([{ id: 1, name: 'Mentor 1' }])
})

it('should get mentors with category', async () => {
const mockMentorRepository = createMockMentorRepository([
{ id: 1, name: 'Mentor 1' }
])
;(dataSource.getRepository as jest.Mock).mockReturnValueOnce(
mockMentorRepository
)

const result = await getAllMentors('SomeCategory')

expect(result.statusCode).toBe(200)
expect(result.message).toBe('Mentors found')
expect(result.mentors).toEqual([{ id: 1, name: 'Mentor 1' }])
})

it('should return 404 if no mentors found', async () => {
const mockMentorRepository = createMockMentorRepository([])
;(dataSource.getRepository as jest.Mock).mockReturnValueOnce(
mockMentorRepository
)

const result = await getAllMentors('SomeCategory')

expect(result.statusCode).toBe(404)
expect(result.message).toBe('No mentors found')
expect(result.mentors).toBeUndefined()
})
})

function createMockMentorRepository(data: any[] | undefined, error?: Error) {

Check failure on line 55 in src/services/mentor.service.test.ts

View workflow job for this annotation

GitHub Actions / build

Missing return type on function

Check warning on line 55 in src/services/mentor.service.test.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return {
createQueryBuilder: jest.fn().mockReturnThis(),
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockImplementation(async () => {
if (error) {
throw error
}
return data
})
}
}
2 changes: 1 addition & 1 deletion src/services/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const searchMentorsByQuery = async (
}

export const getAllMentors = async (
category: string
category?: string | null
): Promise<{
statusCode: number
mentors?: Mentor[] | null
Expand Down

0 comments on commit 8e4e59e

Please sign in to comment.