-
Notifications
You must be signed in to change notification settings - Fork 46
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
add findAll(GET) method in the campaign-application.service(second try) #652
Changes from 12 commits
8f0525a
77c9df8
139a6a9
15ddce4
7d43335
1280e2b
65dc547
cb14fdf
3fdbb75
6199f1d
bc0dd85
a6d35fb
480d492
e5488d3
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 |
---|---|---|
|
@@ -2,14 +2,57 @@ 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 { | ||
CampaignApplicationState, | ||
CampaignState, | ||
CampaignTypeCategory, | ||
Currency, | ||
} from '@prisma/client' | ||
import { CreateCampaignDto } from '../campaign/dto/create-campaign.dto' | ||
import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' | ||
import { CampaignService } from '../campaign/campaign.service' | ||
import { NotificationService } from '../sockets/notifications/notification.service' | ||
import { VaultService } from '../vault/vault.service' | ||
import { ConfigService } from '@nestjs/config' | ||
import { MarketingNotificationsService } from '../notifications/notifications.service' | ||
import { PersonService } from '../person/person.service' | ||
import { EmailService } from '../email/email.service' | ||
import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers' | ||
import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' | ||
import { NotificationGateway } from '../sockets/notifications/gateway' | ||
import { TemplateService } from '../email/template.service' | ||
describe('CampaignApplicationService', () => { | ||
let service: CampaignApplicationService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CampaignApplicationService], | ||
}).compile() | ||
providers: [ | ||
{ | ||
// Use the interface as token | ||
provide: NotificationsProviderInterface, | ||
// But actually provide the service that implements the interface | ||
useClass: SendGridNotificationsProvider, | ||
}, | ||
CampaignService, | ||
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. Do we need all these providers? CampaignService? Vault Service.... and all the rest? 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. Keep in mind that all of these would need to be instantiated with all of their dependencies for each test!!! Instead use mocking (see other tests or look for autoSpy usage) |
||
MockPrismaService, | ||
NotificationService, | ||
VaultService, | ||
MarketingNotificationsService, | ||
ConfigService, | ||
PersonService, | ||
EmailService, | ||
NotificationGateway, | ||
TemplateService, | ||
CampaignApplicationService, | ||
], | ||
}) | ||
.overrideProvider(EmailService) | ||
.useValue({ | ||
sendFromTemplate: jest.fn(() => { | ||
return true | ||
}), | ||
}) | ||
.compile() | ||
|
||
service = module.get<CampaignApplicationService>(CampaignApplicationService) | ||
}) | ||
|
@@ -28,7 +71,7 @@ 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', () => { | ||
const dto: CreateCampaignApplicationDto = { | ||
|
@@ -80,4 +123,81 @@ describe('CampaignApplicationService', () => { | |
expect(service.create(dto)).toBe('This action adds a new campaignApplication') | ||
}) | ||
}) | ||
|
||
describe('find all(GET) campains', () => { | ||
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: '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, | ||
}, | ||
] | ||
|
||
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 campaigns 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 = 'Database error' | ||
prismaMock.campaignApplication.findMany.mockRejectedValue(new Error(errorMessage)) | ||
|
||
await expect(service.findAll()).rejects.toThrow(errorMessage) | ||
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common' | ||
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' | ||
import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' | ||
import { PrismaService } from '../prisma/prisma.service' | ||
|
||
@Injectable() | ||
export class CampaignApplicationService { | ||
constructor(private prisma: PrismaService) {} | ||
async getCampaignByIdWithPersonIds(id: string): Promise<UpdateCampaignApplicationDto> { | ||
throw new Error('Method not implemented.') | ||
} | ||
|
@@ -20,7 +22,7 @@ export class CampaignApplicationService { | |
} | ||
|
||
findAll() { | ||
return `This action returns all campaignApplication` | ||
return this.prisma.campaignApplication.findMany() | ||
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. The controller would need to be updated to check for authenticated user and allow getting only for admin/operator roles. |
||
} | ||
|
||
findOne(id: string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove commented out code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I have missed it.