-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#41 Updated the files and code according to the PR suggestions
- Loading branch information
1 parent
15c7b88
commit 16f203f
Showing
8 changed files
with
88 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const newItem = { | ||
itemType: 'Book', | ||
currentStatus: 'Received', | ||
programId: 1, | ||
donorId: 1, | ||
dateDonated: new Date().toISOString() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const updateData = { | ||
itemType: 'Updated Book', | ||
currentStatus: 'Received', | ||
programId: 1, | ||
donorId: 1, | ||
dateDonated: new Date().toISOString() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ 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 { newItem } from '../__mocks__/mockNewItem'; | ||
import { updateData } from '../__mocks__/mockUpdateData'; | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
|
@@ -10,52 +14,27 @@ 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, ...updateData }); | ||
}); | ||
|
||
// Test POST /donatedItem | ||
it('validates Program name and Donor email before creating a donated item', async () => { | ||
mockPrismaClient.program.findUnique.mockResolvedValue({ id: 1, name: 'Valid Program', description: 'Valid', startDate:'2024-10-04', aimAndCause:'recycle' }); | ||
const newDonor = { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[email protected]', | ||
contact: '1234567890', | ||
addressLine1: '123 Main St', | ||
state: 'Missouri', | ||
city: 'St. Louis', | ||
zipcode: '63108', | ||
emailOptIn: false, | ||
}; | ||
|
||
mockPrismaClient.donor.findUnique.mockResolvedValue({ | ||
id: 1, | ||
...newDonor, | ||
}); | ||
const newItem = { | ||
itemType: 'Book', | ||
currentStatus: 'Received', | ||
programId: 1, | ||
donorId: 1, | ||
dateDonated: new Date().toISOString() | ||
}; | ||
|
||
mockPrismaClient.donatedItem.create.mockResolvedValue({ | ||
id: 1, | ||
...newItem | ||
}); | ||
|
||
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 () => { | ||
mockPrismaClient.program.findUnique.mockResolvedValue(null); | ||
mockPrismaClient.donor.findUnique.mockResolvedValue(null); | ||
|
||
const response = await request(app).post('/donatedItem').send({ | ||
itemType: 'Book', | ||
currentStatus: 'Received', | ||
...newItem, | ||
programId: 99, | ||
donorId: 29, | ||
dateDonated: new Date().toISOString() | ||
|
@@ -66,36 +45,6 @@ describe('DonatedItem API Tests', () => { | |
|
||
// Test PUT /donatedItem/details/{id} | ||
it('updates donated item details correctly', async () => { | ||
mockPrismaClient.program.findUnique.mockResolvedValue({ id: 1, name: 'Valid Program', description: 'Valid', startDate:'2024-10-04', aimAndCause:'recycle' }); | ||
const newDonor = { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[email protected]', | ||
contact: '1234567890', | ||
addressLine1: '123 Main St', | ||
state: 'Missouri', | ||
city: 'St. Louis', | ||
zipcode: '63108', | ||
emailOptIn: false, | ||
}; | ||
|
||
mockPrismaClient.donor.findUnique.mockResolvedValue({ | ||
id: 1, | ||
...newDonor, | ||
}); | ||
const updateData = { | ||
itemType: 'Updated Book', | ||
currentStatus: 'Received', | ||
programId: 1, | ||
donorId: 1, | ||
dateDonated: new Date().toISOString() | ||
}; | ||
|
||
mockPrismaClient.donatedItem.update.mockResolvedValue({ | ||
id: 1, | ||
...updateData | ||
}); | ||
|
||
const response = await request(app).put('/donatedItem/details/1').send(updateData); | ||
expect(response.status).toBe(200); | ||
expect(mockPrismaClient.donatedItem.update).toHaveBeenCalled(); | ||
|
@@ -105,15 +54,14 @@ describe('DonatedItem API Tests', () => { | |
it('returns error responses for invalid Program or Donor values', async () => { | ||
mockPrismaClient.program.findUnique.mockResolvedValue(null); | ||
mockPrismaClient.donor.findUnique.mockResolvedValue(null); | ||
|
||
const donorId = 19; | ||
const programId = 99; | ||
|
||
const response = await request(app).put('/donatedItem/details/1').send({ | ||
itemType: 'Books', | ||
currentStatus: 'Received', | ||
donorId: donorId, | ||
programId: programId, | ||
dateDonated: new Date().toISOString() | ||
...updateData, | ||
donorId, | ||
programId | ||
}); | ||
expect(response.status).toBe(400); | ||
expect(response.body.error).toContain(`Donor with ID: ${donorId} does not exist.`); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |