Skip to content

Commit

Permalink
fix: 그룹 관련 이벤트 핸들러 제거 및 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
Coalery committed Sep 17, 2024
1 parent fc41731 commit ad70a8a
Show file tree
Hide file tree
Showing 60 changed files with 1,319 additions and 1,717 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.18.0
v20.17.0
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
}
31 changes: 31 additions & 0 deletions src/__test__/fixtures/GroupBookmarkFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { faker } from '@faker-js/faker';

import {
GroupBookmark,
GroupBookmarkConstructorParams,
} from '@sight/app/domain/group/model/GroupBookmark';

function generator(
params: Partial<GroupBookmarkConstructorParams> = {},
): GroupBookmark {
return new GroupBookmark({
id: faker.string.uuid(),
userId: faker.string.uuid(),
groupId: faker.string.uuid(),
createdAt: faker.date.anytime(),
...params,
});
}

function normal(
params: Partial<
Pick<GroupBookmarkConstructorParams, 'userId' | 'groupId'>
> = {},
): GroupBookmark {
return generator(params);
}

export const GroupBookmarkFixture = {
raw: generator,
normal,
};
83 changes: 83 additions & 0 deletions src/__test__/fixtures/GroupFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { faker } from '@faker-js/faker';
import {
CUSTOMER_SERVICE_GROUP_ID,
GroupAccessGrade,
GroupCategory,
GroupState,
PRACTICE_GROUP_ID,
} from '@sight/app/domain/group/model/constant';

import {
Group,
GroupConstructorParams,
} from '@sight/app/domain/group/model/Group';

function generator(params: Partial<GroupConstructorParams> = {}): Group {
return new Group({
id: faker.string.uuid(),
category: faker.helpers.enumValue(GroupCategory),
state: faker.helpers.enumValue(GroupState),
title: faker.lorem.word(),
authorUserId: faker.string.uuid(),
adminUserId: faker.string.uuid(),
purpose: faker.lorem.sentence(),
interestIds: [faker.string.uuid()],
technology: [faker.lorem.word()],
grade: faker.helpers.enumValue(GroupAccessGrade),
lastUpdaterUserId: faker.string.uuid(),
repository: faker.internet.url(),
allowJoin: faker.datatype.boolean(),
hasPortfolio: faker.datatype.boolean(),
createdAt: faker.date.anytime(),
updatedAt: faker.date.anytime(),
...params,
});
}

function inProgressJoinable(
params: Partial<GroupConstructorParams> = {},
): Group {
return generator({
state: GroupState.PROGRESS,
grade: GroupAccessGrade.MEMBER,
allowJoin: true,
...params,
});
}

function suspended(params: Partial<GroupConstructorParams> = {}): Group {
return generator({
state: GroupState.SUSPEND,
...params,
});
}

function successfullyEnd(params: Partial<GroupConstructorParams> = {}): Group {
return generator({
state: GroupState.END_SUCCESS,
...params,
});
}

function customerService(params: Partial<GroupConstructorParams> = {}): Group {
return generator({
id: CUSTOMER_SERVICE_GROUP_ID,
...params,
});
}

function practice(params: Partial<GroupConstructorParams> = {}): Group {
return generator({
id: PRACTICE_GROUP_ID,
...params,
});
}

export const GroupFixture = {
raw: generator,
inProgressJoinable,
successfullyEnd,
suspended,
customerService,
practice,
};
47 changes: 0 additions & 47 deletions src/__test__/fixtures/domain/group.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { faker } from '@faker-js/faker';

import {
GroupAccessGrade,
GroupCategory,
GroupState,
} from '@sight/app/domain/group/model/constant';
import {
Group,
GroupConstructorParams,
} from '@sight/app/domain/group/model/Group';
import {
GroupBookmark,
GroupBookmarkConstructorParams,
} from '@sight/app/domain/group/model/GroupBookmark';
import {
GroupLog,
GroupLogConstructorParams,
Expand All @@ -22,28 +9,6 @@ import {
GroupMemberConstructorParams,
} from '@sight/app/domain/group/model/GroupMember';

export function generateGroup(params?: Partial<GroupConstructorParams>): Group {
return new Group({
id: faker.string.uuid(),
category: faker.helpers.enumValue(GroupCategory),
state: GroupState.PROGRESS,
title: faker.lorem.word(),
authorUserId: faker.string.uuid(),
adminUserId: faker.string.uuid(),
purpose: faker.lorem.sentence(),
interestIds: [faker.string.uuid()],
technology: [faker.lorem.word()],
grade: faker.helpers.enumValue(GroupAccessGrade),
lastUpdaterUserId: faker.string.uuid(),
repository: faker.internet.url(),
allowJoin: faker.datatype.boolean(),
hasPortfolio: faker.datatype.boolean(),
createdAt: faker.date.anytime(),
updatedAt: faker.date.anytime(),
...params,
});
}

export function generateGroupMember(
params?: Partial<GroupMemberConstructorParams>,
): GroupMember {
Expand All @@ -68,15 +33,3 @@ export function generateGroupLog(
...params,
});
}

export function generateGroupBookmark(
params?: Partial<GroupBookmarkConstructorParams>,
): GroupBookmark {
return new GroupBookmark({
id: faker.string.uuid(),
userId: faker.string.uuid(),
groupId: faker.string.uuid(),
createdAt: faker.date.anytime(),
...params,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AddBookmarkCommand } from '@sight/app/application/group/command/addBook
import { AddBookmarkCommandHandler } from '@sight/app/application/group/command/addBookmark/AddBookmarkCommandHandler';

import { GroupBookmarkFactory } from '@sight/app/domain/group/GroupBookmarkFactory';
import { Group } from '@sight/app/domain/group/model/Group';
import {
GroupBookmarkRepository,
IGroupBookmarkRepository,
Expand All @@ -14,29 +13,45 @@ import {
GroupRepository,
IGroupRepository,
} from '@sight/app/domain/group/IGroupRepository';
import {
CUSTOMER_SERVICE_GROUP_ID,
PRACTICE_GROUP_ID,
} from '@sight/app/domain/group/model/constant';

import { DomainFixture } from '@sight/__test__/fixtures';
import { Message } from '@sight/constant/message';
import { SlackSender } from '@sight/app/domain/adapter/ISlackSender';
import { GroupFixture } from '@sight/__test__/fixtures/GroupFixture';
import { GroupBookmarkFixture } from '@sight/__test__/fixtures/GroupBookmarkFixture';

describe('AddBookmarkCommandHandler', () => {
let handler: AddBookmarkCommandHandler;
let groupBookmarkFactory: GroupBookmarkFactory;
let groupRepository: jest.Mocked<IGroupRepository>;
let groupBookmarkRepository: jest.Mocked<IGroupBookmarkRepository>;

beforeAll(async () => {
beforeEach(async () => {
advanceTo(new Date());

const testModule = await Test.createTestingModule({
providers: [
AddBookmarkCommandHandler,
GroupBookmarkFactory,
{ provide: GroupRepository, useValue: {} },
{ provide: GroupBookmarkRepository, useValue: {} },
{
provide: GroupRepository,
useValue: {
findById: jest.fn(),
},
},
{
provide: GroupBookmarkRepository,
useValue: {
findByGroupIdAndUserId: jest.fn(),
nextId: jest.fn(),
save: jest.fn(),
},
},
{
provide: SlackSender,
useValue: {
send: jest.fn(),
},
},
],
}).compile();

Expand All @@ -46,29 +61,12 @@ describe('AddBookmarkCommandHandler', () => {
groupBookmarkRepository = testModule.get(GroupBookmarkRepository);
});

afterAll(() => {
clear();
});
afterEach(() => clear());

describe('execute', () => {
let group: Group;

const newBookmarkId = 'new-bookmark-id';
const groupId = 'groupId';
const userId = 'userId';

beforeEach(() => {
group = DomainFixture.generateGroup({ id: groupId });

groupRepository.findById = jest.fn().mockResolvedValue(group);
groupBookmarkRepository.findByGroupIdAndUserId = jest
.fn()
.mockResolvedValue(null);
groupBookmarkRepository.nextId = jest.fn().mockReturnValue(newBookmarkId);

groupBookmarkRepository.save = jest.fn();
});

test('그룹이 존재하지 않으면 예외가 발생해야 한다', async () => {
groupRepository.findById = jest.fn().mockResolvedValue(null);

Expand All @@ -78,34 +76,31 @@ describe('AddBookmarkCommandHandler', () => {
});

test('고객 센터 그룹이라면 예외가 발생해야 한다', async () => {
const customerServiceGroup = DomainFixture.generateGroup({
id: CUSTOMER_SERVICE_GROUP_ID,
});
groupRepository.findById = jest
.fn()
.mockResolvedValue(customerServiceGroup);
const customerServiceGroup = GroupFixture.customerService();
groupRepository.findById.mockResolvedValue(customerServiceGroup);

await expect(
handler.execute(new AddBookmarkCommand(groupId, userId)),
).rejects.toThrowError(Message.DEFAULT_BOOKMARKED_GROUP);
});

test('그룹 활용 실습 그룹이라면 예외가 발생해야 한다', async () => {
const practiceGroup = DomainFixture.generateGroup({
id: PRACTICE_GROUP_ID,
});
groupRepository.findById = jest.fn().mockResolvedValue(practiceGroup);
const practiceGroup = GroupFixture.practice();
groupRepository.findById.mockResolvedValue(practiceGroup);

await expect(
handler.execute(new AddBookmarkCommand(groupId, userId)),
).rejects.toThrowError(Message.DEFAULT_BOOKMARKED_GROUP);
});

test('이미 즐겨찾기 중이라면 새로운 즐겨찾기를 생성하지 않아야 한다', async () => {
const bookmark = DomainFixture.generateGroupBookmark();
groupBookmarkRepository.findByGroupIdAndUserId = jest
.fn()
.mockResolvedValue(bookmark);
const group = GroupFixture.inProgressJoinable();
const bookmark = GroupBookmarkFixture.normal();

groupRepository.findById.mockResolvedValue(group);
groupBookmarkRepository.findByGroupIdAndUserId.mockResolvedValue(
bookmark,
);
jest.spyOn(groupBookmarkFactory, 'create');

await handler.execute(new AddBookmarkCommand(groupId, userId));
Expand All @@ -114,6 +109,12 @@ describe('AddBookmarkCommandHandler', () => {
});

test('즐겨찾기를 생성해야 한다', async () => {
const group = GroupFixture.inProgressJoinable();
const newBookmarkId = 'new-bookmark-id';

groupRepository.findById.mockResolvedValue(group);
groupBookmarkRepository.nextId.mockReturnValue(newBookmarkId);

const expected = groupBookmarkFactory.create({
id: newBookmarkId,
groupId,
Expand All @@ -122,7 +123,6 @@ describe('AddBookmarkCommandHandler', () => {

await handler.execute(new AddBookmarkCommand(groupId, userId));

expect(groupBookmarkRepository.save).toBeCalledTimes(1);
expect(groupBookmarkRepository.save).toBeCalledWith(expected);
});
});
Expand Down
Loading

0 comments on commit ad70a8a

Please sign in to comment.