Skip to content

Commit

Permalink
Merge pull request #478 from ldhbenecia/feature/test-code
Browse files Browse the repository at this point in the history
[Test] 모각코, 그룹 모집 필터링, 오류 처리 테스트
  • Loading branch information
ldhbenecia authored Dec 14, 2023
2 parents b71a023 + 8a66657 commit 5cc513d
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 41 deletions.
84 changes: 54 additions & 30 deletions app/backend/src/groups/groups.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GroupsRepository } from './groups.repository';
import { PrismaService } from 'prisma/prisma.service';
import { CreateGroupsDto } from './dto/create-groups.dto';
import { Member } from '@prisma/client';
import { ForbiddenException, NotFoundException } from '@nestjs/common';

describe('GroupsRepository', () => {
let repository: GroupsRepository;
Expand All @@ -25,6 +26,7 @@ describe('GroupsRepository', () => {
findUnique: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
count: jest.fn(),
},
},
},
Expand All @@ -35,13 +37,22 @@ describe('GroupsRepository', () => {
prismaService = module.get<PrismaService>(PrismaService);
});

const mockMember: Member = {
id: BigInt(1),
providerId: '11111122222222',
email: '[email protected]',
nickname: 'morak morak',
profilePicture: 'morak morak.jpg',
socialType: 'google',
createdAt: new Date(),
};

describe('getAllGroups', () => {
it('모든 그룹을 반환해야 함', async () => {
const expectedGroups = [
{ id: BigInt(1), title: '부스트캠프 웹·모바일 8기', membersCount: 212 },
{ id: BigInt(2), title: '부스트캠프 웹·모바일 9기', membersCount: 187 },
];
const expectedGroups = [{ id: BigInt(1), title: '부스트캠프 웹·모바일 8기', membersCount: 212 }];
const groupId = 212;
jest.spyOn(prismaService.group, 'findMany').mockResolvedValue(expectedGroups);
jest.spyOn(prismaService.groupToUser, 'count').mockResolvedValue(groupId);

const result = await repository.getAllGroups();

Expand Down Expand Up @@ -96,15 +107,6 @@ describe('GroupsRepository', () => {
describe('joinGroup', () => {
it('그룹에 참여해야 함', async () => {
const groupId = 1;
const member: Member = {
id: BigInt(1),
providerId: '11111122222222',
email: '[email protected]',
nickname: 'morak morak',
profilePicture: 'morak morak.jpg',
socialType: 'google',
createdAt: new Date(),
};

jest.spyOn(prismaService.group, 'findUnique').mockResolvedValue({
id: BigInt(1),
Expand All @@ -113,33 +115,43 @@ describe('GroupsRepository', () => {

jest.spyOn(prismaService.groupToUser, 'create').mockResolvedValue({
groupId: BigInt(1),
userId: member.id,
userId: mockMember.id,
});

await repository.joinGroup(groupId, member);
await repository.joinGroup(groupId, mockMember);

// 231204 ldhbenecia | 해당 함수가 특정 인자와 함께 호출되었는지 여부를 확인
expect(prismaService.groupToUser.create).toHaveBeenCalledWith({
data: {
groupId: BigInt(1),
userId: member.id,
userId: mockMember.id,
},
});
});

it('가입할 그룹이 없는 경우 NotFoundException 발생', async () => {
jest.spyOn(prismaService.group, 'findUnique').mockResolvedValueOnce(null);

await expect(repository.joinGroup(1, mockMember)).rejects.toThrowError(NotFoundException);
});

it('그룹에 이미 가입한 멤버가 가입 요청을 하면 Forbidden 발생', async () => {
jest.spyOn(prismaService.group, 'findUnique').mockResolvedValue({
id: BigInt(1),
title: '부스트캠프 웹·모바일 8기',
});
jest.spyOn(prismaService.groupToUser, 'findUnique').mockResolvedValueOnce({
groupId: BigInt(1),
userId: mockMember.id,
});

await expect(repository.joinGroup(1, mockMember)).rejects.toThrowError(ForbiddenException);
});
});

describe('leaveGroup', () => {
it('그룹 탈퇴를 해야 함', async () => {
const groupId = 1;
const member: Member = {
id: BigInt(1),
providerId: '11111122222222',
email: '[email protected]',
nickname: 'morak morak',
profilePicture: 'morak morak.jpg',
socialType: 'google',
createdAt: new Date(),
};

jest.spyOn(prismaService.group, 'findUnique').mockResolvedValue({
id: BigInt(1),
Expand All @@ -148,21 +160,21 @@ describe('GroupsRepository', () => {

jest.spyOn(prismaService.groupToUser, 'findUnique').mockResolvedValue({
groupId: BigInt(1),
userId: member.id,
userId: mockMember.id,
});

jest.spyOn(prismaService.groupToUser, 'delete').mockResolvedValue({
groupId: BigInt(1),
userId: member.id,
userId: mockMember.id,
});

await repository.leaveGroup(groupId, member);
await repository.leaveGroup(groupId, mockMember);

expect(prismaService.groupToUser.findUnique).toHaveBeenCalledWith({
where: {
groupId_userId: {
groupId: groupId,
userId: member.id,
userId: mockMember.id,
},
},
});
Expand All @@ -171,11 +183,23 @@ describe('GroupsRepository', () => {
where: {
groupId_userId: {
groupId: groupId,
userId: member.id,
userId: mockMember.id,
},
},
});
});

it('탈퇴할 그룹이 없는 경우 NotFoundException 발생', async () => {
jest.spyOn(prismaService.group, 'findUnique').mockResolvedValueOnce(null);

await expect(repository.leaveGroup(1, mockMember)).rejects.toThrowError(NotFoundException);
});

it('해당 멤버가 그룹에 없는데 탈퇴 요청을 할 경우 NotFoundException 발생', async () => {
jest.spyOn(prismaService.groupToUser, 'findUnique').mockResolvedValueOnce(null);

await expect(repository.leaveGroup(1, mockMember)).rejects.toThrowError(NotFoundException);
});
});

describe('getMyGroups', () => {
Expand Down
Loading

0 comments on commit 5cc513d

Please sign in to comment.