|
| 1 | +import { mock } from 'jest-mock-extended'; |
| 2 | +import { FoodManufacturersService } from './manufacturers.service'; |
| 3 | +import { FoodManufacturersController } from './manufacturers.controller'; |
| 4 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 5 | +import { FoodManufacturer } from './manufacturers.entity'; |
| 6 | +import { Allergen, DonateWastedFood, ManufacturerStatus } from './types'; |
| 7 | +import { FoodManufacturerApplicationDto } from './dtos/manufacturer-application.dto'; |
| 8 | + |
| 9 | +const mockManufacturersService = mock<FoodManufacturersService>(); |
| 10 | + |
| 11 | +const mockManufacturer1: Partial<FoodManufacturer> = { |
| 12 | + foodManufacturerId: 1, |
| 13 | + foodManufacturerName: 'Good Foods Inc', |
| 14 | + status: ManufacturerStatus.PENDING, |
| 15 | +}; |
| 16 | + |
| 17 | +const mockManufacturer2: Partial<FoodManufacturer> = { |
| 18 | + foodManufacturerId: 2, |
| 19 | + foodManufacturerName: 'Healthy Eats LLC', |
| 20 | + status: ManufacturerStatus.PENDING, |
| 21 | +}; |
| 22 | + |
| 23 | +describe('FoodManufacturersController', () => { |
| 24 | + let controller: FoodManufacturersController; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + |
| 29 | + const module: TestingModule = await Test.createTestingModule({ |
| 30 | + controllers: [FoodManufacturersController], |
| 31 | + providers: [ |
| 32 | + { |
| 33 | + provide: FoodManufacturersService, |
| 34 | + useValue: mockManufacturersService, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }).compile(); |
| 38 | + |
| 39 | + controller = module.get<FoodManufacturersController>( |
| 40 | + FoodManufacturersController, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should be defined', () => { |
| 45 | + expect(controller).toBeDefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('GET /pending', () => { |
| 49 | + it('should return pending food manufacturers', async () => { |
| 50 | + const mockManufacturers: Partial<FoodManufacturer>[] = [ |
| 51 | + mockManufacturer1, |
| 52 | + mockManufacturer2, |
| 53 | + ]; |
| 54 | + |
| 55 | + mockManufacturersService.getPendingManufacturers.mockResolvedValue( |
| 56 | + mockManufacturers as FoodManufacturer[], |
| 57 | + ); |
| 58 | + |
| 59 | + const result = await controller.getPendingManufacturers(); |
| 60 | + |
| 61 | + expect(result).toEqual(mockManufacturers); |
| 62 | + expect(result).toHaveLength(2); |
| 63 | + expect(result[0].foodManufacturerId).toBe(1); |
| 64 | + expect(result[1].foodManufacturerId).toBe(2); |
| 65 | + expect( |
| 66 | + mockManufacturersService.getPendingManufacturers, |
| 67 | + ).toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('GET /:id', () => { |
| 72 | + it('should return a food manufacturer by id', async () => { |
| 73 | + mockManufacturersService.findOne.mockResolvedValue( |
| 74 | + mockManufacturer1 as FoodManufacturer, |
| 75 | + ); |
| 76 | + |
| 77 | + const result = await controller.getFoodManufacturer(1); |
| 78 | + |
| 79 | + expect(result).toEqual(mockManufacturer1); |
| 80 | + expect(mockManufacturersService.findOne).toHaveBeenCalledWith(1); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('POST /api/manufacturers', () => { |
| 85 | + it('should submit a food manufacturer application', async () => { |
| 86 | + const mockApplicationData: FoodManufacturerApplicationDto = { |
| 87 | + foodManufacturerName: 'Good Foods Inc', |
| 88 | + foodManufacturerWebsite: 'https://goodfoods.example.com', |
| 89 | + contactFirstName: 'Alice', |
| 90 | + contactLastName: 'Johnson', |
| 91 | + contactEmail: 'alice.johnson@goodfoods.example.com', |
| 92 | + contactPhone: '555-123-4567', |
| 93 | + unlistedProductAllergens: [Allergen.EGG], |
| 94 | + facilityFreeAllergens: [Allergen.EGG], |
| 95 | + productsGlutenFree: true, |
| 96 | + productsContainSulfites: false, |
| 97 | + productsSustainableExplanation: 'We use eco-friendly packaging.', |
| 98 | + inKindDonations: true, |
| 99 | + donateWastedFood: DonateWastedFood.SOMETIMES, |
| 100 | + }; |
| 101 | + |
| 102 | + mockManufacturersService.addFoodManufacturer.mockResolvedValue(); |
| 103 | + |
| 104 | + await controller.submitFoodManufacturerApplication(mockApplicationData); |
| 105 | + |
| 106 | + expect(mockManufacturersService.addFoodManufacturer).toHaveBeenCalledWith( |
| 107 | + mockApplicationData, |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('POST /approve/:id', () => { |
| 113 | + it('should approve a food manufacturer', async () => { |
| 114 | + mockManufacturersService.approve.mockResolvedValue(); |
| 115 | + |
| 116 | + await controller.approveManufacturer(1); |
| 117 | + |
| 118 | + expect(mockManufacturersService.approve).toHaveBeenCalledWith(1); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('POST /deny/:id', () => { |
| 123 | + it('should deny a food manufacturer', async () => { |
| 124 | + mockManufacturersService.deny.mockResolvedValue(); |
| 125 | + |
| 126 | + await controller.denyManufacturer(1); |
| 127 | + |
| 128 | + expect(mockManufacturersService.deny).toHaveBeenCalledWith(1); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments