Skip to content

Commit 2f0bad3

Browse files
committed
test : workspace info 테스트 코드 작성
1 parent 7e91bf4 commit 2f0bad3

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

server/apis/workspace/service.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const workspaceModel = require('./model');
22
const workspaceService = require('./service');
33
const { default: InvalidJoinError } = require('@errors/invalid-join-error');
4+
const {
5+
default: InvalidWorkspaceError,
6+
} = require('@errors/invalid-workspace-error');
47

58
jest.mock('./model', () => {
69
return {
@@ -12,10 +15,15 @@ jest.mock('./model', () => {
1215

1316
jest.mock('@apis/user/model', () => {
1417
return {
18+
find: jest.fn(),
1519
updateOne: jest.fn(),
1620
};
1721
});
1822

23+
jest.mock('@apis/mom/model', () => {
24+
return { find: jest.fn() };
25+
});
26+
1927
const VALID_CODE = 'wab-0000-0000-0000';
2028

2129
jest.mock('uuid', () => {
@@ -91,4 +99,49 @@ describe('join', () => {
9199
});
92100
});
93101

102+
describe('info', () => {
103+
const WORKSPACE_ID = 1;
104+
const INVALID_WORKSPACE_ID = -1;
105+
106+
it('워크스페이스 ID가 DB에 존재할 경우 조회에 성공한다.', async () => {
107+
const WORKSPACE_NAME = 'Wab';
108+
109+
workspaceModel.findOne.mockResolvedValueOnce({
110+
id: WORKSPACE_ID,
111+
name: WORKSPACE_NAME,
112+
code: VALID_CODE,
113+
users: [],
114+
moms: [],
115+
});
116+
117+
expect(workspaceService.info(WORKSPACE_ID)).resolves.toEqual({
118+
name: WORKSPACE_NAME,
119+
users: [],
120+
moms: [],
121+
});
122+
});
123+
124+
it('워크스페이스 Id가 없는 경우 실패한다.', async () => {
125+
expect(() => workspaceService.info()).rejects.toThrow(
126+
InvalidWorkspaceError,
127+
);
128+
});
129+
130+
it('워크스페이스 Id가 DB에 존재하지 않는 경우 실패한다.', async () => {
131+
workspaceModel.findOne.mockResolvedValueOnce(null);
132+
133+
expect(() => workspaceService.info(INVALID_WORKSPACE_ID)).rejects.toThrow(
134+
InvalidWorkspaceError,
135+
);
136+
});
137+
138+
it('워크스페이스 정보 획득 실패 시 에러를 던진다.', async () => {
139+
workspaceModel.findOne.mockRejectedValueOnce(
140+
new Error('Some error in database operation'),
141+
);
142+
143+
expect(() => workspaceService.info(WORKSPACE_ID)).rejects.toThrow();
144+
});
145+
});
146+
94147
export {};

server/apis/workspace/service.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const join = async (userId: number, code: string) => {
3434
};
3535

3636
export const info = async (workspaceId: number) => {
37-
if (!workspaceId) throw new InvalidJoinError('잘못된 접근이에요 ^^');
37+
if (!workspaceId) throw new InvalidWorkspaceError('잘못된 접근이에요 ^^');
3838

3939
const workspace = await workspaceModel.findOne({ id: workspaceId });
4040

@@ -43,17 +43,16 @@ export const info = async (workspaceId: number) => {
4343

4444
const { name, users: userIds, moms: momsIds } = workspace;
4545

46-
const users: Pick<User, 'name' | 'avatarUrl'>[] = await userModel.find(
47-
{
48-
id: { $in: userIds },
49-
},
50-
{ name: 1, avatarUrl: 1, _id: 0 },
51-
);
52-
53-
const moms: string[] = await momModel.find(
54-
{ id: { $in: momsIds } },
55-
{ name: 1 },
56-
);
46+
const users: Pick<User, 'name' | 'avatarUrl'>[] =
47+
(await userModel.find(
48+
{
49+
id: { $in: userIds },
50+
},
51+
{ name: 1, avatarUrl: 1, _id: 0 },
52+
)) || [];
53+
54+
const moms: string[] =
55+
(await momModel.find({ id: { $in: momsIds } }, { name: 1 })) || [];
5756

5857
return { name, users, moms };
5958
};

0 commit comments

Comments
 (0)