Skip to content

Commit e527e74

Browse files
committed
test: 워크스페이스 생성 및 참여 서비스 로직
1 parent 3c194b9 commit e527e74

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
jest.mock('./model', () => {
2+
return {
3+
repository: {},
4+
create: jest.fn(),
5+
findOne: jest.fn(),
6+
updateOne: jest.fn(),
7+
};
8+
});
9+
const workspaceModel = require('./model');
10+
11+
jest.mock('@apis/user/model', () => {
12+
return {
13+
updateOne: jest.fn(),
14+
};
15+
});
16+
const userModel = require('@apis/user/model');
17+
18+
const VALID_CODE = 'wab-0000-0000-0000';
19+
20+
jest.mock('uuid', () => {
21+
return {
22+
v4: jest.fn(() => VALID_CODE),
23+
};
24+
});
25+
26+
const workspaceService = require('./service');
27+
28+
describe('create', () => {
29+
it('워크스페이스 이름이 주어진 경우 생성에 성공한다.', async () => {
30+
const WORKSPACE_NAME = 'Wab';
31+
32+
workspaceModel.create.mockResolvedValueOnce({
33+
name: WORKSPACE_NAME,
34+
code: VALID_CODE,
35+
});
36+
37+
expect(workspaceService.create(WORKSPACE_NAME)).resolves.toEqual({
38+
name: WORKSPACE_NAME,
39+
code: VALID_CODE,
40+
});
41+
});
42+
43+
it('워크스페이스 이름이 없는 경우 생성에 실패한다.', async () => {
44+
expect(() => workspaceService.create()).rejects.toThrow(
45+
'워크스페이스 이름을 입력하세요 ^^',
46+
);
47+
});
48+
});
49+
50+
describe('join', () => {
51+
const USER_ID = 1;
52+
53+
it('유효한 참여코드가 주어진 경우 성공한다.', async () => {
54+
const WORKSPACE_ID = 1;
55+
const WORKSPACE_NAME = 'Wab';
56+
57+
workspaceModel.findOne.mockResolvedValueOnce({
58+
id: WORKSPACE_ID,
59+
name: WORKSPACE_NAME,
60+
code: VALID_CODE,
61+
users: [],
62+
moms: [],
63+
});
64+
65+
expect(workspaceService.join(USER_ID, VALID_CODE)).resolves.toEqual({
66+
id: WORKSPACE_ID,
67+
name: WORKSPACE_NAME,
68+
code: VALID_CODE,
69+
});
70+
});
71+
72+
it('참여코드가 없는 경우 실패한다.', async () => {
73+
expect(() => workspaceService.join(USER_ID)).rejects.toThrow(
74+
'참여코드를 입력하세요 ^^',
75+
);
76+
});
77+
78+
it('참여코드가 유효하지 않은 경우 실패한다.', async () => {
79+
workspaceModel.findOne.mockResolvedValueOnce(null);
80+
81+
expect(() =>
82+
workspaceService.join(USER_ID, 'invalid-code'),
83+
).rejects.toThrow('잘못된 참여코드에요 ^^');
84+
});
85+
86+
it('db 업데이트 중 에러가 발생하면 실패한다.', async () => {
87+
workspaceModel.updateOne.mockRejectedValueOnce(
88+
new Error('Some error in database operation'),
89+
);
90+
91+
expect(() => workspaceService.join(USER_ID, VALID_CODE)).rejects.toThrow();
92+
});
93+
});

server/apis/workspace/service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import AuthorizationError from '@errors/authorization-error';
55
import InvalidJoinError from '@errors/invalid-join-error';
66

77
export const create = async (name: string) => {
8+
if (!name) throw new Error('워크스페이스 이름을 입력하세요 ^^');
9+
810
const code = uuidv4();
911

1012
const workspace = await workspaceModel.create({ name, code });

server/jest.config.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ module.exports = {
22
preset: 'ts-jest', // to use typescript
33
verbose: true,
44
moduleNameMapper: {
5-
"@apis/(.*)": "<rootDir>/apis/$1",
6-
"@config": "<rootDir>/config",
7-
"@constants/(.*)": "<rootDir>/constants/$1",
8-
"@db": "<rootDir>/db",
9-
"@middlewares/(.*)": "<rootDir>/middlewares/$1",
10-
"@utils/(.*)": "<rootDir>/utils/$1"
11-
}
12-
}
5+
'@apis/(.*)': '<rootDir>/apis/$1',
6+
'@config': '<rootDir>/config',
7+
'@constants/(.*)': '<rootDir>/constants/$1',
8+
'@db': '<rootDir>/db',
9+
'@errors': '<rootDir>/errors',
10+
'@middlewares/(.*)': '<rootDir>/middlewares/$1',
11+
'@utils/(.*)': '<rootDir>/utils/$1',
12+
},
13+
};

0 commit comments

Comments
 (0)