forked from Code-4-Community/scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
[SSF-52] jest tests for pantries controller/service files #55
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
Open
swarkewalia
wants to merge
10
commits into
main
Choose a base branch
from
sk/SSF-52-backend-pantries-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b48bbb
jest tests for pantries controller/service files
swarkewalia 134fbe2
Merge branch 'main' into sk/SSF-52-backend-pantries-tests
swarkewalia b43b145
Merge branch 'main' into sk/SSF-52-backend-pantries-tests
swarkewalia 60c7a94
controller tests update
skewalia 17ce3f7
Merge branch 'main' into sk/SSF-52-backend-pantries-tests
swarkewalia eea2721
Merge branch 'main' of https://github.com/Code-4-Community/ssf into s…
swarkewalia a572a9b
Merge branch 'sk/SSF-52-backend-pantries-tests' of https://github.com…
swarkewalia 074f0b0
pantries service tests update
swarkewalia 1176da6
deleted unused user line
swarkewalia 552895a
delete cache files
swarkewalia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,212 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { PantriesController } from './pantries.controller'; | ||
| import { PantriesService } from './pantries.service'; | ||
| import { Pantry } from './pantries.entity'; | ||
| import { User } from '../users/user.entity'; | ||
| import { Role } from '../users/types'; | ||
| import { mock } from 'jest-mock-extended'; | ||
| import { PantryApplicationDto } from './dtos/pantry-application.dto'; | ||
|
|
||
| const mockPantriesService = mock<PantriesService>(); | ||
|
|
||
| describe('PantriesController', () => { | ||
| let controller: PantriesController; | ||
|
|
||
| const mockUser = { | ||
| id: 1, | ||
| role: Role.STANDARD_VOLUNTEER, | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| email: '', | ||
| phone: '123-456-7890', | ||
| }; | ||
|
|
||
| const mockPantry = { | ||
| pantryId: 1, | ||
| pantryName: 'Test Pantry', | ||
| addressLine1: '123 Test St', | ||
| addressCity: 'Boston', | ||
| addressState: 'MA', | ||
| addressZip: '02115', | ||
| allergenClients: 'Yes', | ||
| refrigeratedDonation: 'Yes', | ||
| reserveFoodForAllergic: 'Yes', | ||
| dedicatedAllergyFriendly: 'Yes', | ||
| newsletterSubscription: true, | ||
| restrictions: ['Peanuts', 'Dairy'], | ||
| pantryRepresentative: mockUser, | ||
| status: 'pending', | ||
| dateApplied: new Date(), | ||
| activities: ['Food Distribution'], | ||
| itemsInStock: 'Canned goods', | ||
| needMoreOptions: 'More options needed', | ||
| } as Pantry; | ||
|
|
||
| const mockPantryApplication = { | ||
| contactFirstName: 'John', | ||
| contactLastName: 'Doe', | ||
| contactEmail: 'john.doe@example.com', | ||
| contactPhone: '(508) 111-1111', | ||
| pantryName: 'Community Food Pantry', | ||
| addressLine1: '123 Test Street', | ||
| addressCity: 'Boston', | ||
| addressState: 'MA', | ||
| addressZip: '02101', | ||
| allergenClients: '10', | ||
| restrictions: ['Egg allergy'], | ||
| refrigeratedDonation: 'Yes', | ||
| reserveFoodForAllergic: 'Some', | ||
| dedicatedAllergyFriendly: 'Yes', | ||
| activities: [], | ||
| itemsInStock: 'Rice, pasta', | ||
| needMoreOptions: 'More options needed', | ||
| newsletterSubscription: 'Yes', | ||
| } as PantryApplicationDto; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [PantriesController], | ||
| providers: [ | ||
| { | ||
| provide: PantriesService, | ||
| useValue: mockPantriesService, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<PantriesController>(PantriesController); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
|
|
||
| describe('getPendingPantries', () => { | ||
| it('should return an array of pending pantries', async () => { | ||
| mockPantriesService.getPendingPantries.mockResolvedValueOnce([ | ||
| mockPantry, | ||
| ] as Pantry[]); | ||
|
|
||
| const result = await controller.getPendingPantries(); | ||
|
|
||
| expect(result).toEqual([mockPantry] as Pantry[]); | ||
| expect(mockPantriesService.getPendingPantries).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return an empty array if no pending pantries', async () => { | ||
| mockPantriesService.getPendingPantries.mockResolvedValueOnce([]); | ||
|
|
||
| const result = await controller.getPendingPantries(); | ||
|
|
||
| expect(result).toEqual([]); | ||
| expect(mockPantriesService.getPendingPantries).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getPantry', () => { | ||
| it('should return a single pantry by id', async () => { | ||
| mockPantriesService.findOne.mockResolvedValueOnce(mockPantry as Pantry); | ||
|
|
||
| const result = await controller.getPantry(1); | ||
|
|
||
| expect(result).toEqual(mockPantry as Pantry); | ||
| expect(mockPantriesService.findOne).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('should throw NotFoundException if pantry does not exist', async () => { | ||
| mockPantriesService.findOne.mockRejectedValueOnce( | ||
| new Error('Pantry 999 not found'), | ||
| ); | ||
|
|
||
| await expect(controller.getPantry(999)).rejects.toThrow(); | ||
| expect(mockPantriesService.findOne).toHaveBeenCalledWith(999); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getSSFRep', () => { | ||
| it('should return the SSF representative for a pantry', async () => { | ||
| mockPantriesService.findSSFRep.mockResolvedValueOnce(mockUser as User); | ||
|
|
||
| const result = await controller.getSSFRep(1); | ||
|
|
||
| expect(result).toEqual(mockUser as User); | ||
| expect(mockPantriesService.findSSFRep).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('should throw error if pantry does not exist', async () => { | ||
| mockPantriesService.findSSFRep.mockRejectedValueOnce( | ||
| new Error('Pantry 999 not found'), | ||
| ); | ||
|
|
||
| await expect(controller.getSSFRep(999)).rejects.toThrow(); | ||
| expect(mockPantriesService.findSSFRep).toHaveBeenCalledWith(999); | ||
| }); | ||
| }); | ||
|
|
||
| describe('approvePantry', () => { | ||
| it('should approve a pantry', async () => { | ||
| mockPantriesService.approve.mockResolvedValueOnce(undefined); | ||
|
|
||
| await controller.approvePantry(1); | ||
|
|
||
| expect(mockPantriesService.approve).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('should throw error if pantry does not exist', async () => { | ||
| mockPantriesService.approve.mockRejectedValueOnce( | ||
| new Error('Pantry 999 not found'), | ||
| ); | ||
|
|
||
| await expect(controller.approvePantry(999)).rejects.toThrow(); | ||
| expect(mockPantriesService.approve).toHaveBeenCalledWith(999); | ||
| }); | ||
| }); | ||
|
|
||
| describe('denyPantry', () => { | ||
| it('should deny a pantry', async () => { | ||
| mockPantriesService.deny.mockResolvedValueOnce(undefined); | ||
|
|
||
| await controller.denyPantry(1); | ||
|
|
||
| expect(mockPantriesService.deny).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('should throw error if pantry does not exist', async () => { | ||
| mockPantriesService.deny.mockRejectedValueOnce( | ||
| new Error('Pantry 999 not found'), | ||
| ); | ||
|
|
||
| await expect(controller.denyPantry(999)).rejects.toThrow(); | ||
| expect(mockPantriesService.deny).toHaveBeenCalledWith(999); | ||
| }); | ||
| }); | ||
|
|
||
| describe('submitPantryApplication', () => { | ||
| it('should submit a pantry application successfully', async () => { | ||
| mockPantriesService.addPantry.mockResolvedValueOnce(undefined); | ||
|
|
||
| await controller.submitPantryApplication(mockPantryApplication); | ||
|
|
||
| expect(mockPantriesService.addPantry).toHaveBeenCalledWith( | ||
| mockPantryApplication, | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw error if application data is invalid', async () => { | ||
| mockPantriesService.addPantry.mockRejectedValueOnce( | ||
| new Error('Invalid application data'), | ||
| ); | ||
|
|
||
| await expect( | ||
| controller.submitPantryApplication(mockPantryApplication), | ||
| ).rejects.toThrow(); | ||
| expect(mockPantriesService.addPantry).toHaveBeenCalledWith( | ||
| mockPantryApplication, | ||
| ); | ||
| }); | ||
Juwang110 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.