Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test] 모각코, 그룹 모집 필터링, 오류 처리 테스트 #478

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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