-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for createPartner method
- Loading branch information
1 parent
d602da1
commit b9ef6c6
Showing
1 changed file
with
62 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { PartnerEntity } from '../entities/partner.entity'; | ||
import { PartnerService } from './partner.service'; | ||
import { Repository } from 'typeorm'; | ||
import { PartnerAccessEntity } from '../entities/partner-access.entity'; | ||
import { PartnerAdminEntity } from '../entities/partner-admin.entity'; | ||
import { UserEntity } from '../entities/user.entity'; | ||
import { mockPartnerRepositoryMethods } from '../../test/utils/mockedServices'; | ||
import { mockPartnerEntity } from '../../test/utils/mockData'; | ||
|
||
const createPartnerDto = { | ||
name: mockPartnerEntity.name, | ||
}; | ||
describe('PartnerService', () => { | ||
let service: PartnerService; | ||
let mockPartnerRepository: DeepMocked<Repository<PartnerEntity>>; | ||
let mockPartnerAccessRepository: DeepMocked<Repository<PartnerAccessEntity>>; | ||
let mockPartnerAdminRepository: DeepMocked<Repository<PartnerAdminEntity>>; | ||
let mockUserRepository: DeepMocked<Repository<UserEntity>>; | ||
|
||
beforeEach(async () => { | ||
mockPartnerRepository = createMock<Repository<PartnerEntity>>( | ||
mockPartnerRepositoryMethods, | ||
); | ||
mockPartnerAccessRepository = createMock<Repository<PartnerAccessEntity>>(); | ||
mockPartnerAdminRepository = createMock<Repository<PartnerAdminEntity>>(); | ||
mockUserRepository = createMock<Repository<UserEntity>>(); | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
PartnerService, | ||
{ provide: getRepositoryToken(PartnerEntity), useValue: mockPartnerRepository }, | ||
{ provide: getRepositoryToken(PartnerAccessEntity), useValue: mockPartnerAccessRepository }, | ||
{ provide: getRepositoryToken(PartnerAdminEntity), useValue: mockPartnerAdminRepository }, | ||
{ provide: getRepositoryToken(UserEntity), useValue: mockUserRepository }, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<PartnerService>(PartnerService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('createPartner', () => { | ||
it('when supplied with correct data should return new partner', async () => { | ||
const response = await service.createPartner(createPartnerDto); | ||
expect(response).toMatchObject(createPartnerDto); | ||
}) | ||
it('when supplied with a name that already exists, it should throw error', async () => { | ||
jest | ||
.spyOn(mockPartnerRepository, 'create') | ||
.mockImplementationOnce(() => { | ||
throw ({ code: '23505' }); | ||
}); | ||
await expect(service.createPartner(createPartnerDto)).rejects.toThrow(); | ||
}) | ||
}); | ||
}); |