-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CROW-27] Add claim event listener (#27)
- Loading branch information
1 parent
8af0e82
commit 1447ec9
Showing
17 changed files
with
531 additions
and
40 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
68 changes: 68 additions & 0 deletions
68
src/features/campaigns/repositories/mongo/campaign-claim/campaign-claim.repository.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { getModelToken } from '@nestjs/mongoose'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { Model, ObjectId } from 'mongoose'; | ||
|
||
import { CampaignClaim } from 'src/features/campaigns/schemas/campaign-claim.schema'; | ||
import { campaignClaimMock } from 'src/features/campaigns/tests/mocks'; | ||
import { CampaignClaimMongoRepository } from './campaign-claim.repository'; | ||
|
||
describe('Campaign Claim Mongo Repository', () => { | ||
let campaignClaimRepository: CampaignClaimMongoRepository; | ||
let campaignClaimModel: Model<CampaignClaim>; | ||
|
||
const campaignId = '634f3292a486274ca2f3d47f' as unknown as ObjectId; | ||
const userId = '634f3292a486274ca2f3d47a' as unknown as ObjectId; | ||
const tokenId = '634f3292a486274ca2f3d47b' as unknown as ObjectId; | ||
const amount = '1'; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CampaignClaimMongoRepository, | ||
{ | ||
provide: getModelToken(CampaignClaim.name), | ||
useValue: { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
sort: jest.fn(), | ||
skip: jest.fn(), | ||
limit: jest.fn(), | ||
lean: jest.fn(), | ||
create: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
campaignClaimRepository = module.get<CampaignClaimMongoRepository>( | ||
CampaignClaimMongoRepository, | ||
); | ||
campaignClaimModel = module.get<Model<CampaignClaim>>( | ||
getModelToken(CampaignClaim.name), | ||
); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(campaignClaimRepository).toBeDefined(); | ||
}); | ||
|
||
describe('create method', () => { | ||
it('should call create method without errors', async () => { | ||
jest | ||
.spyOn(campaignClaimModel, 'create') | ||
.mockReturnValue(campaignClaimMock as any); | ||
|
||
const response = await campaignClaimRepository.create({ | ||
campaignId, | ||
userId, | ||
tokenId, | ||
amount, | ||
}); | ||
|
||
expect(response).toStrictEqual(campaignClaimMock); | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
src/features/campaigns/repositories/mongo/campaign-claim/campaign-claim.repository.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model, ObjectId } from 'mongoose'; | ||
|
||
import { | ||
CampaignClaim, | ||
CampaignClaimDocument, | ||
} from '../../../schemas/campaign-claim.schema'; | ||
|
||
@Injectable() | ||
export class CampaignClaimMongoRepository { | ||
constructor( | ||
@InjectModel(CampaignClaim.name) | ||
private campaignClaimModel: Model<CampaignClaimDocument>, | ||
) {} | ||
|
||
// TODO: possible to use an abstract class to define the interface? | ||
async create({ | ||
campaignId, | ||
userId, | ||
tokenId, | ||
amount, | ||
}: { | ||
userId: ObjectId; | ||
campaignId: ObjectId; | ||
tokenId: ObjectId; | ||
amount: string; | ||
}) { | ||
return await this.campaignClaimModel.create({ | ||
campaign: campaignId, | ||
user: userId, | ||
token: tokenId, | ||
amount, | ||
date: new Date(), | ||
}); | ||
} | ||
} |
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
160 changes: 160 additions & 0 deletions
160
src/features/campaigns/services/campaign-claim/campaign-claim.service.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { | ||
campaignClaimArgumentMock, | ||
campaignClaimMock, | ||
campaignPledgeArgumentMock, | ||
mongoClaimedCampaingStatus, | ||
tokenMock, | ||
userMock, | ||
} from '../../tests/mocks'; | ||
import { campaignMock } from 'src/features/users/tests/mocks'; | ||
import { CampaignsService } from 'src/features/campaigns/services/campaigns.service'; | ||
import { TokensService } from 'src/features/tokens/services/tokens.service'; | ||
import { UsersService } from 'src/features/users/services/users.service'; | ||
import { CampaignStatusService } from 'src/features/campaign-statuses/services/campaign-statuses.service'; | ||
import { CampaignClaimService } from 'src/features/campaigns/services/campaign-claim/campaign-claim.service'; | ||
import { CampaignsMongoRepository } from 'src/features/campaigns/repositories/mongo/campaigns.repository'; | ||
import { CampaignClaimMongoRepository } from 'src/features/campaigns/repositories/mongo/campaign-claim/campaign-claim.repository'; | ||
import { UserCampaignsRepository } from 'src/features/users/repositories/user-campaigns/mongo/user-campaigns.repository'; | ||
|
||
describe('CampaignClaimService', () => { | ||
let campaignClaimService: CampaignClaimService; | ||
let campaignService: CampaignsService; | ||
let usersService: UsersService; | ||
let tokensService: TokensService; | ||
let campaignStatusService: CampaignStatusService; | ||
let campaignClaimMongoRepository: CampaignClaimMongoRepository; | ||
let campaignMongoRepository: CampaignsMongoRepository; | ||
let userCampaignsMongoRepository: UserCampaignsRepository; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CampaignClaimService, | ||
{ | ||
provide: CampaignsService, | ||
useValue: { | ||
findOne: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: CampaignStatusService, | ||
useValue: { | ||
getStatusByCode: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: UsersService, | ||
useValue: { | ||
findUserByAddress: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: TokensService, | ||
useValue: { | ||
getByDefault: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: CampaignClaimMongoRepository, | ||
useValue: { | ||
create: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: CampaignsMongoRepository, | ||
useValue: { | ||
update: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: UserCampaignsRepository, | ||
useValue: { | ||
updateUserCampaignByEvent: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
campaignClaimService = | ||
module.get<CampaignClaimService>(CampaignClaimService); | ||
campaignService = module.get<CampaignsService>(CampaignsService); | ||
usersService = module.get<UsersService>(UsersService); | ||
tokensService = module.get<TokensService>(TokensService); | ||
campaignStatusService = module.get<CampaignStatusService>( | ||
CampaignStatusService, | ||
); | ||
campaignClaimMongoRepository = module.get<CampaignClaimMongoRepository>( | ||
CampaignClaimMongoRepository, | ||
); | ||
campaignMongoRepository = module.get<CampaignsMongoRepository>( | ||
CampaignsMongoRepository, | ||
); | ||
userCampaignsMongoRepository = module.get<UserCampaignsRepository>( | ||
UserCampaignsRepository, | ||
); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(campaignClaimService).toBeDefined(); | ||
}); | ||
|
||
describe('create method', () => { | ||
it('should call create method and fails because arguments are not right', async () => { | ||
await expect(() => | ||
campaignClaimService.create('WrongArgument'), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('should call create method and fails because there is no associated campaign', async () => { | ||
jest.spyOn(campaignService, 'findOne').mockResolvedValue(null as any); | ||
jest | ||
.spyOn(usersService, 'findUserByAddress') | ||
.mockResolvedValue(userMock as any); | ||
jest | ||
.spyOn(tokensService, 'getByDefault') | ||
.mockResolvedValue(tokenMock as any); | ||
|
||
await expect(() => | ||
campaignClaimService.create(campaignClaimArgumentMock), | ||
).rejects.toThrow(); | ||
|
||
expect(campaignService.findOne).toBeCalledTimes(1); | ||
expect(usersService.findUserByAddress).toBeCalledTimes(1); | ||
expect(tokensService.getByDefault).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should call create method without errors', async () => { | ||
jest | ||
.spyOn(campaignService, 'findOne') | ||
.mockResolvedValue({ campaign: campaignMock } as any); | ||
jest | ||
.spyOn(usersService, 'findUserByAddress') | ||
.mockResolvedValue(userMock as any); | ||
jest | ||
.spyOn(tokensService, 'getByDefault') | ||
.mockResolvedValue(tokenMock as any); | ||
jest | ||
.spyOn(campaignClaimMongoRepository, 'create') | ||
.mockResolvedValue(campaignClaimMock as any); | ||
jest | ||
.spyOn(campaignStatusService, 'getStatusByCode') | ||
.mockResolvedValue(mongoClaimedCampaingStatus as any); | ||
jest.spyOn(campaignMongoRepository, 'update').mockResolvedValue(null); | ||
jest | ||
.spyOn(userCampaignsMongoRepository, 'updateUserCampaignByEvent') | ||
.mockResolvedValue(null); | ||
|
||
await expect(() => | ||
campaignClaimService.create(campaignPledgeArgumentMock), | ||
).not.toThrow(); | ||
|
||
expect(campaignService.findOne).toBeCalledTimes(1); | ||
expect(usersService.findUserByAddress).toBeCalledTimes(1); | ||
expect(tokensService.getByDefault).toBeCalledTimes(1); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.