-
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.
- Loading branch information
1 parent
d94ef3b
commit 863d5f0
Showing
1 changed file
with
66 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,66 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { HttpException, HttpStatus } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { ResourceService } from 'src/resource/resource.service'; | ||
import { FEEDBACK_TAGS_ENUM } from 'src/utils/constants'; | ||
import { mockResource } from 'test/utils/mockData'; | ||
import { Repository } from 'typeorm'; | ||
import { ResourceFeedbackEntity } from '../entities/resource-feedback.entity'; | ||
import { ResourceFeedbackService } from './resource-feedback.service'; | ||
|
||
describe('ResourceFeedbackService', () => { | ||
let service: ResourceFeedbackService; | ||
let mockResourceFeedbackRepository: DeepMocked<Repository<ResourceFeedbackEntity>>; | ||
let mockResourceService: DeepMocked<ResourceService>; | ||
|
||
const resourceFeedbackDto = { | ||
resourceId: mockResource.id, | ||
feedbackTags: FEEDBACK_TAGS_ENUM.RELATABLE, | ||
feedbackDescription: 'feedback comments', | ||
} as ResourceFeedbackEntity; | ||
|
||
beforeEach(async () => { | ||
mockResourceFeedbackRepository = createMock<Repository<ResourceFeedbackEntity>>(); | ||
mockResourceService = createMock<ResourceService>(); | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ResourceFeedbackService, | ||
{ | ||
provide: getRepositoryToken(ResourceFeedbackEntity), | ||
useValue: mockResourceFeedbackRepository, | ||
}, | ||
{ | ||
provide: ResourceService, | ||
useValue: mockResourceService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<ResourceFeedbackService>(ResourceFeedbackService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should create a resource feedback when resource exists', async () => { | ||
jest.spyOn(mockResourceService, 'findOne').mockResolvedValueOnce(mockResource); | ||
jest.spyOn(mockResourceFeedbackRepository, 'save').mockResolvedValueOnce(resourceFeedbackDto); | ||
|
||
const result = await service.create(resourceFeedbackDto); | ||
expect(result).toEqual(resourceFeedbackDto); | ||
expect(mockResourceFeedbackRepository.save).toHaveBeenCalledWith(resourceFeedbackDto); | ||
}); | ||
|
||
it('should throw an HttpException when resource does not exist', async () => { | ||
jest.spyOn(mockResourceService, 'findOne').mockResolvedValueOnce(null); | ||
|
||
await expect(service.create(resourceFeedbackDto)).rejects.toThrow( | ||
new HttpException('RESOURCE NOT FOUND', HttpStatus.NOT_FOUND), | ||
); | ||
}); | ||
}); | ||
}); |