Skip to content

Commit f0a5d1f

Browse files
committed
test: 워크스페이스 서비스 유닛 테스트 추가
- import userModel - 중복 가입 에러에 대한 테스트 케이스 추가
1 parent b8ecaf2 commit f0a5d1f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

server/apis/workspace/service.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const userModel = require('@apis/user/model');
12
const workspaceModel = require('./model');
23
const workspaceService = require('./service');
34
const { default: InvalidJoinError } = require('@errors/invalid-join-error');
@@ -57,18 +58,18 @@ describe('create', () => {
5758

5859
describe('join', () => {
5960
const USER_ID = 1;
61+
const WORKSPACE_ID = 1;
62+
const WORKSPACE_NAME = 'Wab';
6063

6164
it('유효한 참여코드가 주어진 경우 성공한다.', async () => {
62-
const WORKSPACE_ID = 1;
63-
const WORKSPACE_NAME = 'Wab';
64-
6565
workspaceModel.findOne.mockResolvedValueOnce({
6666
id: WORKSPACE_ID,
6767
name: WORKSPACE_NAME,
6868
code: VALID_CODE,
6969
users: [],
7070
moms: [],
7171
});
72+
userModel.find.mockResolvedValueOnce([{ workspaces: [] }]);
7273

7374
expect(workspaceService.join(USER_ID, VALID_CODE)).resolves.toEqual({
7475
id: WORKSPACE_ID,
@@ -91,6 +92,21 @@ describe('join', () => {
9192
).rejects.toThrow(InvalidJoinError);
9293
});
9394

95+
it('이미 참여한 워크스페이스인 경우 실패한다.', async () => {
96+
workspaceModel.findOne.mockResolvedValueOnce({
97+
id: WORKSPACE_ID,
98+
name: WORKSPACE_NAME,
99+
code: VALID_CODE,
100+
users: [],
101+
moms: [],
102+
});
103+
userModel.find.mockResolvedValueOnce([{ workspaces: [WORKSPACE_ID] }]);
104+
105+
expect(() => workspaceService.join(USER_ID, VALID_CODE)).rejects.toThrow(
106+
InvalidJoinError,
107+
);
108+
});
109+
94110
it('db 업데이트 중 에러가 발생하면 실패한다.', async () => {
95111
workspaceModel.updateOne.mockRejectedValueOnce(
96112
new Error('Some error in database operation'),

server/apis/workspace/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const join = async (userId: number, code: string) => {
3232
)[0].workspaces;
3333

3434
if (userWorkspaces.includes(id)) {
35-
throw new Error('이미 참여한 워크스페이스에요 ^^');
35+
throw new InvalidJoinError('이미 참여한 워크스페이스에요 ^^');
3636
}
3737

3838
await workspaceModel.updateOne({ id }, { $addToSet: { users: userId } });

0 commit comments

Comments
 (0)