-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add findAll(GET) method in the campaign-application.service(second tr…
…y) (#652) * add create campaign agreements validation if any of the 3 agreements for creating campaign are not checked -> throw a HttpException with status 405 * fix http error status code in campaign-application.service http error code is now 400 in validation for all 3 agreements in create method * fix throw new BadRequestException changed the error from HttpException to BadRequestException for the agreements validation in create method in campaign-application.service.ts * injected prisma service * add findAll camapings method created findAll functionality in campaign-application.service and add testing for the method * reverted commit * fix test for findAll (campaign-applications) method new the mocked campaign-applications in the tests have the right schema(schema of the campaign-application) * working on "cleaning my branches" * Revert "fix test for findAll (campaign-applications) method" This reverts commit cb14fdf. * add findAll(GET) method in the campaign-application.service * fix campaign-application controller and services remove unused providers in campaign-application.spec.ts add validation if user is admin in findAll method in campaign-application.controller.ts add tests for findAll method * remove commented code
- Loading branch information
Showing
5 changed files
with
127 additions
and
14 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
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
3 changes: 2 additions & 1 deletion
3
apps/api/src/campaign-application/campaign-application.module.ts
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 |
---|---|---|
@@ -1,14 +1,26 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { CampaignApplicationService } from './campaign-application.service' | ||
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' | ||
import { BadRequestException, HttpStatus } from '@nestjs/common' | ||
import { BadRequestException } from '@nestjs/common' | ||
import { CampaignApplicationState, CampaignTypeCategory } from '@prisma/client' | ||
import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' | ||
import { EmailService } from '../email/email.service' | ||
|
||
describe('CampaignApplicationService', () => { | ||
let service: CampaignApplicationService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CampaignApplicationService], | ||
providers: [ | ||
CampaignApplicationService, | ||
MockPrismaService, | ||
{ | ||
provide: EmailService, | ||
useValue: { | ||
sendFromTemplate: jest.fn(() => true), | ||
}, | ||
}, | ||
], | ||
}).compile() | ||
|
||
service = module.get<CampaignApplicationService>(CampaignApplicationService) | ||
|
@@ -28,9 +40,10 @@ describe('CampaignApplicationService', () => { | |
organizerBeneficiaryRel: 'Test Relation', | ||
goal: 'Test Goal', | ||
amount: '1000', | ||
toEntity: jest.fn(), // Mock implementation | ||
toEntity: jest.fn(), | ||
} | ||
it('should throw an error if acceptTermsAndConditions are not accepted', () => { | ||
|
||
it('should throw an error if acceptTermsAndConditions is false', () => { | ||
const dto: CreateCampaignApplicationDto = { | ||
...baseDto, | ||
acceptTermsAndConditions: false, | ||
|
@@ -43,7 +56,7 @@ describe('CampaignApplicationService', () => { | |
) | ||
}) | ||
|
||
it('should throw an error if transparencyTermsAccepted are not accepted', () => { | ||
it('should throw an error if transparencyTermsAccepted is false', () => { | ||
const dto: CreateCampaignApplicationDto = { | ||
...baseDto, | ||
acceptTermsAndConditions: true, | ||
|
@@ -56,7 +69,7 @@ describe('CampaignApplicationService', () => { | |
) | ||
}) | ||
|
||
it('should throw an error if personalInformationProcessingAccepted is not accepted', () => { | ||
it('should throw an error if personalInformationProcessingAccepted is false', () => { | ||
const dto: CreateCampaignApplicationDto = { | ||
...baseDto, | ||
acceptTermsAndConditions: true, | ||
|
@@ -69,7 +82,7 @@ describe('CampaignApplicationService', () => { | |
) | ||
}) | ||
|
||
it('should add a new campaign application if all agreements are accepted', () => { | ||
it('should add a new campaign-application if all agreements are true', () => { | ||
const dto: CreateCampaignApplicationDto = { | ||
...baseDto, | ||
acceptTermsAndConditions: true, | ||
|
@@ -80,4 +93,81 @@ describe('CampaignApplicationService', () => { | |
expect(service.create(dto)).toBe('This action adds a new campaignApplication') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('findAll', () => { | ||
it('should return an array of campaign-applications', async () => { | ||
const mockCampaigns = [ | ||
{ | ||
id: 'testId', | ||
createdAt: new Date('2022-04-08T06:36:33.661Z'), | ||
updatedAt: new Date('2022-04-08T06:36:33.662Z'), | ||
description: 'Test description', | ||
organizerId: 'testOrganizerId1', | ||
organizerName: 'Test Organizer1', | ||
organizerEmail: '[email protected]', | ||
beneficiary: 'test beneficary', | ||
organizerPhone: '123456789', | ||
organizerBeneficiaryRel: 'Test Relation', | ||
campaignName: 'Test Campaign', | ||
goal: 'Test Goal', | ||
history: 'test history', | ||
amount: '1000', | ||
campaignGuarantee: 'test campaignGuarantee', | ||
otherFinanceSources: 'test otherFinanceSources', | ||
otherNotes: 'test otherNotes', | ||
state: CampaignApplicationState.review, | ||
category: CampaignTypeCategory.medical, | ||
ticketURL: 'testsodifhso', | ||
archived: false, | ||
}, | ||
{ | ||
id: 'testId2', | ||
createdAt: new Date('2022-04-08T06:36:33.661Z'), | ||
updatedAt: new Date('2022-04-08T06:36:33.662Z'), | ||
description: 'Test description', | ||
organizerId: 'testOrganizerId2', | ||
organizerName: 'Test Organizer2', | ||
organizerEmail: '[email protected]', | ||
beneficiary: 'test beneficary2', | ||
organizerPhone: '123456789', | ||
organizerBeneficiaryRel: 'Test Relation2', | ||
campaignName: 'Test Campaign2', | ||
goal: 'Test Goal2', | ||
history: 'test history2', | ||
amount: '2000', | ||
campaignGuarantee: 'test campaignGuarantee2', | ||
otherFinanceSources: 'test otherFinanceSources2', | ||
otherNotes: 'test otherNotes2', | ||
state: CampaignApplicationState.review, | ||
category: CampaignTypeCategory.medical, | ||
ticketURL: 'testsodifhso2', | ||
archived: false, | ||
}, | ||
] | ||
|
||
prismaMock.campaignApplication.findMany.mockResolvedValue(mockCampaigns) | ||
|
||
const result = await service.findAll() | ||
|
||
expect(result).toEqual(mockCampaigns) | ||
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should return an empty array if no campaign-applications are found', async () => { | ||
prismaMock.campaignApplication.findMany.mockResolvedValue([]) | ||
|
||
const result = await service.findAll() | ||
|
||
expect(result).toEqual([]) | ||
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should handle errors and throw an exception', async () => { | ||
const errorMessage = 'error' | ||
prismaMock.campaignApplication.findMany.mockRejectedValue(new Error(errorMessage)) | ||
|
||
await expect(service.findAll()).rejects.toThrow(errorMessage) | ||
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
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