-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
41 update get post and put endpoints for donateditem with full program and donor details #49
Changes from all commits
f2e4b3f
4d7f241
15c7b88
16f203f
e2b1fb2
80c953f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const updateItem = { | ||
itemType: 'Updated Book', | ||
currentStatus: 'Received', | ||
programId: 1, | ||
donorId: 1, | ||
dateDonated: new Date().toISOString() | ||
}; | ||
export const newItem = { | ||
itemType: 'Book', | ||
currentStatus: 'Received', | ||
programId: 1, | ||
donorId: 1, | ||
dateDonated: new Date().toISOString() | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const newDonor = { | ||
id: 1, | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[email protected]', | ||
contact: '1234567890', | ||
addressLine1: '123 Main St', | ||
state: 'Missouri', | ||
city: 'St. Louis', | ||
zipcode: '63108', | ||
emailOptIn: false | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const newProgram = { | ||
id: 1, | ||
name: 'Valid Program', | ||
description: 'Valid', | ||
startDate: '2024-10-04', | ||
aimAndCause: 'recycle' | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import request from 'supertest'; | ||
import express from 'express'; | ||
import donatedItemRoutes from '../routes/donatedItemRoutes'; | ||
import mockPrismaClient from '../__mocks__/mockPrismaClient'; | ||
import { newProgram } from '../__mocks__/mockProgram'; | ||
import { newDonor } from '../__mocks__/mockDonor'; | ||
import { updateItem,newItem } from '../__mocks__/mockDonatedItem'; | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.use('/donatedItem', donatedItemRoutes); | ||
|
||
describe('DonatedItem API Tests', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// Setup mock responses | ||
mockPrismaClient.program.findUnique.mockResolvedValue(newProgram); | ||
mockPrismaClient.donor.findUnique.mockResolvedValue(newDonor); | ||
mockPrismaClient.donatedItem.create.mockResolvedValue({ id: 1, ...newItem }); | ||
mockPrismaClient.donatedItem.update.mockResolvedValue({ id: 1, ...updateItem }); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Keeping the required mock resolved values for prisma at one place seems more clear |
||
|
||
// Test POST /donatedItem | ||
it('validates Program name and Donor email before creating a donated item', async () => { | ||
const response = await request(app).post('/donatedItem').send(newItem); | ||
expect(response.status).toBe(201); | ||
expect(mockPrismaClient.donatedItem.create).toHaveBeenCalled(); | ||
|
||
}); | ||
|
||
it('handles errors when the provided Program or Donor does not exist', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the long term, will this unit test be able to work going into the future? Especially when we get to a point where we definitely have more than 30 users? I personally would consider making it a certain amount higher than the amount of users/programs we have. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @truffer11 It wouldn't check in the real or actual database; it will use mock prisma client and you can see at this line that it resolves to value |
||
const programId = 99; | ||
const donorId = 29; | ||
mockPrismaClient.program.findUnique.mockResolvedValue(null); | ||
mockPrismaClient.donor.findUnique.mockResolvedValue(null); | ||
|
||
const response = await request(app).post('/donatedItem').send({ | ||
...newItem, | ||
programId, | ||
donorId, | ||
dateDonated: new Date().toISOString() | ||
}); | ||
expect(response.status).toBe(400); | ||
expect(response.body.error).toContain(`Donor with ID: ${donorId} does not exist.`); | ||
|
||
}); | ||
|
||
// Test PUT /donatedItem/details/{id} | ||
it('updates donated item details correctly', async () => { | ||
const response = await request(app).put('/donatedItem/details/1').send(updateItem); | ||
expect(response.status).toBe(200); | ||
expect(mockPrismaClient.donatedItem.update).toHaveBeenCalled(); | ||
expect(response.body.itemType).toBe(updateItem.itemType); | ||
}); | ||
|
||
it('returns error responses for invalid Program or Donor values', async () => { | ||
mockPrismaClient.program.findUnique.mockResolvedValue(null); | ||
mockPrismaClient.donor.findUnique.mockResolvedValue(null); | ||
|
||
const donorId = 19; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to what I mentioned above, you may want to consider modifying your testing to work beyond just 20 total donors |
||
const programId = 99; | ||
|
||
const response = await request(app).put('/donatedItem/details/1').send({ | ||
...updateItem, | ||
donorId, | ||
programId | ||
}); | ||
expect(response.status).toBe(400); | ||
expect(response.body.error).toContain(`Donor with ID: ${donorId} does not exist.`); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ router.get('/', async (req: Request, res: Response) => { | |
const donors = await prisma.donor.findMany(); | ||
res.json(donors); | ||
} catch (error) { | ||
console.error('Error fetching donor:', error); | ||
res.status(500).json({ message: 'Error fetching donors' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am unable to test what both of these do, but do both of these do different things? At a glance, they seem to have the same functionality |
||
} | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import prisma from '../prismaClient'; // Adjust the import path as necessary | ||
|
||
export async function validateDonor(donorId: number) { | ||
const donor = await prisma.donor.findUnique({ | ||
where: { id: donorId } | ||
}); | ||
if (!donor) { | ||
throw new Error(`Donor with ID: ${donorId} does not exist.`); | ||
} | ||
return donor; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import prisma from '../prismaClient'; // Adjust the import path as necessary | ||
|
||
export async function validateProgram(programId: number) { | ||
const program = await prisma.program.findUnique({ | ||
where: { id: programId } | ||
}); | ||
if (!program) { | ||
throw new Error(`Program ID ${programId} is not valid or does not exist.`); | ||
} | ||
return program; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests look really good and pretty extensive, good job!