Skip to content

Commit

Permalink
add resource feedback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes committed Dec 3, 2024
1 parent d94ef3b commit 863d5f0
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/resource-feedback/resource-feedback.service.spec.ts
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),
);
});
});
});

0 comments on commit 863d5f0

Please sign in to comment.