Skip to content

Commit 192e559

Browse files
authored
Merge pull request #36 from boostcampwm-2022/test/#24-K
Test/#24-K: μ›Œν¬μŠ€νŽ˜μ΄μŠ€ 생성 및 μ°Έμ—¬ μ„œλΉ„μŠ€ 둜직
2 parents ef115cb + ab19ac5 commit 192e559

File tree

7 files changed

+102
-29
lines changed

7 files changed

+102
-29
lines changed

β€Žserver/apis/auth/service.test.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ describe('login', () => {
6565
expect(() => login()).rejects.toThrow('fail');
6666
});
6767
});
68+
69+
export {};

β€Žserver/apis/user/service.test.tsβ€Ž

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,23 @@ jest.mock('@apis/workspace/model', () => {
1111
return { find: jest.fn() };
1212
});
1313

14-
describe('getWorkspaces()', () => {
14+
describe('getWorkspaces', () => {
1515
const successfulUser = '';
1616
const successfulWorkspaces: Object[] = [];
1717

1818
it('정상적인 μœ μ € 아이디λ₯Ό λ°›μ•„μ„œ μ„±κ³΅μ μœΌλ‘œ μ›Œν¬μŠ€νŽ˜μ΄μŠ€λ₯Ό κ°€μ Έμ˜¨λ‹€.', async () => {
19-
// arrange
2019
userModel.findOne.mockResolvedValueOnce(successfulUser);
2120
workspaceModel.find.mockResolvedValueOnce(successfulWorkspaces);
2221

23-
// act
2422
const workspaces = await getWorkspaces('id', 'id');
2523

26-
// assert
2724
expect(workspaces).toEqual(successfulWorkspaces);
2825
});
2926

3027
it('비정상적인 μœ μ € 아이디λ₯Ό λ°›μœΌλ©΄ μ—λŸ¬λ₯Ό λ˜μ§„λ‹€.', async () => {
31-
// arrange
3228
const user1 = 1;
3329
const user2 = 2;
3430

35-
// act & assert
3631
expect(() => getWorkspaces(user1, user2)).rejects.toThrow(
3732
AuthorizationError,
3833
);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const workspaceModel = require('./model');
2+
const workspaceService = require('./service');
3+
const { default: InvalidJoinError } = require('@errors/invalid-join-error');
4+
5+
jest.mock('./model', () => {
6+
return {
7+
create: jest.fn(),
8+
findOne: jest.fn(),
9+
updateOne: jest.fn(),
10+
};
11+
});
12+
13+
jest.mock('@apis/user/model', () => {
14+
return {
15+
updateOne: jest.fn(),
16+
};
17+
});
18+
19+
const VALID_CODE = 'wab-0000-0000-0000';
20+
21+
jest.mock('uuid', () => {
22+
return {
23+
v4: jest.fn(() => VALID_CODE),
24+
};
25+
});
26+
27+
describe('create', () => {
28+
it('μ›Œν¬μŠ€νŽ˜μ΄μŠ€ 이름이 μ£Όμ–΄μ§„ 경우 생성에 μ„±κ³΅ν•œλ‹€.', async () => {
29+
const WORKSPACE_NAME = 'Wab';
30+
31+
workspaceModel.create.mockResolvedValueOnce({
32+
name: WORKSPACE_NAME,
33+
code: VALID_CODE,
34+
});
35+
36+
expect(workspaceService.create(WORKSPACE_NAME)).resolves.toEqual({
37+
name: WORKSPACE_NAME,
38+
code: VALID_CODE,
39+
});
40+
});
41+
42+
it('μ›Œν¬μŠ€νŽ˜μ΄μŠ€ 이름이 μ—†λŠ” 경우 생성에 μ‹€νŒ¨ν•œλ‹€.', async () => {
43+
expect(() => workspaceService.create()).rejects.toThrow(
44+
'μ›Œν¬μŠ€νŽ˜μ΄μŠ€ 이름을 μž…λ ₯ν•˜μ„Έμš” ^^',
45+
);
46+
});
47+
});
48+
49+
describe('join', () => {
50+
const USER_ID = 1;
51+
52+
it('μœ νš¨ν•œ μ°Έμ—¬μ½”λ“œκ°€ μ£Όμ–΄μ§„ 경우 μ„±κ³΅ν•œλ‹€.', async () => {
53+
const WORKSPACE_ID = 1;
54+
const WORKSPACE_NAME = 'Wab';
55+
56+
workspaceModel.findOne.mockResolvedValueOnce({
57+
id: WORKSPACE_ID,
58+
name: WORKSPACE_NAME,
59+
code: VALID_CODE,
60+
users: [],
61+
moms: [],
62+
});
63+
64+
expect(workspaceService.join(USER_ID, VALID_CODE)).resolves.toEqual({
65+
id: WORKSPACE_ID,
66+
name: WORKSPACE_NAME,
67+
code: VALID_CODE,
68+
});
69+
});
70+
71+
it('μ°Έμ—¬μ½”λ“œκ°€ μ—†λŠ” 경우 μ‹€νŒ¨ν•œλ‹€.', async () => {
72+
expect(() => workspaceService.join(USER_ID)).rejects.toThrow(
73+
InvalidJoinError,
74+
);
75+
});
76+
77+
it('μ°Έμ—¬μ½”λ“œκ°€ μœ νš¨ν•˜μ§€ μ•Šμ€ 경우 μ‹€νŒ¨ν•œλ‹€.', async () => {
78+
workspaceModel.findOne.mockResolvedValueOnce(null);
79+
80+
expect(() =>
81+
workspaceService.join(USER_ID, 'invalid-code'),
82+
).rejects.toThrow(InvalidJoinError);
83+
});
84+
85+
it('db μ—…λ°μ΄νŠΈ 쀑 μ—λŸ¬κ°€ λ°œμƒν•˜λ©΄ μ‹€νŒ¨ν•œλ‹€.', async () => {
86+
workspaceModel.updateOne.mockRejectedValueOnce(
87+
new Error('Some error in database operation'),
88+
);
89+
90+
expect(() => workspaceService.join(USER_ID, VALID_CODE)).rejects.toThrow();
91+
});
92+
});
93+
94+
export {};

β€Ž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 });
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import CustomError from '.';
22
import { BAD_REQUEST } from '@constants/http-status';
33

4-
export default class InvalidJoinError extends CustomError {
4+
class InvalidJoinError extends CustomError {
55
constructor(message = 'Unauthorized') {
66
super(message, BAD_REQUEST);
77
}
88
}
9+
10+
export default InvalidJoinError;

β€Žserver/exceptions/authorization-error.tsβ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

β€Žserver/exceptions/index.tsβ€Ž

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
Β (0)