diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 833d08c..955a7cd 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -16,7 +16,6 @@ export const jwtSecret = process.env.JWT_SECRET; imports: [PrismaModule, PassportModule, UsersModule, JwtModule.register({})], controllers: [AuthController], providers: [ - AuthService, AuthService, UsersService, JwtAccessStrategy, diff --git a/src/data/data.controller.spec.ts b/src/data/data.controller.spec.ts index 74839d2..3d2dd71 100644 --- a/src/data/data.controller.spec.ts +++ b/src/data/data.controller.spec.ts @@ -1,20 +1,62 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { DataController } from './data.controller'; import { DataService } from './data.service'; +import { DataController } from './data.controller'; +import { PrismaModule } from 'src/prisma/prisma.module'; -describe('DataController', () => { +describe('CityController', () => { let controller: DataController; + let dataService: DataService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [DataController], providers: [DataService], + imports: [PrismaModule] }).compile(); controller = module.get(DataController); + dataService = module.get(DataService); }); - it('should be defined', () => { - expect(controller).toBeDefined(); + describe('cityData', () => { + it('should return city data for a valid language', async () => { + const mockQuery = { lang: 'en' }; + + const mockCityData = { + lang: 'en', + items: [ + { doName: 'City / Province'}, + { doName: 'Seoul' }, + { doName: 'Busan' }, + { doName: 'Daegu' }, + { doName: 'Incheon' }, + { doName: 'Gwangju' }, + { doName: 'Daejeon' }, + { doName: 'Ulsan' }, + { doName: 'Sejong' }, + { doName: 'Gyeonggi-do' }, + { doName: 'Gangwon-do' }, + { doName: 'Chungcheongbuk-do' }, + { doName: 'Chungcheongnam-do' }, + { doName: 'Jeollabuk-do' }, + { doName: 'Jeollanam-do' }, + { doName: 'Gyeongsangbuk-do' }, + { doName: 'Gyeongsangnam-do' }, + { doName: 'Jeju-do' }, + ], + } + + const result = await controller.cityData(mockQuery); + + expect(result).toEqual(mockCityData); + }); + + it('should return an error message for an invalid language', async () => { + const mockQuery = { lang: 'fr' }; + + const result = await controller.cityData(mockQuery); + + expect(result).toEqual({ message: '[ko, en, jp] 중 하나를 입력하세요' }); + }); }); }); diff --git a/src/data/data.service.spec.ts b/src/data/data.service.spec.ts index a414789..98eab20 100644 --- a/src/data/data.service.spec.ts +++ b/src/data/data.service.spec.ts @@ -1,18 +1,25 @@ -import { Test, TestingModule } from '@nestjs/testing'; import { DataService } from './data.service'; describe('DataService', () => { - let service: DataService; + let dataService; + let mockPrisma; beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [DataService], - }).compile(); - - service = module.get(DataService); + mockPrisma = { + region: { + findMany: jest.fn(), + }, + }; + dataService = new DataService(mockPrisma); }); - it('should be defined', () => { - expect(service).toBeDefined(); + test('guNameData Method', async () => { + const query = { doName: 'exampleDoName' }; + await dataService.guNameData(query); + + expect(mockPrisma.region.findMany).toHaveBeenCalledWith({ + where: { doName: query.doName }, + select: { guName: true }, + }); }); }); diff --git a/src/events/events.service.spec.ts b/src/events/events.service.spec.ts index 5f25bb9..7b35d27 100644 --- a/src/events/events.service.spec.ts +++ b/src/events/events.service.spec.ts @@ -1,20 +1,86 @@ -import { Test, TestingModule } from '@nestjs/testing'; import { EventsService } from './events.service'; -import { PrismaService } from 'src/prisma/prisma.service'; -describe('EventsService', () => { - let prisma: PrismaService - let service: EventsService; +let eventsService; +let mockPrisma; +let mockCacheManager; + +mockPrisma = { + event: { + create: jest.fn(), + findMany: jest.fn(), + findUnique: jest.fn(), + findFirst: jest.fn(), + update: jest.fn(), + delete: jest.fn(), + }, +}; +mockCacheManager = { + get: jest.fn(), + set: jest.fn() +} - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [EventsService], - }).compile(); +mockCacheManager = {}; +eventsService = new EventsService(mockCacheManager, mockPrisma); - service = module.get(EventsService); +describe('EventsService', () => { + beforeEach(async () => { + jest.resetAllMocks(); }); - it('should be defined', () => { - expect(service).toBeDefined(); + test('create Method', async () => { + const mockReturn = 'create Value'; + mockPrisma.event.create.mockReturnValue(mockReturn); + + const createEventDto = { + eventName: '같이 산책하실분', + maxSize: 10, + eventDate: new Date('2023-11-12'), + signupStartDate: new Date('2023-11-10'), + signupEndDate: new Date('2023-11-11'), + location_City: '서울특별시', + location_District: '종로구', + content: '재밌게 놀아요', + category: '산책', + isDeleted: false, + isVerified: '🙋‍♀️아무나', + eventImg: null + }; + + const createEventData = await eventsService.create(createEventDto) + expect(createEventData).toEqual(mockReturn) + expect(mockPrisma.event.creaet).toHaveBeenCalledWith({ + data: createEventDto + }) }); + + test('findAll Method', async ()=> { + const mockReturn = 'findMany Value' + mockPrisma.event.findMany.mockReturnValue(mockReturn) + + const page = 1 + const events = await eventsService.findAll(page) + + expect(events).toBe(mockReturn) + expect(eventsService.mockPrisma.event.findMany).toHaveBeenCalledTimes(1) + }) + + test('findOne Method', async ()=> { + const mockReturn = "findOne Value" + mockPrisma.event.findUnique.mockReturn(mockReturn) + + const result = await eventsService.findOne(1) + expect(result).toEqual(mockReturn) + }) + + test('findOne NotFoundException', async ()=> { + mockPrisma.event.findUnique.mockReturnValue(null) + try { + await eventsService.findOne(12345) + } catch (err) { + expect(eventsService.findOne).toHaveBeenCalledTimes(1); + expect(eventsService.findOne).toHaveBeenCalledWith(12345) + + expect(err.message).toEqual(`12345번 이벤트가 없습니다`) + } + }) }); diff --git a/src/searches/searches.controller.spec.ts b/src/searches/searches.controller.spec.ts index 63a075e..7252bcd 100644 --- a/src/searches/searches.controller.spec.ts +++ b/src/searches/searches.controller.spec.ts @@ -1,20 +1,40 @@ -import { Test, TestingModule } from '@nestjs/testing'; import { SearchesController } from './searches.controller'; -import { SearchesService } from './searches.service'; +import { SearchesDto } from './searches.dto/searches.dto'; describe('SearchesController', () => { - let controller: SearchesController; + let searchesController + let mockSearchesService beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [SearchesController], - providers: [SearchesService], - }).compile(); - - controller = module.get(SearchesController); + mockSearchesService = { + search: jest.fn() + } + searchesController = new SearchesController(mockSearchesService) }); - it('should be defined', () => { - expect(controller).toBeDefined(); - }); + test('search Method', async () =>{ + const sampleEvents = [ + { + eventId: 1, + eventName: "Developer", + content: "안녕하세요", + createdAt: "2023-08-25T03:43:20", + updatedAt: "2023-08-25T03:43:20", + }, + { + eventId: 2, + eventName: "Developer", + content: "안녕하세요", + createdAt: "2023-08-26T03:43:20", + updatedAt: "2023-08-26T03:43:20", + }, + ]; + + mockSearchesService.search.mockReturnValue(sampleEvents) + const searchDto = SearchesDto + + await searchesController.searchBy(1, searchDto) + + expect(mockSearchesService.search).toHaveBeenCalledTimes(1) + }) }); diff --git a/src/searches/searches.service.spec.ts b/src/searches/searches.service.spec.ts index e09bd0e..e81a11c 100644 --- a/src/searches/searches.service.spec.ts +++ b/src/searches/searches.service.spec.ts @@ -1,18 +1,64 @@ -import { Test, TestingModule } from '@nestjs/testing'; import { SearchesService } from './searches.service'; describe('SearchesService', () => { - let service: SearchesService; + let searchesService; + let mockPrisma; - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [SearchesService], - }).compile(); - - service = module.get(SearchesService); + beforeEach(() => { + mockPrisma = { + event: { + findMany: jest.fn(), + }, + }; + searchesService = new SearchesService(mockPrisma); }); - it('should be defined', () => { - expect(service).toBeDefined(); + test('search', async () => { + const page = 1; + const searchesDto = { + keyWord: 'exampleKeyword', + verify: true, + city: 'exampleCity', + guName: 'exampleGuName', + category: 'exampleCategory', + }; + + await searchesService.search(page, searchesDto); + + expect(mockPrisma.event.findMany).toHaveBeenCalledWith({ + take: 4, + skip: page, + where: { + isDeleted: false, + AND: expect.arrayContaining([ + expect.objectContaining({ + OR: expect.arrayContaining([ + expect.objectContaining({ + eventName: { contains: searchesDto.keyWord }, + }), + expect.objectContaining({ + content: { contains: searchesDto.keyWord }, + }), + ]), + }), + expect.objectContaining({ + isVerified: { contains: searchesDto.verify }, + }), + expect.objectContaining({ + location_City: { contains: searchesDto.city }, + }), + expect.objectContaining({ + location_District: { contains: searchesDto.guName }, + }), + expect.objectContaining({ + category: { contains: searchesDto.category }, + }), + ]), + }, + include: expect.any(Object), + orderBy: { + createdAt: 'desc', + }, + }); }); });