-
Notifications
You must be signed in to change notification settings - Fork 0
[SSF-79] Improve Peformance for Log New Donation Form Modal #58
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
base: main
Are you sure you want to change the base?
Changes from all commits
c1c0485
09d9357
3d95bbd
706b59f
9c47821
ea1d063
ec957b7
20b9bbe
8e773fe
98b5e25
0c249bc
c971104
8548fc4
9c36a1c
cc585cf
9451c7b
ee7ad64
1106308
c0f4c1e
79df911
bc0e307
2f7c61a
ccc073d
aa6a0ab
72e34dc
9ccc8df
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,115 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { DonationItemsController } from './donationItems.controller'; | ||
| import { DonationItemsService } from './donationItems.service'; | ||
| import { DonationItem } from './donationItems.entity'; | ||
| import { mock } from 'jest-mock-extended'; | ||
| import { FoodType } from './types'; | ||
| import { CreateMultipleDonationItemsDto } from './dtos/create-donation-items.dto'; | ||
|
|
||
| const mockDonationItemsService = mock<DonationItemsService>(); | ||
|
|
||
| describe('DonationItemsController', () => { | ||
| let controller: DonationItemsController; | ||
|
|
||
| const mockDonationItemsCreateData: Partial<DonationItem>[] = [ | ||
| { | ||
| itemId: 1, | ||
| donationId: 1, | ||
| itemName: 'Canned Beans', | ||
| quantity: 100, | ||
| reservedQuantity: 0, | ||
| ozPerItem: 15, | ||
| estimatedValue: 200, | ||
| foodType: FoodType.DAIRY_FREE_ALTERNATIVES, | ||
| }, | ||
| { | ||
| itemId: 2, | ||
| donationId: 1, | ||
| itemName: 'Rice', | ||
| quantity: 50, | ||
| reservedQuantity: 0, | ||
| ozPerItem: 20, | ||
| estimatedValue: 150, | ||
| foodType: FoodType.GLUTEN_FREE_BAKING_PANCAKE_MIXES, | ||
| }, | ||
| ]; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [DonationItemsController], | ||
| providers: [ | ||
| { provide: DonationItemsService, useValue: mockDonationItemsService }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<DonationItemsController>(DonationItemsController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
|
|
||
| describe('create', () => { | ||
| it('should call donationItemsService.create and return a donationItem', async () => { | ||
| const donationItemData = mockDonationItemsCreateData[0]; | ||
| mockDonationItemsService.create.mockResolvedValue( | ||
| donationItemData as DonationItem, | ||
| ); | ||
| const result = await controller.createDonationItem( | ||
| donationItemData as DonationItem, | ||
| ); | ||
| expect(result).toEqual(donationItemData as DonationItem); | ||
| expect(mockDonationItemsService.create).toHaveBeenCalledWith( | ||
| donationItemData.donationId, | ||
| donationItemData.itemName, | ||
| donationItemData.quantity, | ||
| donationItemData.reservedQuantity, | ||
| donationItemData.ozPerItem, | ||
| donationItemData.estimatedValue, | ||
| donationItemData.foodType, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createMultipleDonationItems', () => { | ||
| it('should call donationItemsService.createMultipleDonationItems with donationId and items, and return the created donation items', async () => { | ||
| const mockBody: CreateMultipleDonationItemsDto = { | ||
| donationId: 1, | ||
| items: [ | ||
| { | ||
| itemName: 'Rice Noodles', | ||
| quantity: 100, | ||
| reservedQuantity: 0, | ||
| ozPerItem: 5, | ||
| estimatedValue: 100, | ||
| foodType: FoodType.DAIRY_FREE_ALTERNATIVES, | ||
| }, | ||
| { | ||
| itemName: 'Beans', | ||
| quantity: 50, | ||
| reservedQuantity: 0, | ||
| ozPerItem: 10, | ||
| estimatedValue: 80, | ||
| foodType: FoodType.GLUTEN_FREE_BAKING_PANCAKE_MIXES, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const mockCreatedItems: Partial<DonationItem>[] = [ | ||
| { itemId: 1, donationId: 1, ...mockBody.items[0] }, | ||
| { itemId: 2, donationId: 1, ...mockBody.items[1] }, | ||
| ]; | ||
|
|
||
| mockDonationItemsService.createMultipleDonationItems.mockResolvedValue( | ||
| mockCreatedItems as DonationItem[], | ||
| ); | ||
|
|
||
| const result = await controller.createMultipleDonationItems(mockBody); | ||
|
|
||
| expect( | ||
| mockDonationItemsService.createMultipleDonationItems, | ||
| ).toHaveBeenCalledWith(mockBody.donationId, mockBody.items); | ||
| expect(result).toEqual(mockCreatedItems); | ||
| }); | ||
| }); | ||
| }); | ||
|
Collaborator
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. since create-multiple takes care of 1+ donation items, when would the create endpoint be used? i see the endpoint associated with it isn't called from the frontend (postDonationItem). was there a plan to delete it later?
Author
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. This is a really good point. I personally cannot think of a case as of right now where we would want to just make one donation item, since food manufacturers are the only ones donating food. This could likely be deleted later on, but I think just in case there may be value to keeping it. @sam-schu thoughts? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { | ||
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| IsNumber, | ||
| IsString, | ||
| IsArray, | ||
| ValidateNested, | ||
| Min, | ||
| IsEnum, | ||
| IsNotEmpty, | ||
| Length, | ||
| } from 'class-validator'; | ||
| import { Type } from 'class-transformer'; | ||
| import { FoodType } from '../types'; | ||
|
|
||
| export class CreateDonationItemDto { | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @Length(1, 255) | ||
| itemName: string; | ||
|
|
||
| @IsNumber() | ||
| @Min(1) | ||
| quantity: number; | ||
|
|
||
| @IsNumber() | ||
| @Min(0) | ||
| reservedQuantity: number; | ||
|
|
||
| @IsNumber() | ||
| @Min(1) | ||
| ozPerItem: number; | ||
|
|
||
| @IsNumber() | ||
| @Min(1) | ||
| estimatedValue: number; | ||
|
|
||
| @IsEnum(FoodType) | ||
| foodType: FoodType; | ||
| } | ||
|
|
||
| export class CreateMultipleDonationItemsDto { | ||
| @IsNumber() | ||
| donationId: number; | ||
|
|
||
| @IsArray() | ||
| @ValidateNested({ each: true }) | ||
| @Type(() => CreateDonationItemDto) | ||
| items: CreateDonationItemDto[]; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.