From 5caf16e3a783280cf28bfcbe86f68802d0e82bc1 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Thu, 14 Dec 2023 17:26:34 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20Test=20:=20report.service.spec.ts=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/report/report.service.spec.ts | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 BE/src/report/report.service.spec.ts diff --git a/BE/src/report/report.service.spec.ts b/BE/src/report/report.service.spec.ts new file mode 100644 index 0000000..4b0e053 --- /dev/null +++ b/BE/src/report/report.service.spec.ts @@ -0,0 +1,83 @@ +import { Repository } from 'typeorm'; +import { ReportService } from './report.service'; +import { ReportEntity } from '../entities/report.entity'; +import { PostEntity } from '../entities/post.entity'; +import { Test, TestingModule } from '@nestjs/testing'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { CreateReportDto } from './dto/createReport.dto'; +import { HttpException } from '@nestjs/common'; + +const mockReportRepository = () => ({ + save: jest.fn(), + find: jest.fn(), + findOne: jest.fn(), + softDelete: jest.fn(), +}); + +const mockPostRepository = () => ({ + findOne: jest.fn(), + exist: jest.fn(), +}); + +type MockRepository = Partial, jest.Mock>>; + +describe('ReportService', function () { + let service: ReportService; + let reportRepository: MockRepository; + let postRepository: MockRepository; + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + ReportService, + { + provide: getRepositoryToken(ReportEntity), + useValue: mockReportRepository(), + }, + { + provide: getRepositoryToken(PostEntity), + useValue: mockPostRepository(), + }, + ], + }).compile(); + + service = module.get(ReportService); + reportRepository = module.get>( + getRepositoryToken(ReportEntity), + ); + postRepository = module.get>( + getRepositoryToken(PostEntity), + ); + }); + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('createReport()', function () { + const body = new CreateReportDto(); + body.post_id = 123; + body.user_id = 'user'; + body.description = 'test'; + it('should bad request', function () { + expect(async () => { + await service.createReport(body, 'user'); + }).rejects.toThrowError( + new HttpException('자신의 게시글은 신고 할 수 없습니다.', 400), + ); + }); + + it('should not found', function () { + postRepository.exist.mockResolvedValue(false); + expect(async () => { + await service.createReport(body, 'user1'); + }).rejects.toThrowError( + new HttpException('신고할 대상이 존재 하지 않습니다.', 404), + ); + }); + + it('should save', async function () { + postRepository.exist.mockResolvedValue(true); + await service.createReport(body, 'user1'); + expect(reportRepository.save).toHaveBeenCalledTimes(1); + }); + }); +});