Skip to content

Commit

Permalink
Merge pull request #168 from LocalMingle/jonghwa
Browse files Browse the repository at this point in the history
[작업완료]:테스트 코드 작성
  • Loading branch information
kimjonghwa230412 authored Nov 13, 2023
2 parents 6ae0e82 + 793e2c4 commit 953b675
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const jwtSecret = process.env.JWT_SECRET;
imports: [PrismaModule, PassportModule, UsersModule, JwtModule.register({})],
controllers: [AuthController],
providers: [
AuthService,
AuthService,
UsersService,
JwtAccessStrategy,
Expand Down
50 changes: 46 additions & 4 deletions src/data/data.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(DataController);
dataService = module.get<DataService>(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] 중 하나를 입력하세요' });
});
});
});
25 changes: 16 additions & 9 deletions src/data/data.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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 },
});
});
});
90 changes: 78 additions & 12 deletions src/events/events.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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번 이벤트가 없습니다`)
}
})
});
44 changes: 32 additions & 12 deletions src/searches/searches.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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)
})
});
66 changes: 56 additions & 10 deletions src/searches/searches.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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',
},
});
});
});

0 comments on commit 953b675

Please sign in to comment.